Automatic Daily Database Backup with Laravel 6 by Francis Mwakatumbula

Automatic Daily Database Backup with Laravel 6

Francis Mwakatumbula
Lockwood Technology
3 min readAug 1, 2020

--

This article will show you step by step how to automate database backups daily using Laravel Framework on your project.

In this example, you will learn laravel database backup example. i would like to show you laravel export database to sql. let’s discuss about how to take database backup in laravel. We will look at example of how to set automatic database backup in laravel.

Sometime we work on large website with important data. so we most of the need to take database backup every day, weekly or monthly. so we must need to cron schedule to getting database backup. here i will give you step by step instruction how to create automatic db backup in laravel.

In this example, we will create database:backup and we will schedule daily this command to run. this command will take backup of database and put into the storage folder.

Let’s follow few step and set auto daily database backup using laravel 6 and laravel 7.

Step 1: Install Laravel

In this step, if you haven’t laravel application setup then we have to get fresh laravel 6 application. So run below command and get clean fresh laravel 6 application.

composer create-project --prefer-dist laravel/laravel site

Step 2: Create Command

In this step, we will create console command using laravel artisan command. so let’s run bellow command:

php artisan make:command DatabaseBackUp

Now they created DatabaseBackUp.php file on console directory. so let’s update that file with daily update code.

Note: if your using Wamp or xamp server create a DUMP_PATH on .env file DUMP_PATH=C:\wamp\bin\mysql\mysql5.7.26\bin\mysqldump.exe

phpStorm 2020: app/Console/Commands/DatabaseBackUp.php

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') . ".sql";


$command = "".env('DUMP_PATH')." --user=" . env('DB_USERNAME') . " --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;


$returnVar = NULL;
$output = NULL;


exec($command, $output, $returnVar);
}
}

Step 3: Create backup Folder to store the DB

In this step, we need to create “backup” folder in your storage folder. you must have to create “backup” on following path:

storage/app/backup

make sure you give permission to put backup file.

Step 4: Schedule Command

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

app/Console/Kernel.php

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

you can check following command to getting database backup with this command:

php artisan database:backup

It will create one backup file on your backup folder. you can check there.

Now, we are ready to setup cron on our server.

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

Run following command:

crontab -e

That’s it. You now have all the steps needed to create auto DB backup on your laravel project. i’d appreciate your feedback, and claps too 👏.

--

--

Francis Mwakatumbula
Lockwood Technology

Full stack Developer for Mobile and Web Based application,Software Engineer, #flutter #Nodejs #php