How to Set Up Automatic Daily Database Backup with Laravel 12

How to Set Up Automatic Daily Database Backup with Laravel 12

Data is the backbone of any application. Imagine losing your entire database due to a server crash, hacker attack, or accidental deletion — terrifying, right? This is why automated daily database backups are essential for every Laravel application.

Laravel Development Services

Sometimes, we work on large websites that contain important and sensitive data. In such cases, it’s essential to regularly back up the database, whether daily, weekly, or monthly, to ensure data safety and recovery in case of any failure. In this post, we will learn how to set up automatic daily, weekly, and monthly database backups in a Laravel 12 application.

Why Automate Backups?

Manual backups are unreliable. People forget, make mistakes, or miss schedules. Automated backups ensure:

  • Consistent backups
  • Faster recovery in case of failure
  • Peace of mind for developers and stakeholders

Steps to Setup Automatic Daily Database Backup with Laravel 12:

Step 1: Install Laravel 12

composer create-project laravel/laravel magecomp-project

Step 2: Create Command

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

php artisan make:command Databasebackup

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

Open this file 

app/Console/Commands/Databasebackup.php

Add the below code:

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

Open below file

routes/console.php

And add the following code

<?php
use Illuminate\Support\Facades\Schedule;
Schedule::command('database:backup')->daily();

You can check manually with the following command to get the database backup.

php artisan database:backup

It will create one backup file in your backup folder. You can check there.

Setup on Server:

Now, we are ready to set up cron on our server.

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

Run the following command:

crontab -e

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

Conclusion:

With Laravel 12, automating daily database backups is quick, reliable, and customizable. Don’t wait for a disaster to remind you of the importance of backups — automate today and safeguard your data!

Need help setting up backups for your Laravel project? Drop a comment or contact us — we’re here to help!

Hire laravel Developer
Previous Article

Magento 2: How to Get Customer Data after Login with Observer

Next Article

Magento 2: How to Store Existing Value of Dynamic Add Configuration Field

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨