In this tutorials, you’ll learn how to take a database backup with Spatie in a Laravel application quickly and easily. This Laravel spatie example will show you how to use the spatie package to take database backups.
The Spatie eloquently assists you in backing up your Laravel application in minutes. It makes a zip file backup that contains everything of your application’s basic directory, files, and, most crucially, the database dump.
The Laravel Spatie has the advantage of allowing you to store laravel backups on whichever filesystem you wish for your laravel application.
Table of Content
- Install Laravel Project
- Configure Database Connection
- Install Laravel Spatie Package
- Register Service Provider
- Set Up Backup in Laravel
- Take Backup to Secure Laravel Data
1. Install Laravel Project
To build a new Laravel project from scratch, use the composer to install the app, open the command-line tool, and run the following command.
composer create-project laravel/laravel --prefer-dist laravel-demo-app
2. Configure Database Connection
You can use MAMP or XAMPP as a local web server by editing the
.env file with the database name, username, and password.
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=root DB_PASSWORD=
3. Install Laravel Spatie Package
This is the first stage, in which we’ll show you how to install the spatie package in your Laravel app using the composer command.
composer require spatie/laravel-backup
The PHP artisan command spatie sends a backup email after execution; you must specify the email address where you wish to receive backup information.
In the .
env file, put the email address.
4. Register Service Provider
In this stage, you’ll register the service provider, check for package service providers in the
config/app.php file, and carefully insert the BackupServiceProvider class.
'providers' => [ ... ... ... Spatie\Backup\BackupServiceProvider::class, ];
5. Set Up Backup in Laravel
To get started with Laravel Backup, make sure the config file is published to
Execute the specified command to accomplish this task.
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
Here is the file you can look at to see the backup configuration’s default contents.
<?php return [ 'backup' => [ 'name' => env('APP_NAME', 'laravel-backup'), 'source' => [ 'files' => [ 'include' => [ base_path(), ], 'exclude' => [ base_path('vendor'), base_path('node_modules'), ], 'follow_links' => false, 'ignore_unreadable_directories' => false, 'relative_path' => null, ], 'databases' => [ 'mysql', ], ], 'database_dump_compressor' => null, 'database_dump_file_extension' => '', 'destination' => [ 'filename_prefix' => '', 'disks' => [ 'local', ], ], 'temporary_directory' => storage_path('app/backup-temp'), 'password' => env('BACKUP_ARCHIVE_PASSWORD'), 'encryption' => 'default', ], 'notifications' => [ 'notifications' => [ \Spatie\Backup\Notifications\Notifications\BackupHasFailedNotification::class => ['mail'], \Spatie\Backup\Notifications\Notifications\UnhealthyBackupWasFoundNotification::class => ['mail'], \Spatie\Backup\Notifications\Notifications\CleanupHasFailedNotification::class => ['mail'], \Spatie\Backup\Notifications\Notifications\BackupWasSuccessfulNotification::class => ['mail'], \Spatie\Backup\Notifications\Notifications\HealthyBackupWasFoundNotification::class => ['mail'], \Spatie\Backup\Notifications\Notifications\CleanupWasSuccessfulNotification::class => ['mail'], ], 'notifiable' => \Spatie\Backup\Notifications\Notifiable::class, 'mail' => [ 'to' => 'email@example.com', 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'firstname.lastname@example.org'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], ], 'slack' => [ 'webhook_url' => '', 'channel' => null, 'username' => null, 'icon' => null, ], 'discord' => [ 'webhook_url' => '', 'username' => null, 'avatar_url' => null, ], ], 'monitor_backups' => [ [ 'name' => env('APP_NAME', 'laravel-backup'), 'disks' => ['local'], 'health_checks' => [ \Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumAgeInDays::class => 1, \Spatie\Backup\Tasks\Monitor\HealthChecks\MaximumStorageInMegabytes::class => 5000, ], ], ], 'cleanup' => [ 'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::class, 'default_strategy' => [ 'keep_all_backups_for_days' => 7, 'keep_daily_backups_for_days' => 16, 'keep_weekly_backups_for_weeks' => 8, 'keep_monthly_backups_for_months' => 4, 'keep_yearly_backups_for_years' => 2, 'delete_oldest_backups_when_using_more_megabytes_than' => 5000, ], ], ];
6. Take Backup to Secure Laravel Data
We’ve covered how to set up the laravel app, install the spatie plugin, and configure its basic settings; now it’s time to take a backup.
But first, let’s quickly delete Laravel’s configuration cache; use the command below to do so.
php artisan config:clear
Taking a backup is straightforward; be sure to use the following command.
php artisan backup:run
The backup of the laravel application is required because there is a high likelihood of an error during the app development process.
Fortunately, using the Spatie package, we discovered a unique approach to backup the laravel app and database; in this article, we learned how to install and set up laravel spatie for laravel backup from scratch.
I hope you will like the content and it will help you to learn Laravel 9 Database Backup with Spatie Tutorial Example
If you like this content, do share.