Skip to content

Instantly share code, notes, and snippets.

@btxtiger
Last active February 14, 2024 16:53
Show Gist options
  • Save btxtiger/00cdf77d20baf2fc7038b9068c3fe919 to your computer and use it in GitHub Desktop.
Save btxtiger/00cdf77d20baf2fc7038b9068c3fe919 to your computer and use it in GitHub Desktop.
Laravel MySQL DB Backup Command
<?php
namespace App\Console\Commands\DB;
use Illuminate\Console\Command;
class BackupDbImportCmd extends Command {
/**
* The name and signature of the console command.
*/
protected $signature = 'db:backup:import {--select= : Select a backup to import}';
/**
* The console command description.
*/
protected $description = 'Import a backup of the database';
/**
* Execute the console command.
*/
public function handle() {
$availableBackups = glob(storage_path('app/backups/db/*.sql'));
$selectedBackup = end($availableBackups);
if ($this->option('select')) {
$selectedBackup = $this->choice('Select a backup to import', $availableBackups);
}
$this->info('Importing backup from ' . $selectedBackup);
// Delete all tables in the target database
$tables = \DB::select('SHOW TABLES');
foreach ($tables as $table) {
$tableName = array_values((array) $table)[0];
\DB::statement('SET FOREIGN_KEY_CHECKS=0;');
\DB::statement("DROP TABLE IF EXISTS $tableName");
\DB::statement('SET FOREIGN_KEY_CHECKS=1;');
}
// Import the backup
$host = config('database.connections.mysql.host');
$username = config('database.connections.mysql.username');
$password = config('database.connections.mysql.password');
$database = config('database.connections.mysql.database');
$password = $password ? "-p$password" : '';
$command = sprintf('mysql -h%s -u%s %s %s < %s', $host, $username, $password, $database, $selectedBackup);
exec($command);
$this->info('Backup imported from ' . $selectedBackup);
}
}
<?php
namespace App\Console\Commands\DB;
use Illuminate\Console\Command;
class DbBackupCreateCmd extends Command {
/**
* The name and signature of the console command.
*/
protected $signature = 'db:backup:create { --keep-last= : Keep the last n backups }';
/**
* The console command description.
*/
protected $description = 'Create a backup of the database';
/**
* Execute the console command.
*/
public function handle() {
$dir = storage_path('app/backups/db');
if (!is_dir($dir)) {
if (!mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dir));
}
}
$dumpFile = $dir . '/db-backup-' . time() . '.sql';
$host = config('database.connections.mysql.host');
$username = config('database.connections.mysql.username');
$password = config('database.connections.mysql.password');
$database = config('database.connections.mysql.database');
$password = $password ? "-p$password" : '';
$command = sprintf('mysqldump -h%s -u%s %s %s > %s', $host, $username, $password, $database, $dumpFile);
exec($command);
$this->info('Backup created at ' . $dumpFile);
if ($keepLast = $this->option('keep-last')) {
$this->info('Keeping the last ' . $keepLast . ' backups');
$backups = glob($dir . '/*.sql');
$backups = array_combine($backups, array_map('filemtime', $backups));
arsort($backups);
$backups = array_keys($backups);
$backups = array_slice($backups, $keepLast);
foreach ($backups as $backup) {
unlink($backup);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment