diff --git a/src/Console/Commands/MysqlDump.php b/src/Console/Commands/MysqlDump.php index 46c6f56..2cab6d0 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); 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.