Home Laravel How To Make Daily Weekly Monthly Automatic Database Backup In Laravel 9?

How To Make Daily Weekly Monthly Automatic Database Backup In Laravel 9?

by Shahriar Sagor

Hello Dev,Today now in this post i will show you laravel 9 database backup daily. Now in this example i will help you laravel 9 database weekly backup. Here i will teach you laravel 9 database monthly backup. Here i will help you to give an example of laravel 9 automatic database backup.

Step 1: Install Laravel

At first of all we have to get the fresh Laravel version application by using the bellow command. So open your terminal OR command prompt and then run the bellow command:

composer create-project laravel/laravel blog
Step 2: Create Command

Now in this step, i will create the  DatabaseBackUp console command by using the laravel artisan command. so let’s run the bellow command:

php artisan make:command DatabaseBackUp

Now it will created DatabaseBackUp.php file on 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;
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

Now in this step, i have to create “backup” folder in my storage folder. i must have to create “backup” on following path:

storage/app/backup

And make sure i have to give permission to put backup file.

Step 4: Schedule Command

Now, in this step, i have to schedule our created command. so let’s update the kernel file as like as bellow:

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

now i can check the manually with following command to getting database backup with this command:

php artisan database:backup

Then It will be create one backup file on our backup folder. we can check there.

Setup on Server:

Now, i am ready to setup cron on our server.

At last we can manage this command on scheduling task, we have to add a single entry to our server’s crontab file:

Just Run the following command:

crontab -e

We can add the following line to our crontab file. We have to change the folder path.

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

Read Also: php artisan serve not working in Laravel

Thanks for read. I hope it help you. For more you can follow us on facebook

close

You may also like