<commit_before><?php
namespace App\Shell;

use App\Model\Table\AppTable;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\Folder;
use Cake\Utility\Hash;

class BackupShell extends Shell
{
	protected $config;

	public function initialize()
	{
		parent::initialize();

		$defaults =
			[ 'target' => ROOT . DS . 'backups' . DS
			, 'mysql' => '/usr/bin/mysql'
			, 'mysqldump' => '/usr/bin/mysqldump'
			];

		$this->config = (Hash::merge($defaults, Configure::read('Backups')));
	}

	public function main()
	{
		$this->checkApp($this->config['mysql']);
		$this->checkApp($this->config['mysqldump']);

		$this->index();
	}

	public function index()
	{
		$backups = $this->getBackupFiles();
		$headers = [ 'Filename', 'Size', 'Datetime' ];

		$this->helper('table')->output(array_merge([$headers], $backups));
	}

	public function export($descr = NULL)
	{
		$target = $this->config['target'];
		if(!is_writable($target)) {
			$this->err(sprintf('Directory `%s` not writable', $target));
			return false;
		}

		if(is_null($descr)) {
			$descr = '';
		} else {
			$descr = '_'.preg_replace("/[^a-zA-Z0-9]+/", "_", $descr);;
		}
		$filename = sprintf('%s%s%s.sql', $target , date('YmdHis'), $descr);

		if(file_exists($filename)) {
			$this->err(sprintf('File `%s` already exists', $filename));
			return false;
		}

		$tables = $this->_tableOrder(true);
		if(empty($tables)) {
			return false;
		}

		if(!$this->checkApp($this->config['mysqldump'])) {
			return false;
		}

		$this->out('Exporting database content to file:');
		$this->quiet($filename);

		$connection = ConnectionManager::getConfig('default');
		$auth = $this->_storeAuth($connection, 'mysqldump');
		$cmd = sprintf('%s --defaults-file=%s -t --result-file=%s %s %s'
					, $this->config['mysqldump'], $auth
					, $filename, $connection['database']
					, implode($tables, ' '));
		$this->verbose("exec: $cmd");
		exec($cmd);
		unlink($auth);

		if(!file_exists($filename)) {
			$this->err(sprintf('File `%s` not created', $filename));
			return false;
		}

		if(filesize($filename) == 0) {
			$this->err(sprintf('File `%s` is empty', $filename));
			return false;
		}

		$this->out('Done');
	}

	public function import($filename = NULL)
	{
		if(!Folder::isAbsolute($filename)) {
			$filename = Configure::read('Backups.target') . $filename;
		}<commit_msg>backup: export all tables except phinxlog<commit_after><?php
namespace App\Shell;

use App\Model\Table\AppTable;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\Folder;
use Cake\Utility\Hash;

class BackupShell extends Shell
{
	protected $config;

	public function initialize()
	{
		parent::initialize();

		$defaults =
			[ 'target' => ROOT . DS . 'backups' . DS
			, 'mysql' => '/usr/bin/mysql'
			, 'mysqldump' => '/usr/bin/mysqldump'
			];

		$this->config = (Hash::merge($defaults, Configure::read('Backups')));
	}

	public function main()
	{
		$this->checkApp($this->config['mysql']);
		$this->checkApp($this->config['mysqldump']);

		$this->index();
	}

	public function index()
	{
		$backups = $this->getBackupFiles();
		$headers = [ 'Filename', 'Size', 'Datetime' ];

		$this->helper('table')->output(array_merge([$headers], $backups));
	}

	public function export($descr = NULL)
	{
		$target = $this->config['target'];
		if(!is_writable($target)) {
			$this->err(sprintf('Directory `%s` not writable', $target));
			return false;
		}

		if(is_null($descr)) {
			$descr = '';
		} else {
			$descr = '_'.preg_replace("/[^a-zA-Z0-9]+/", "_", $descr);;
		}
		$filename = sprintf('%s%s%s.sql', $target , date('YmdHis'), $descr);

		if(file_exists($filename)) {
			$this->err(sprintf('File `%s` already exists', $filename));
			return false;
		}

		$tables = $this->_tableOrder(true);
		if(empty($tables)) {
			return false;
		}

		if(!$this->checkApp($this->config['mysqldump'])) {
			return false;
		}

		$this->out('Exporting database content to file:');
		$this->quiet($filename);

		$connection = ConnectionManager::getConfig('default');
		$auth = $this->_storeAuth($connection, 'mysqldump');

		$cmd = $this->config['mysqldump'];
		$cmd .= " --defaults-file=" . $auth;
		$cmd .= " -t --result-file=" . $filename;
		$cmd .= " --ignore-table=" . $connection['database'] . ".phinxlog";
		$cmd .= " " . $connection['database'];
		$this->verbose("exec: $cmd");

		exec($cmd);
		unlink($auth);

		if(!file_exists($filename)) {
			$this->err(sprintf('File `%s` not created', $filename));
			return false;
		}

		if(filesize($filename) == 0) {
			$this->err(sprintf('File `%s` is empty', $filename));
			return false;
		}

		$this->out('Done');
	}

	public function import($filename = NULL)
	{
		if(!Folder::isAbsolute($filename)) {
			$filename = Configure::read('Backups.target') . $filename;
		}
{'fraction_non_alphanumeric': 0.21611570247933884, 'fraction_numerical': 0.0012396694214876034, 'mean_word_length': 4.205376344086021, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '326267', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1722, 'n_words': 306}