In this tutorial we are going to learn how to Automatically Backup Database Daily Weekly Monthly Laravel 9.
Its important to take backup when you are working large amount of database. Its help to secure you data everyday, monthly, yearly as you set. That didn’t lost you data when you backup automatically.
Steps to Automatically Backup Database Daily Weekly Monthly Laravel 9
- Step 1: Install Laravel 9 Application
- Step 2: Creating Command
- Step 3: Creating Backup Folder
- Step 4: Schedule Command
- Step 5: Setup on Server
- Step 6: Conclusion
Also Read: Create Custom Helper in CodeIgniter (2022)
Step 1: Install Laravel 9 Application
First, We will start with installing a new Laravel 9 Application in our localhost. To install a Laravel just run the following command in terminal. Read the Laravel Document on how to install Laravel in other ways.
composer create-project laravel/laravel blog
“blog” is our Laravel Application name.
Step 2: Creating Command
So Now, We are going to create DatabaseBackUp command using artisan. To create DatabaseBackUp command run the following command in terminal:
php artisan make:command DatabaseBackUp
The above command will create a DatabaseBackUp.php file on console directory. Just update the code with below code.
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') . ".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);
}
}
Also Read: Laravel Load More Data on Scroll with jQuery and Ajax
Step 3: Creating Backup Folder
We need to create a backup folder where its going to store the backup database. So create a backup folder inside storage/app directory.
storage/app/backup
Make sure to give permissions the backup folder to store the backups.
Step 4: Schedule Command
We need to update the kernal file to schedule our created command. Just open app/Console/Kernel.php file and update the code as show below
app/Console/Kernel.php
<?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');
}
}
If you like to manually create the backups you can run the below command in terminal:
php artisan database:backup
This will create a backup file in you storage/app/backup folder you may check it.
Also Read: Upload Images to Server PHP Tutorial (2022)
Step 5: Setup on Server
If you want to setup the automatically backup of database you need to setup cron on your 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
Step 6: Conclusion
Today, We had learn Automatically Backup Database Daily Weekly Monthly Laravel 9. Hope this tutorial helped you with learning Laravel 9 Automatically Backup Database Daily Weekly Monthly. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.
Also Read: Create Livewire CRUD Application in Laravel 9 Example
[…] Also Read: Automatically Backup Database Laravel 9 […]
[…] Also Read: Automatically Backup Database Laravel 9 […]