Hello Artisan
In this example tutorial i will show you how we can create daily laravel database backup example. I will discuss laravel backup database from scratch. So you can learn laravel database backup and restore from start to finish.
We will look at the example of how to set automatic database backup in laravel. Sometimes we create large scale application and sometimes it contains our very important information of our website. That's why we should take database backup every day, weekly or monthly. So we must need to run daily cron schedule to getting database backup. Here i will give you step by step instruction how to create automatic db backup in laravel.
So in this auto backup in laravel tutorial, we will simply run daily cron schedule in laravel to create laravel daily backup. This command will take backup of database and put into the storage folder.
So now Let's follow some few steps to set auto daily database backup using laravel 8 application.
Step 1: Install Laravel
In this step, as we start from scratch so download a fresh laravel 7 app by this following command.
composer create-project --prefer-dist laravel/laravel blog
Step 2: Create Command
In this second step, we will create laravel 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.
app/Console/Commands/DatabaseBackUp.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);
}
}
Read also : Laravel 7.x Queues Example with Redis and Horizon
Step 3: Create backup Folder
In this third step, we have to create "backup" folder in your storage folder. you have to create "backup" on following path:
make sure that you have to give permission to put backup file.
Step 4: Schedule Command
Now, in this fourth step, we have to run schedule our created command. So now time to update kernel file as like bellow:
app/Console/Kernel.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 we can check following command to have database backup with this command:
php artisan database:backup
It will create one backup file in our backup folder. you can check there. Now, we are ready to setup cron in our live server. At last you can manage this command on scheduling task, we have to add a single entry to your server’s crontab file:
Run following command:
crontab -e
Read also : Laravel Cron Jobs with Task Scheduler Example
* * * * * 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
Hope this automatic laravel database backup tutorial will help you.
#laravel #laravel-6 #laravel-7 #laravel-7x #database-backup #task-scheduler #cron-job #artisan-command