Automatically Backup Database Laravel 10

Automatically Backup Database Laravel 10

If you take manual backup of your Laravel application’s database on daily basis. Then this tutorial is for you. By using this tutorial you can take automatically daily, weekly, monthly, and yearly backup of your Laravel application. So, In this tutorial, you will learn how to automatic daily, weekly and monthly backups of database in Laravel 10 apps.

How to Automatically Backup Database in Laravel 10

By using the following steps, you can automatically take daily, weekly, and monthly database backups in Laravel 10 apps and save them to in directory:

  • Step 1: Create Command
  • Step 2: Setup Command In “Kernel.php” 
  • Step 3: Update the “DbBackup.php” File
  • Step 4: Backup Files

Step 1: Create Command

Firstly, Open your terminal or cmd and execute the following command to navigate to your laravel app directory:

cd /project directory

Then execute the following command on terminal or command prompt to create backup command:

php artisan make:command DbBackup

This command creates one file named DbBackup.php.

Step 2: Setup Command In “Kernel.php” 

Once you have created backup command. Now, you need to setup with kernal.php.

So, 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: Update the “DbBackup.php” File

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

<?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 a backup of the database in zipped format and place the file at “storage/app/backup”.

So you can navigate the 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

Recommended:-Laravel Try Catch

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of 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 from a starting stage. As well as demo example.

Leave a Reply

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