How to Set Auto Database BackUp using Cron Scheduler In Laravel

Websolutionstuff | Feb-18-2021 | Categories : Laravel PHP

In this article, we will see how to set auto database backup using the cron scheduler in laravel. here we will set auto database backup using the cron scheduler. As you all know the database is a very important part of any website and we need to take a backup as per our requirements. Using cron job scheduling in laravel 7 and laravel 8 we will set MySQL backups in laravel, you can also set daily, weekly, or monthly database backups using a cron job.

So, let's see laravel database backup daily using the cron scheduler and automatic database backup using laravel 7/8.

Step 1: Install Laravel

Step 2: Create Command For Database Backup

Step 3: Create Backup Folder

Step 4: Schedule Command For Auto Backup Database

 

Step 1: Install Laravel

Install the laravel application if required.

 

Step 2: Create Command

Now, we will create a command for auto database backup using the cron scheduler.

php artisan make:command Auto_Backup_Database

 

As of now DatabaseBackUp.php file created on the console directory. So, edit that file with the daily update code.

app/Console/Commands/Auto_Backup_Database.php

namespace App\Console\Commands;
  
use Illuminate\Console\Command;
use Carbon\Carbon;
   
class Auto_Backup_Database 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

Now, we need to create a new folder in your storage folder to store our backup of the database and make sure that you have given permission to put a backup file.

 

Step 4: Schedule Command

In this step, we will update the kernel file in this path app/Console/Kernel.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\Auto_Backup_Database'
    ];
  
    /**
     * 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');
    }
}

 

We can check the database backup using the below command.

php artisan database:backup

 

It will create one backup file in your backup folder. Now, we will set up cron in our live server. At last, you can manage this command on scheduling tasks, we have to add a single entry to your server’s crontab file.

crontab -e

 

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

Now, it will take auto backup of your database on daily basis.

 


You might also like:

Recommended Post
Featured Post
How To File Upload In Laravel 10 Example
How To File Upload In Laravel...

In this article, we will see how to file upload in laravel 10 examples. Here, we will learn about the laravel...

Read More

Mar-15-2023

Laravel 11 Livewire 3 wire:submit - Form Submit Event
Laravel 11 Livewire 3 wire:sub...

In this tutorial, I will guide you on how to handle form submission in Laravel 11 using Livewire 3. With Livewire 3, you...

Read More

Mar-20-2025

How To Setup Cron Job Task Scheduler In Laravel 10
How To Setup Cron Job Task Sch...

In this article, we will delve into the process of setting up a cron job task scheduler in Laravel 10. Our focus will be...

Read More

Apr-05-2023

Laravel mix is not recognized as an internal or external command
Laravel mix is not recognized...

Greetings, developers! If you've encountered the frustrating "Laravel Mix is not recognized as an internal...

Read More

Dec-29-2023