From 357d032d87b2a262b9e7d19517c10b5a639f489e Mon Sep 17 00:00:00 2001 From: nickrouty Date: Fri, 30 Mar 2018 11:24:15 -0700 Subject: [PATCH 1/4] Adding ability to define a specific table to dump --- src/Console/Commands/MysqlDump.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 46c6f56..9d4dedc 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -14,6 +14,7 @@ class MysqlDump extends Command */ protected $signature = 'backup:mysql-dump {filename? : Mysql backup filename} + {--t|table= : Specific table that you want to be dumped} {--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}'; @@ -22,7 +23,7 @@ class MysqlDump extends Command * * @var string */ - protected $description = 'Dump your Mysql database to a file'; + protected $description = 'Dump your entire MySQL database or an individual table to a file'; /** * The database connection data. @@ -45,6 +46,13 @@ class MysqlDump extends Command */ protected $filename; + /** + * Table to be dumped. + * + * @var string + */ + protected $table; + /** * Local disk where backups will be stored. * @@ -140,14 +148,21 @@ protected function handleOptions() $this->isCompressionEnabled = config('backup.mysql.compress', false); } + $this->setTable(); $this->setFilename(); } + protected function setTable() + { + $table = trim($this->option('table')); + $this->table = (empty($table)) ? null : $table; + } + protected function setFilename() { $filename = trim($this->argument('filename')); if (empty($filename)) { - $filename = $this->connection['database'].'_'.\Carbon\Carbon::now()->format('YmdHis'); + $filename = $this->connection['database'].'_'.((!empty($this->table)) ? $this->table . '_' : '') . \Carbon\Carbon::now()->format('YmdHis'); } $filename = explode('.', $filename)[0]; $this->filename = $filename.'.sql'.($this->isCompressionEnabled ? '.gz' : ''); @@ -183,7 +198,7 @@ protected function storeDumpFile($data) Storage::disk($this->localDisk)->put($this->getFilePath(), $data); } $compressionMessage = $this->isCompressionEnabled ? 'and compressed' : ''; - $this->info("Database '{$this->connection['database']}' dumped {$compressionMessage} successfully"); + $this->info("Database " . ((!empty($this->table)) ? 'table ' : '') . "'{$this->connection['database']}'" . ((!empty($this->table)) ? ".'" . $this->table . "'" : '') . " dumped {$compressionMessage} successfully"); if ($this->cloudSync) { Storage::disk($this->cloudDisk)->put($this->getFileCloudPath(), $data); $this->info("Database dump '{$this->filename}' synced successfully with '{$this->cloudDisk}' disk"); @@ -199,10 +214,11 @@ protected function dumpDatabase() $password = $this->connection['password']; $databaseArg = escapeshellarg($database); + $tableArg = (empty($this->table)) ? '' : escapeshellarg($this->table); $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}"; + $dumpCommand = "{$this->mysqldumpPath} -C -h {$hostname} {$portArg} -u{$username} {$passwordArg} --single-transaction --skip-lock-tables --quick {$databaseArg} {$tableArg}"; exec($dumpCommand, $dumpResult, $result); From 9390d722d061c32c2d0d0463685dc45a8f43a5b8 Mon Sep 17 00:00:00 2001 From: nickrouty Date: Fri, 30 Mar 2018 11:27:49 -0700 Subject: [PATCH 2/4] Fixed a couple typos --- src/Console/Commands/MysqlRestore.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Console/Commands/MysqlRestore.php b/src/Console/Commands/MysqlRestore.php index 773e08b..a2949bb 100644 --- a/src/Console/Commands/MysqlRestore.php +++ b/src/Console/Commands/MysqlRestore.php @@ -13,7 +13,7 @@ class MysqlRestore extends Command * @var string */ protected $signature = "backup:mysql-restore - {--f|filename= : Especifiy a backup file name} + {--f|filename= : Specify a backup file name} {--A|all-backup-files : Display all available backup files on disk. By default displays files for current connection's database} {--C|from-cloud : Display a list of backup files from cloud disk} {--L|restore-latest-backup : Use latest backup file to restore database} @@ -25,7 +25,7 @@ class MysqlRestore extends Command * * @var string */ - protected $description = 'Restore your Mysql database from a file'; + protected $description = 'Restore your MySQL database from a file'; /** * The database connection data. From 866140d4f54a395bb1af999ab88c76bb3c76f010 Mon Sep 17 00:00:00 2001 From: nickrouty Date: Fri, 30 Mar 2018 11:36:37 -0700 Subject: [PATCH 3/4] Few code style updates --- 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 9d4dedc..474be0a 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -162,7 +162,7 @@ protected function setFilename() { $filename = trim($this->argument('filename')); if (empty($filename)) { - $filename = $this->connection['database'].'_'.((!empty($this->table)) ? $this->table . '_' : '') . \Carbon\Carbon::now()->format('YmdHis'); + $filename = $this->connection['database'].'_'.((!empty($this->table)) ? $this->table.'_' : '').\Carbon\Carbon::now()->format('YmdHis'); } $filename = explode('.', $filename)[0]; $this->filename = $filename.'.sql'.($this->isCompressionEnabled ? '.gz' : ''); @@ -198,7 +198,7 @@ protected function storeDumpFile($data) Storage::disk($this->localDisk)->put($this->getFilePath(), $data); } $compressionMessage = $this->isCompressionEnabled ? 'and compressed' : ''; - $this->info("Database " . ((!empty($this->table)) ? 'table ' : '') . "'{$this->connection['database']}'" . ((!empty($this->table)) ? ".'" . $this->table . "'" : '') . " dumped {$compressionMessage} successfully"); + $this->info('Database '.((!empty($this->table)) ? 'table ' : '') . "'{$this->connection['database']}'".((!empty($this->table)) ? ".'".$this->table."'" : '')." dumped {$compressionMessage} successfully"); if ($this->cloudSync) { Storage::disk($this->cloudDisk)->put($this->getFileCloudPath(), $data); $this->info("Database dump '{$this->filename}' synced successfully with '{$this->cloudDisk}' disk"); From 5d8d09c7b503719eda632f87d1ae1300c1197559 Mon Sep 17 00:00:00 2001 From: nickrouty Date: Fri, 30 Mar 2018 11:37:53 -0700 Subject: [PATCH 4/4] More code style updates --- src/Console/Commands/MysqlDump.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 474be0a..2cab6d0 100644 --- a/src/Console/Commands/MysqlDump.php +++ b/src/Console/Commands/MysqlDump.php @@ -198,7 +198,7 @@ protected function storeDumpFile($data) Storage::disk($this->localDisk)->put($this->getFilePath(), $data); } $compressionMessage = $this->isCompressionEnabled ? 'and compressed' : ''; - $this->info('Database '.((!empty($this->table)) ? 'table ' : '') . "'{$this->connection['database']}'".((!empty($this->table)) ? ".'".$this->table."'" : '')." dumped {$compressionMessage} successfully"); + $this->info('Database '.((!empty($this->table)) ? 'table ' : '')."'{$this->connection['database']}'".((!empty($this->table)) ? ".'".$this->table."'" : '')." dumped {$compressionMessage} successfully"); if ($this->cloudSync) { Storage::disk($this->cloudDisk)->put($this->getFileCloudPath(), $data); $this->info("Database dump '{$this->filename}' synced successfully with '{$this->cloudDisk}' disk");