Laravel 8 Automatic Daily Database Backup Tutorial


Share:
Published By: Admin, Published On: Dec 09, 2021, Category: Laravel 8 Laravel


Hi Dev,

Today, in this article I will share with you something new about how to set automatic database backup in laravel 8 application. in laravel 8 application, we will show export database to SQL. but in this tutorial, I will how to take database backup in laravel 8 application.

So, Suppose you will work on a large databases project so in your mind question raise how to handle big database backup should you need to laravel provide a cron schedule to getting database backup you set time e.g. every day, month or year. I will give you a full example of how to automatic database backup in laravel 8 as below so follow my all steps.

Let's follow a few steps and set auto daily database backup using laravel 8.

Step 1 : Install Laravel 8

In the first step, we need to get fresh laravel 8 version application So let's open terminal and run bellow command to install fresh laravel project.

composer create-project --prefer-dist laravel/laravel backup
Step 2 : Create Command

In second step, we will create console command using laravel artisan command so let's below run command:

php artisan make:command DatabaseBackUp

Now successfully run command after they created DatabaseBackUp.php file on console directory. so let's update that file with daily update code.

Path: app/Console/Commands/DatabaseBackUp.php
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Storage;
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 = 'Command description';

    /**
     * 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') . ".gz";
        $command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . "  | gzip > " .Storage::path("public/database/". $filename);

        $returnVar = NULL;
        $output  = NULL;

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

    }
}
Step 3: Create backup Folder

In this step, we will created database folder in your storage folder in below following path:

storage/app/public/database

make sure you give permission to put backup file.

Step 4: Schedule Command

Now in this step schedule our created command, so let's update a Kernel.php file

Path : app/Console/Kernel.php
<?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 = [
        //
    ];

    /**
     * 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');
    }
}

you can check following command to getting database backup with this command:

php artisan database:backup

It will create one backup file on your backup folder. you can check there.

Now, we are ready to setup cron on our server.

At last you can manage this command on scheduling task, you have to add a single entry to your server’s crontab file:

Run following command:

crontab -e
* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1

OR
 
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

It will help you...

Share: