Laravel 9 Daily Weekly Monthly Automatic Database Backup

Hi,

In this tutorial, you will learn laravel 9 database backup daily. this example will help you laravel 9 database weekly backup. Here you will learn laravel 9 database monthly backup. we will help you to give example of laravel 9 automatic database backup.

Sometimes we work on large websites 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 get database backup. here I will give you step-by-step instructions on how to create an automatic DB backup in laravel.

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

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

Laravel 9 Daily Weekly Monthly Automatic Database Backup

Step 1: Install Laravel

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

composer create-project laravel/laravel blog

Step 2: Create Command

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

.medrectangle-4-multi-155{border:none!important;display:block!important;float:none;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

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

<?php

namespace AppConsoleCommands;

use IlluminateConsoleCommand;

use CarbonCarbon;

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);

}

}

Step 3: Create Backup Folder

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

<?php

namespace AppConsole;

use IlluminateConsoleSchedulingSchedule;

use IlluminateFoundationConsoleKernel as ConsoleKernel;

class Kernel extends ConsoleKernel

{

/**

* The Artisan commands provided by your application.

*

* @var array

*/

protected $commands = [

'AppConsoleCommandsDatabaseBackUp'

];

/**

* Define the application's command schedule.

*

* @param IlluminateConsoleSchedulingSchedule $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 manually with 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.

Setup on Server:

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

You can add following line to your crontab file. you have to change folder path.

* * * * * 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

I hope it can help you…

You can also follow this tutorial for Backup with Laravel App: Todo with laravel backup service.

Leave a Comment