Skip to content

Instantly share code, notes, and snippets.

@syofyanzuhad
Created March 4, 2025 06:42
Show Gist options
  • Select an option

  • Save syofyanzuhad/c1a8ee8ba2e267edd6453e75cbb6823a to your computer and use it in GitHub Desktop.

Select an option

Save syofyanzuhad/c1a8ee8ba2e267edd6453e75cbb6823a to your computer and use it in GitHub Desktop.
Laravel backup command without any dependencies
<?php
use Illuminate\Database\Console\Command;
/*
*
* // routes/console.php
* Schedule::command('db:backup')->daily();
*
*/
class BackupCommand extends Command
{
protected $signature = 'db:backup {connection?} {--read} {--write}';
protected $description = 'Backup the database.';
public function handle(): void
{
Process::run(fn() => [
'mysqldump',
$this->commandArguments($this->getConnection()),
'>',
Storage::path('backups/' . date('Y-m-d-His') . '.sql')
]);
$this->cleanup();
}
public function cleanup(): void
{
// Cleanup old backups.
$backups = collect(Storage::files('backups'));
$backups->each(fn($file) => Storage::delete($file));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment