How to set auto database backup with cron scheduler in Laravel
This tutorial is on How to set auto database backup with the cron scheduler in Laravel 8. We will set auto database backUp using the cron scheduler in Laravel, as you all know database is a very important part of any website backUp and we need to take backup. Laravel cron job scheduling we will set MySQL backups in Laravel, you can also set daily, weekly, or monthly database backups using a cron job.
Create Command
First, we need to create a command for auto database backUp using the cron scheduler.
php artisan make:command Auto_Backup Output: Console command created successfully.
Now Auto_Backup.php (app/Console/Commands/Auto_Backup.php)
file created on the console directory.
<?php #app/Console/Commands/Auto_Backup.php namespace App\Console\Commands; use Illuminate\Console\Command; class Auto_Backup extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'database:backup'; //change /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { // added $filename = "backup-".time().".gz"; $command = "mysqldump --user=" . env('DB_USERNAME') ." -- password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " ". env('DB_DATABASE') . " | gzip > " . storage_path() ."/app/backups/".$filename; $returnVar = NULL; $output = NULL; exec($command, $output, $returnVar); } }
Create Backup Folder
Now, create a new folder in the storage folder to store our backups of the database and make sure that give permission to put backups files.
Schedule Command
Now, we will update Kernel.php(app/Console/Kernel.php)
file.
<?php #app/Console/Kernel.php namespace App\Console; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { /** * The Artisan commands provided by your application. * * @var array */ protected $commands = [ 'App\Console\Commands\Auto_Backup' //added ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('database:backup')->hourly(); //change // $schedule->command('database:backup')->daily(); //added } /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } }
Manually check
We can check manually database backup using the below command:
php artisan database:backup
It will create one backups file in your backups folder. Now, we will set up cron in our live server.
crontab -e
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
OR
* * * * * cd /path_your_project && php artisan schedule:run >> /dev/null 2>&1
Now, it will take a backup of your database on a daily base.