From d8fd00502815c2345539e2ffdfa4ea65c518fa2c Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Mon, 6 Aug 2018 16:38:26 +0200 Subject: [PATCH 1/7] Ignore a specific table with parameter Example: php artisan backup:mysql-dump my_backup --ignore-table='backups' --- src/Console/Commands/MysqlDump.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 46c6f56..ce07b5a 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -15,7 +15,8 @@ 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-table= : Ignore a specific table.}'; /** * The console command description. @@ -52,6 +53,13 @@ class MysqlDump extends Command */ protected $localDisk; + /** + * In case that a specific table has to be ignored. + * + * @var string + */ + protected $ignoredTable; + /** * Local path where the backups will be stored. * @@ -131,6 +139,7 @@ protected function handleOptions() { $compress = $this->option('compress'); $noCompress = $this->option('no-compress'); + $ignoreTable = $this->option("ignore-table"); if ($compress) { $this->isCompressionEnabled = true; @@ -140,6 +149,10 @@ protected function handleOptions() $this->isCompressionEnabled = config('backup.mysql.compress', false); } + if($ignoreTable){ + $this->ignoredTable = trim($ignoreTable); + } + $this->setFilename(); } @@ -202,7 +215,9 @@ 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}"; + $ignoreTable = $this->ignoredTable ? '--ignore-table='.$database.".".escapeshellarg($this->ignoredTable) : ''; + + $dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} {$ignoreTable} --single-transaction --skip-lock-tables --quick {$databaseArg}"; exec($dumpCommand, $dumpResult, $result); From 60d292839bea80f4d17f624dbed369885d9b6f37 Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Mon, 6 Aug 2018 16:50:58 +0200 Subject: [PATCH 2/7] Added support for multiple tables! Ability to ignore multiple tables, example: php artisan backup:mysql-dump my_backup --ignore-tables='backups,clients' --- src/Console/Commands/MysqlDump.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index ce07b5a..9e2fc42 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -16,7 +16,7 @@ class MysqlDump extends Command {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} - {--ignore-table= : Ignore a specific table.}'; + {--ignore-tables= : Ignore 1 or more tables.}'; /** * The console command description. @@ -58,7 +58,7 @@ class MysqlDump extends Command * * @var string */ - protected $ignoredTable; + protected $ignoredTables; /** * Local path where the backups will be stored. @@ -139,7 +139,7 @@ protected function handleOptions() { $compress = $this->option('compress'); $noCompress = $this->option('no-compress'); - $ignoreTable = $this->option("ignore-table"); + $ignoreTables = $this->option("ignore-tables"); if ($compress) { $this->isCompressionEnabled = true; @@ -149,8 +149,8 @@ protected function handleOptions() $this->isCompressionEnabled = config('backup.mysql.compress', false); } - if($ignoreTable){ - $this->ignoredTable = trim($ignoreTable); + if($ignoreTables){ + $this->ignoredTables = explode(",", trim($ignoreTables)); } $this->setFilename(); @@ -215,9 +215,9 @@ protected function dumpDatabase() $portArg = !empty($port) ? '-P '.escapeshellarg($port) : ''; $passwordArg = !empty($password) ? '-p'.escapeshellarg($password) : ''; - $ignoreTable = $this->ignoredTable ? '--ignore-table='.$database.".".escapeshellarg($this->ignoredTable) : ''; + $ignoredTables = $this->ignoredTables ? $this->getIgnoredTablesCommandLine($database, $this->ignoredTables) : ''; - $dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} {$ignoreTable} --single-transaction --skip-lock-tables --quick {$databaseArg}"; + $dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} {$ignoredTables} --single-transaction --skip-lock-tables --quick {$databaseArg}"; exec($dumpCommand, $dumpResult, $result); @@ -229,4 +229,11 @@ 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; + } } From 0c2351180089ded863371b6cd79ad41a42f1cfe1 Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Mon, 6 Aug 2018 16:56:17 +0200 Subject: [PATCH 3/7] Style errors --- src/Console/Commands/MysqlDump.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 9e2fc42..ca4287d 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -139,7 +139,7 @@ protected function handleOptions() { $compress = $this->option('compress'); $noCompress = $this->option('no-compress'); - $ignoreTables = $this->option("ignore-tables"); + $ignoreTables = $this->option('ignore-tables'); if ($compress) { $this->isCompressionEnabled = true; @@ -150,7 +150,7 @@ protected function handleOptions() } if($ignoreTables){ - $this->ignoredTables = explode(",", trim($ignoreTables)); + $this->ignoredTables = explode(',', trim($ignoreTables)); } $this->setFilename(); @@ -230,9 +230,9 @@ protected function dumpDatabase() } } private function getIgnoredTablesCommandLine($database, $tables){ - $command_line = ""; + $command_line = ''; foreach($tables as $table){ - $command_line .= ' --ignore-table='.escapeshellarg($database.".".$table); + $command_line .= ' --ignore-table='.escapeshellarg($database.'.'.$table); } return $command_line; } From 3d969d68aa65289a0e58ab0b11181d946d841747 Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Mon, 6 Aug 2018 16:57:21 +0200 Subject: [PATCH 4/7] More style errors --- src/Console/Commands/MysqlDump.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index ca4287d..681f5fc 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -149,7 +149,7 @@ protected function handleOptions() $this->isCompressionEnabled = config('backup.mysql.compress', false); } - if($ignoreTables){ + if ($ignoreTables){ $this->ignoredTables = explode(',', trim($ignoreTables)); } @@ -229,9 +229,10 @@ protected function dumpDatabase() $this->error("Database '{$database}' cannot be dumped"); } } - private function getIgnoredTablesCommandLine($database, $tables){ + private function getIgnoredTablesCommandLine($database, $tables) + { $command_line = ''; - foreach($tables as $table){ + foreach ($tables as $table){ $command_line .= ' --ignore-table='.escapeshellarg($database.'.'.$table); } return $command_line; From 96c127215aa0c31873462eb41af91d4215f2b130 Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Mon, 6 Aug 2018 16:58:38 +0200 Subject: [PATCH 5/7] More style errors :angry: --- src/Console/Commands/MysqlDump.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 681f5fc..12b066a 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -149,7 +149,7 @@ protected function handleOptions() $this->isCompressionEnabled = config('backup.mysql.compress', false); } - if ($ignoreTables){ + if ($ignoreTables) { $this->ignoredTables = explode(',', trim($ignoreTables)); } @@ -232,7 +232,7 @@ protected function dumpDatabase() private function getIgnoredTablesCommandLine($database, $tables) { $command_line = ''; - foreach ($tables as $table){ + foreach ($tables as $table) { $command_line .= ' --ignore-table='.escapeshellarg($database.'.'.$table); } return $command_line; From 65df7b5470d8ed0a5119733011d67db25168ba39 Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Mon, 6 Aug 2018 16:59:39 +0200 Subject: [PATCH 6/7] More style errors? Is this a joke? :laughing: --- src/Console/Commands/MysqlDump.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 12b066a..3485c76 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -229,12 +229,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; } } From c2dbace4c3cc816d75d82a6fab0c5bd9be2a9114 Mon Sep 17 00:00:00 2001 From: Alex Paredes Martinez Date: Wed, 8 Aug 2018 08:29:26 +0200 Subject: [PATCH 7/7] Only dump specific tables parameter --tables='' parameter will override --ignore-tables='' argument --- src/Console/Commands/MysqlDump.php | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 3485c76..d316cb5 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -16,7 +16,8 @@ class MysqlDump extends Command {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} - {--ignore-tables= : Ignore 1 or more tables.}'; + {--ignore-tables= : Ignore 1 or more tables.} + {--tables= : Dump specific tables, this argument will override ignore-tables.}'; /** * The console command description. @@ -60,6 +61,13 @@ class MysqlDump extends Command */ protected $ignoredTables; + /** + * These specific tables will be dumped (ignoring ignoredTables argument) + * + * @var string + */ + protected $tables; + /** * Local path where the backups will be stored. * @@ -140,6 +148,7 @@ 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; @@ -149,8 +158,10 @@ protected function handleOptions() $this->isCompressionEnabled = config('backup.mysql.compress', false); } - if ($ignoreTables) { + if ($ignoreTables && !$tables) { $this->ignoredTables = explode(',', trim($ignoreTables)); + }else if($tables){ + $this->tables = explode(',', trim($tables)); } $this->setFilename(); @@ -216,8 +227,9 @@ protected function dumpDatabase() $passwordArg = !empty($password) ? '-p'.escapeshellarg($password) : ''; $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}"; + $dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} {$ignoredTables} --single-transaction --skip-lock-tables --quick {$databaseArg} {$tables}"; exec($dumpCommand, $dumpResult, $result);