Setup Automatic Daily Database Backup with Laravel 11

By Hardik Savani September 4, 2024 Category : Laravel

In this post, we will learn how to setup automatic daily, weekly, monthly database backup in laravel 11 application.

Sometimes we work on large websites with important data, so we mostly need to take database backups every day, weekly, or monthly. Thus, we must set up a cron schedule to obtain database backups. Here, I will provide you with step-by-step instructions on how to create an automatic DB backup in Laravel.

In this example, we will create a `database:backup` command and schedule it to run daily. This command will take a backup of the database and store it in the storage folder.

Let's follow a few steps to set up automatic daily database backups using Laravel.

laravel 11 daily database backup

Step for Laravel 11 Automatic Daily Weekly Monthly Database Backup Example

  • Step 1: Install Laravel 11
  • Step 2: Create Command
  • Step 3: Create Backup Folder
  • Step 4: Schedule Command
  • >

Step 1: Install Laravel 11

First of all, we need to get a fresh Laravel 11 version application using the command below because we are starting from scratch. So, open your terminal or command prompt and run the command below:

composer create-project laravel/laravel example-app

Step 2: Create Command

In this step, we will create the DatabaseBackUp console command using Laravel Artisan command. So let's run the below command:

php artisan make:command DatabaseBackUp

Now they created DatabaseBackUp.php file in the console directory. So let's update that file with the daily update code.

app/Console/Commands/DatabaseBackUp.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

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';

    /**
     * Execute the console command.
     */
    public function handle()
    {
        $filename = "backup-" . 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

In this step, we need to create the "backup" folder in your storage folder. You must create the "backup" folder at the following path:

storage/app/backup

Make sure you give permission to put the backup file.

Step 4: Schedule Command

Now, in this step, we need to schedule our created command, so let's update the console.php file as below:

app/Console/Kernel.php

<?php

use Illuminate\Support\Facades\Schedule;

Schedule::command('database:backup')->daily();

you can check manually with 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.

Setup on Server:

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

You can add following line to your crontab file. you have to change folder path.


* * * * * php /path/to/artisan schedule:run 1>> /dev/null 2>&1
   
OR
  
* * * * * cd /var/www/laravel-project-folder && php artisan schedule:run >> /dev/null 2>&1

I hope it can help you...

Shares