How to implement Daily Database Backups in Laravel 11
Published: 2024-07-10 04:58:35

**Note: In laravel 11, kernel.php is no longer present and these are handled through the bootstrap/app.php file.**

Regular backups are essential for maintaining the integrity of your Laravel application and safeguarding against unexpected events like data loss, system failures, or malicious attacks. In this blog post, we will explore how to create a daily backup system for your Laravel 11 application using custom commands and Laravel’s Artisan command-line interface (CLI). We will also discuss the popular Laravel Backup package, which simplifies backup management.

Why Regular Backups Matter

Data loss can occur due to various reasons such as hardware failures, software bugs, or security breaches. Regular backups ensure that you can restore your application to its previous state, minimizing downtime and data loss. By automating the backup process, you can ensure consistency and reduce the risk of human error.

Using the Laravel Backup Package

The Laravel Backup package is a powerful tool that allows you to easily manage your application’s backups. It can back up your files and databases to various storage locations, including local disks, FTP, SFTP, Amazon S3, and more.

To get started with the Laravel Backup package in Laravel 11, follow these steps:

Step 1. Install the Laravel Backup Package

Use Composer to install the package:

composer global require laravel/installer

or

composer require spatie/laravel-backup

Step 2. Creating Custom Artisan Commands

php artisan make:command DatabaseBackup

Step 3. Defining the Custom Command

Open the generated command file and set the command signature and behavior. Here’s an example of how to define the command to back up the database:

and add this DatabaseBackup.php file

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

}

}

4. Scheduling the Backup Command

<?php

use Illuminate\Console\Scheduling\Schedule;

use Illuminate\Foundation\Application;

use Illuminate\Foundation\Configuration\Exceptions;

use Illuminate\Foundation\Configuration\Middleware;

return Application::configure(basePath: dirname(__DIR__))

->withRouting(

web: __DIR__.’/../routes/web.php’,

api: __DIR__.’/../routes/api.php’,

apiPrefix: ‘api’,

commands: __DIR__.’/../routes/console.php’,

channels: __DIR__.’/../routes/channels.php’,

health: ‘/up’,

)

->withMiddleware(function (Middleware $middleware) {

//

})

->withExceptions(function (Exceptions $exceptions) {

//

})

->withSchedule(function (Schedule $schedule) {

/**

* Define the application’s command schedule.

*/

$schedule->command(‘db:backup’)->daily();

})

->create();

In laravel 11, kernel.php is no longer present and these are handled through the bootstrap/app.php file.

Step 5: Run the Laravel scheduler.
php artisan db:backup run
this cmd for testing prupose

, and you can also see the backup file storage->app->backup folder

* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1

I hope it will help you.
Thank you
Arvind Kumar
[Egiz Solution](url)