mysqldump -u[user] -p[pass] [db] > [file_path]
But what if you want to automate the process? Listing down the steps one by one:
* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1
php artisan make:command BackupDatabase
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class BackupDatabase extends Command
{
protected $signature = 'db:backup';
protected $description = 'Backup the database';
protected $process;
public function __construct()
{
parent::__construct();
$this->process = new Process(sprintf(
'mysqldump -u%s -p%s %s > %s',
config('database.connections.mysql.username'),
config('database.connections.mysql.password'),
config('database.connections.mysql.database'),
storage_path('app/backups/backup-' . time() . '.sql')
));
}
public function handle()
{
try {
$this->process->mustRun();
$this->info('The backup has been proceed successfully.');
} catch (ProcessFailedException $exception) {
$this->error('The backup process has been failed.');
}
}
}
$schedule->command('db:backup')->saturdays()->at('23:00');
* !.gitignore
That’s it you’ve now enabled weekly automated database backups
For more information and credits: https://pineco.de/scheduling-mysql-backups-with-laravel/