Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore specific tables with parameter or only dump some tables #25

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Console/Commands/MysqlDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class MysqlDump extends Command
protected $signature = 'backup:mysql-dump
{filename? : Mysql backup filename}
{--no-compress : Disable file compression regardless if is enabled in the configuration file. This option will be always overwrited by --compress option}
{--compress : Enable file compression regardless if is disabled in the configuration file. This option will always overwrite --no-compress option}';
{--compress : Enable file compression regardless if is disabled in the configuration file. This option will always overwrite --no-compress option}
{--ignore-tables= : Ignore 1 or more tables.}
{--tables= : Dump specific tables, this argument will override ignore-tables.}';

/**
* The console command description.
Expand Down Expand Up @@ -52,6 +54,20 @@ class MysqlDump extends Command
*/
protected $localDisk;

/**
* In case that a specific table has to be ignored.
*
* @var string
*/
protected $ignoredTables;

/**
* These specific tables will be dumped (ignoring ignoredTables argument)
*
* @var string
*/
protected $tables;

/**
* Local path where the backups will be stored.
*
Expand Down Expand Up @@ -131,6 +147,8 @@ protected function handleOptions()
{
$compress = $this->option('compress');
$noCompress = $this->option('no-compress');
$ignoreTables = $this->option('ignore-tables');
$tables = $this->option('tables');

if ($compress) {
$this->isCompressionEnabled = true;
Expand All @@ -140,6 +158,12 @@ protected function handleOptions()
$this->isCompressionEnabled = config('backup.mysql.compress', false);
}

if ($ignoreTables && !$tables) {
$this->ignoredTables = explode(',', trim($ignoreTables));
}else if($tables){
$this->tables = explode(',', trim($tables));
}

$this->setFilename();
}

Expand Down Expand Up @@ -202,7 +226,10 @@ protected function dumpDatabase()
$portArg = !empty($port) ? '-P '.escapeshellarg($port) : '';
$passwordArg = !empty($password) ? '-p'.escapeshellarg($password) : '';

$dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} --single-transaction --skip-lock-tables --quick {$databaseArg}";
$ignoredTables = $this->ignoredTables ? $this->getIgnoredTablesCommandLine($database, $this->ignoredTables) : '';
$tables = $this->tables ? implode(" ", $this->tables) : "";

$dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} {$ignoredTables} --single-transaction --skip-lock-tables --quick {$databaseArg} {$tables}";

exec($dumpCommand, $dumpResult, $result);

Expand All @@ -214,4 +241,14 @@ protected function dumpDatabase()
$this->error("Database '{$database}' cannot be dumped");
}
}

private function getIgnoredTablesCommandLine($database, $tables)
{
$command_line = '';
foreach ($tables as $table) {
$command_line .= ' --ignore-table='.escapeshellarg($database.'.'.$table);
}

return $command_line;
}
}