Created
March 4, 2025 06:42
-
-
Save syofyanzuhad/c1a8ee8ba2e267edd6453e75cbb6823a to your computer and use it in GitHub Desktop.
Laravel backup command without any dependencies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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