Laravel 9 Daily Weekly Monthly Automatic Database Backup

Laravel 9 Daily Weekly Monthly Automatic Database Backup

Automatic database backup in laravel 9; In this tutorial, we will learn how to automatic daily, monthly, weekly backup of database in laravel 9 apps.

In this Laravel Automatic Database Backup, you need to create a class using Artisan command. And then scheduling this command with class using laravel scheduler. With this it will automatically take daily, weekly and monthly backup of the database and store it in a directory at a given time.

Laravel 9 Daily Weekly Monthly Automatic Database Backup

Use the following steps to take Automatically Backup Database Daily Weekly Monthly Laravel 9 apps:

  • Step 1: Create Command
  • Step 2: Register Command In “Kernel.php” 
  • Step 3: Edit the “ DbBackup.php” 
  • Step 4: Backup Files

Step 1: Create Command

First of all use the below command to navigate to your laravel app directory:

cd /project directory

Then use the below command to create command:

php artisan make:command DbBackup

This command creates one file named DbBackup.php.

Step 2: Register Command In “Kernel.php” 

Next step, Navigate to app/console and open kernal.php file. And then update the following code into your file:

<?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\DbBackup'
    ];
    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('db:backup')->daily();
    }
    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');
        require base_path('routes/console.php');
    }
}

Step 3: Edit the “ DbBackup.php” 

Now, Navigate to app/Console/Commands/ folder and open DbBackup.php. And then update the following code into your file

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Carbon\Carbon;
class DbBackup extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'db:backup';
/**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create Database Backup';
/**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }
    /**
     * Execute the console command.
     *
     * @return mixed
     */
    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 4: Backup Files

The above Laravel scheduler command will take backup of database in zipped format and place file at “storage/app/backup”.

So you can navigate storage/app/backup folder and find daily database backup files here.

Conclusion

In this tutorial, you have learned how to schedule daily database backup using this command in laravel apps.

Recommended Laravel Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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