Laravel 8 Automatic Daily Database Backup Example

Laravel 8 Automatic Daily Database Backup Example

Automatic daily database backup example; In this tutorial, you will learn how to automatic daily backup of database in laravel 8 apps.

In this laravel automatic database backup example create a class with a command and schedule the use class with the help of laravel scheduler. With this it will automatically back up the database daily and store it in a directory at a fixed time.

Mysqldump is a command-line utility that is used to generate the logical backup of the MySQL database. It produces the SQL Statements that can be used to recreate the database objects and data. Now, when you restore the database, the command executes all the SQL Statements to create tables and insert the data. See this article to learn more about the different types of SQL database backup.

But this tutorial will help you step by step to take automatic database backup daily using artisan command in laravel apps.

Automatic Daily, Weekly, Monthly Database Backup in Laravel 8 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

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 *