Setup Automatic Daily Database Backup with Laravel 11
In this post, we will learn how to setup automatic daily, weekly, monthly database backup in laravel 11 application.
Sometimes we work on large websites with important data, so we mostly need to take database backups every day, weekly, or monthly. Thus, we must set up a cron schedule to obtain database backups. Here, I will provide you with step-by-step instructions on how to create an automatic DB backup in Laravel.
In this example, we will create a `database:backup` command and schedule it to run daily. This command will take a backup of the database and store it in the storage folder.
Let's follow a few steps to set up automatic daily database backups using Laravel.
Step for Laravel 11 Automatic Daily Weekly Monthly Database Backup Example
- Step 1: Install Laravel 11
- Step 2: Create Command
- Step 3: Create Backup Folder
- Step 4: Schedule Command
Step 1: Install Laravel 11
First of all, we need to get a fresh Laravel 11 version application using the command below because we are starting from scratch. So, open your terminal or command prompt and run the command below:
composer create-project laravel/laravel example-app
Step 2: Create Command
In this step, we will create the DatabaseBackUp console command using Laravel Artisan command. So let's run the below command:
php artisan make:command DatabaseBackUp
Now they created DatabaseBackUp.php file in the console directory. So let's update that file with the daily update code.
app/Console/Commands/DatabaseBackUp.php
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
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';
/**
* Execute the console command.
*/
public function handle()
{
$filename = "backup-" . 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 the "backup" folder in your storage folder. You must create the "backup" folder at the following path:
storage/app/backup
Make sure you give permission to put the backup file.
Step 4: Schedule Command
Now, in this step, we need to schedule our created command, so let's update the console.php file as below:
app/Console/Kernel.php
<?php
use Illuminate\Support\Facades\Schedule;
Schedule::command('database:backup')->daily();
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 /var/www/laravel-project-folder && php artisan schedule:run >> /dev/null 2>&1
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Razorpay Payment Gateway Integration Example
- How to Install and Configuration Telescope in Laravel 11?
- Laravel 11 Google Recaptcha V3 Validation Tutorial
- Laravel 11 Image Intervention Tutorial With Example
- How to Create Event Calendar in Laravel 11?
- Laravel 11 Summernote Image Upload Tutorial
- How to use Multiple Database in Laravel 11?
- Laravel 11 Generate and Read Sitemap XML File Tutorial
- Laravel 11 Stripe Payment Gateway Integration Example
- Laravel 11 Dynamic Google Charts Integration Tutorial
- Laravel 11 Socialite Login with Twitter / X Account Example
- Laravel 11 Multi Auth: Create Multiple Authentication in Laravel 11
- Laravel 11 REST API Authentication using Sanctum Tutorial