Hi Developers,
Lets see How To Set Automatic Database Backup Daily In Laravel 10, In this post we will learn how to setup automatic daily backup of the database and the store it in the public path. In large website we need to take backup regularly in order to maintain the records properly so instead of taking the backup manually we can setup backup automatically . This tutorial consist of two phases,
1 ) Creating the custom artisan command to take backup from database.
2 ) Then scheduling the artisan command based on requirement.
So lets see How To Set Automatic Database Backup Daily In Laravel 10 .
Learn to automate weekly database backups in Laravel 10. Utilize Laravel's built-in scheduler and artisan commands for seamless, reliable backups.
Lets create the custom Artisan command using the the following artisan command,
php artisan make:command DatabaseBackUp
the above artisan command will create the DatabaseBackUp.php file in the following directory, Lets code to take backup when the command gets executed. We need to add the database information entered in the .env file since we need to access the database for taking the backup. And also the name of the file the will be generated.
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(): void
{
$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);
}
}
Since we have setup to store the backup file in storage/app/backup so we need to create the folder in the storage folder to make sure the folder is available while the command gets executed,
storage/app/backup
Still now we have created the custom artisan command and also set to take backup everytime when the command gets executed, Now we need to schedule the command to execute the command automatically based on our requirement. We are going to schedule daily to take backup daily this can be changed based on the requirement.
app/Console/Kernel.php
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command('database:backup')
->daily();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}
After Scheduling the our custom artisan command we need to make some changes in the crontab file. So lets run the following command,
crontab -e
then we need to add the line in the crontab file,
* * * * * 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
We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.
Be the first to know about releases and tutorial news and solutions.
We care about your data in our privacy policy.
ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.
Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides