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

Ability to specify an individual table for dump #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
24 changes: 20 additions & 4 deletions src/Console/Commands/MysqlDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}';

Expand All @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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' : '');
Expand Down Expand Up @@ -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");
Expand All @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions src/Console/Commands/MysqlRestore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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.
Expand Down