Laravel 9 Daily Weekly Monthly Automatic Database Backup

In this article you will be learning about Laravel 9 database weekly backup. Most of the need to take database backup every day, weekly or monthly. In this example, we will be creating a database: backup. Let’s follow few step and set auto daily database backup using Laravel.

Step 1: Install Laravel

If you haven’t installed Laravel application, then run the below command:

composer create-project laravel/laravel blog

Step 2: Create Command

Create DatabaseBackUp console command using Laravel artisan command. Run the below command:

php artisan make:command DatabaseBackUp

Now, we have created DatabaseBackUp.php file on console directory. Let’s update that file with daily update code.

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 = '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() . "/app/backup/" . $filename;
  
        $returnVar = NULL;
        $output  = NULL;
  
        exec($command, $output, $returnVar);
    }

Step 3: Create Backup Folder

Create “backup” folder in your storage folder. you must have to create “backup” on following path:

storage/app/backup

Make sure you give permission to put backup file.

Step 4: Schedule Command

Schedule our created command. Let’s update kernel file as below:

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 = [
        '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');
    }
}

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

php artisan database:backup

Create one backup file on your backup folder. you can check there.

Setup on Server

Set up the cron on our server.

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

Run following command

crontab -e

You can add following line to your crontab file. Change the folder path.

Example

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