Laravel 11 Automatic Daily Database Backup Example

Laravel 11 automatic daily database backup example; Through this tutorial, i am going to show you how to automatic daily backup of database in Laravel 11 apps.

Laravel 11 Automatic Daily, Weekly, Monthly Database Backup

Use the below given steps to create automatically daily, weekly, monthly and database backup in Laravel 11 apps:

  • 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

Run the following command on command prompt to navigate to 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” 

Go 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” 

Go 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.

Recommended Laravel Tutorials

Leave a Comment