In this blog post, we explore how to create a daily backup system for your Laravel application using custom commands. Backing up your data is essential for maintaining application integrity and protecting against unexpected events. We’ll show you how Laravel’s command-line interface simplifies this process.
We start by emphasizing the importance of regular backups and the risks of data loss. Then, we dive into the Laravel Backup package, a popular choice for backup management in Laravel projects.
We explain how to generate commands, set their signature, and define their behavior using the Laravel Artisan CLI.
Step 1: Install laravel application.
If you already have laravel application need to integrate daily backup then follow up by step2.
1 |
composer create-project laravel/laravel laravel-daily-backup |
Or you can create laravel project by using laravel alias
1 |
laravel new laravel-daily-backup |
Step 2: Create Command.
Firstly need to navigate root directory of your application then open terminal and then execute below command.
1 |
php artisan make:command DatabaseBackup |
after execute this command DatabaseBackup.php file will be created into app\Console\Commands\DatabaseBackup.php. This file looks like
For Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?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 = 'app:database-backup'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Execute the console command. */ public function handle() { // } } ?> |
Step 3: Logic of backuping database.
Now need to update its signature, description and logic of your backup into handle method.
In this below example command signature name is db:backup this signature name is used to execute the command as command name, like
1 |
php artisan db:backup |
and you can describe the description as you want.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/** * The name and signature of the console command. * * @var string */ protected $signature = 'db:backup'; /** * The console command description. * * @var string */ protected $description = 'Automating Daily Backups'; |
Here is an example of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php namespace App\Console\Commands; use Carbon\Carbon; use Illuminate\Console\Command; use Illuminate\Support\Facades\Storage; class DatabaseBackup extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'db:backup'; /** * The console command description. * * @var string */ protected $description = 'Automating Daily Backups'; /** * Execute the console command. */ public function handle() { if (! Storage::exists('backup')) { Storage::makeDirectory('backup'); } $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 4: Register Command.
Now we need to register this command into App\Console\Kernel.php file into schedule method, in below example of code there registry of your command.
1 2 3 4 5 6 7 |
/** * Define the application's command schedule. */ protected function schedule(Schedule $schedule): void { $schedule->command('db:backup')->daily(); } |
Here is an example of code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?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('db:backup')->daily(); } /** * Register the commands for the application. */ protected function commands(): void { $this->load(__DIR__.'/Commands'); require base_path('routes/console.php'); } } |
Step 5: Run the Laravel scheduler.
To activate the Laravel scheduler, you need to add an entry to your server’s crontab or use a task scheduler provided by your hosting environment. The following command runs the scheduler every minute:
1 |
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1 |
That`s all for implementing the Daily Database Backup. Let us know your thoughts in the comments below.
If you’re looking to build something amazing with Laravel, why not consider hire laravel developers who can bring your vision to life? You can also checkout our Bagisto Extensions.