Automatic Daily, Weekly, Monthly Database Backup in Laravel Apps

An automatic daily database backup is a process that creates a copy of your database on a regular basis, typically every day. This copy, or backup, serves as a safety net in case your original database gets corrupted, damaged, or accidentally deleted.

In this tutorial we will learn how to build an application that automatically backs up the database daily, weekly, monthly in Laravel 8 application. In this laravel automatic database backup example create a class with a command and schedule the use class with the help of laravel scheduler. With this it will automatically back up the database daily and store it in a directory at a fixed time.

Step 1: Create Command

First of all use the below command to navigate to your laravel app directory:

cd /project directory

Then use the below command to create command:

php artisan make:command DbBackup

This command creates one file named DbBackup.php.

Step 2: Register Command In “Kernel.php” 

Next step, Navigate to app/console and open kernal.php file. And then update the following code into your file:

<?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, Navigate to app/Console/Commands/ folder and open DbBackup.php. And then update the following code into your file

<?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

The above Laravel scheduler command will take backup of database in zipped format and place file at “storage/app/backup”.

So you can navigate storage/app/backup folder and find daily database backup files here.

Conclusion

In this tutorial, you have learned how to schedule daily database backup using this command in laravel apps.

#laravel #web-development #webdev #database

Automatic Daily, Weekly, Monthly Database Backup in Laravel Apps
14.50 GEEK