In this tutorial you will learn about the Laravel 8 Automatic Daily Database Backup Example and its application with practical example.
In this Laravel 8 Automatic Daily Database Backup Example tutorial, I’ll show you how to take daily automatic database backup in laravel. In this tutorial you will learn to take Daily Automatic Database Backup in laravel. In this article I will guide you through process how to take daily automatic database backup of your laravel application.
Laravel 8 Automatic Daily Database Backup Example
In this step by step tutorial I’ll guide you through the process to take daily automatic database backup in laravel. Pleasae Follow the instruction given below and take daily automatic database backup in laravel apps using schedular cron job:
- Step 1: Create Command
- Step 2: Register Command In “Kernel.php”
- Step 3: Edit the “ DbBackup.php”
- Step 4: Backup Files
Step 1: Create Command
In this step we will switch to project directory as following:
1 |
cd /project directory |
Then use the below command to create command:
1 |
php artisan make:command DbBackup |
This command creates one file named DbBackup.php.
Step 2: Register Command In “Kernel.php”
Now go to app/console and open kernal.php file. And then update the following code into your file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
<?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\DbBackup' ]; /** * Define the application's command schedule. * * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { $schedule->command('db:backup')->daily(); } /** * Register the commands for the application. * * @return void */ protected function commands() { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } } |
Step 3: Edit the “ DbBackup.php”
Now, go to app/Console/Commands/ folder and open DbBackup.php. And then update the following code into your
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?php namespace App\Console\Commands; use Illuminate\Console\Command; use Carbon\Carbon; class DbBackup extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'db:backup'; /** * The console command description. * * @var string */ protected $description = 'Create Database Backup'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return mixed */ public function handle() { $filename = "backup-" . Carbon::now()->format('Y-m-d') . ".gz"; $command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " | gzip > " . storage_path() . "/app/backup/" . $filename; $returnVar = NULL; $output = NULL; exec($command, $output, $returnVar); } } |
Step 4: Backup Files
Finally the above Laravel scheduler command will take backup of database in zipped format and place file at “storage/app/backup”. So you can go to storage/app/backup folder and find daily database backup files here.