Laravel Automatic Daily Database Backup Example

In this post i explain automatic daily database backup in your laravel application. Laravel automatic database backup is mostly use. Also automatic database backup to you can save your information.

In this example we create automatic database backup artisan command in our application. I simply explain how to setup automatic database backup in Laravel.

We will learn example of how to set automatic daily database backup in laravel 7 and laravel 8 application. Sometime we work on big website with important data. So we most of the require to take automatic database backup every day, weekly or monthly.

Step 1: Install Laravel

In this step we install fresh laravel package for automatic database backup. So run the following command to install laravel new version application fresh code:

composer create-project --prefer-dist laravel/laravel blog

Step 2: Create DB Backup Command

In this step we have create automatic database backup daily in laravel application. So run the following command to create DB backup command:

php artisan make:command DatabaseBackUp

This command create DatabaseBackUp.php file in your console directory. In this file you can write you DB backup code:

Read Also :  Laravel Blade For Loop Example

app/Console/Commands/DatabaseBackUp.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Carbon\Carbon;

class DatabaseBackUp extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'database:backup';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Database Backup Command';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $filename = "backup-" . Carbon::now()->format('Y-m-d') . ".sql";


        $command = "".env('DUMP_PATH')." --user=" . env('DB_USERNAME') . " --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  > " . storage_path() . "/app/backup/" . $filename;


        $returnVar = NULL;
        $output = NULL;


        exec($command, $output, $returnVar);
    }
}

Step 3: Edit .env File

DUMP_PATH= C:\xampp\mysql\bin\mysqldump.exe

Step 4: Create backup Folder

In this step i create backup folder you storage app directory. So you following the must path:

storage/app/backup

Step 5: Schedule Command

In this step we require to schedule our created command. so let’s update kernel file as following:

<?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\DatabaseBackUp'
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('database:backup')->daily();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}

After following the all step enter the following command to automatic database backup in your application:

php artisan database:backup

I hope you understand laravel automatic database backup and it can help you…

Read Also :  Laravel 8 Add Watermark on Image