In this tutorial, you will discover how to quickly and effortlessly take database backup with Spatie in Laravel application. This Laravel spatie example will show you the profound method of taking database backup using the spatie package.
The Spatie eloquently helps you take the Laravel application backup in minutes. It creates a zip file backup, which holds all the primitive directory, files and most importantly, the database dump of your application.
The great thing about the Laravel Spatie is that you can store laravel backup on any filesystems you want for your laravel application.
How to Backup Laravel 9 App with Database Dump using Spatie
- Step 1: Install Laravel Project
- Step 2: Configure Database Connection
- Step 3: Install Laravel Spatie Package
- Step 4: Register Service Provider
- Step 5: Set Up Backup in Laravel
- Step 6: Take Backup to Secure Laravel Data
Install Laravel Project
The composer provides handy way to install laravel app, open command-line tool and run the following command to create a fresh Laravel project from scratch.
composer create-project laravel/laravel --prefer-dist laravel-demo-app
Move into laravel app.
cd laravel-demo-app
Configure Database Connection
You can use MAMP or XAMPP as a local web server, insert the database name, username and password in the .env file.
DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=
If you are using MAMP local server in macOs; make sure to append UNIX_SOCKET and DB_SOCKET below database credentials in .env file.
UNIX_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
DB_SOCKET=/Applications/MAMP/tmp/mysql/mysql.sock
Install Laravel Spatie Package
This is the primary step, wherein we will show you how to use the composer command to install the spatie package in the laravel app.
composer require spatie/laravel-backup
After execution, the PHP artisan command spatie sends the mail regarding backup; you have to define the email where you want to receive info about the backup.
Add the mail address in the .env file.
MAIL_FROM_ADDRESS=demo@gmail.com
MAIL_FROM_NAME="${APP_NAME}"
Register Service Provider
In this step, you will register the service provider, get inside the config/app.php file, look for package service providers and carefully insert the BackupServiceProvider class.
'providers' => [
...
...
...
SpatieBackupBackupServiceProvider::class,
];
Set Up Backup in Laravel
To begin with Backup in Laravel, make sure to publish the config file to config/laravel-backup.php.
To complete this task, execute the given command.
php artisan vendor:publish --provider="SpatieBackupBackupServiceProvider"
Here is the file that you can check to view the default contents of the backup configuration.
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' => [
SpatieBackupNotificationsNotificationsBackupHasFailedNotification::class => ['mail'],
SpatieBackupNotificationsNotificationsUnhealthyBackupWasFoundNotification::class => ['mail'],
SpatieBackupNotificationsNotificationsCleanupHasFailedNotification::class => ['mail'],
SpatieBackupNotificationsNotificationsBackupWasSuccessfulNotification::class => ['mail'],
SpatieBackupNotificationsNotificationsHealthyBackupWasFoundNotification::class => ['mail'],
SpatieBackupNotificationsNotificationsCleanupWasSuccessfulNotification::class => ['mail'],
],
'notifiable' => SpatieBackupNotificationsNotifiable::class,
'mail' => [
'to' => 'your@example.com',
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'),
'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' => [
SpatieBackupTasksMonitorHealthChecksMaximumAgeInDays::class => 1,
SpatieBackupTasksMonitorHealthChecksMaximumStorageInMegabytes::class => 5000,
],
],
],
'cleanup' => [
'strategy' => SpatieBackupTasksCleanupStrategiesDefaultStrategy::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,
],
],
];
Take Backup to Secure Laravel Data
We have explained how to set up the laravel app, install the spatie plugin and its basic configurations; now, we are ready to take backup.
But before that, let us swiftly reset laravel’s configuration cache; you may use the below command to clear the config cache.
php artisan config:clear
It is simple to take backup; make sure to execute the following command.
php artisan backup:run
Conclusion
The backup of the laravel application is necessary; there is a high possibility of a mishap happening in the app development process.
Luckily, we found a unique way to backup the laravel app and the laravel app database using the Spatie package; in this tutorial, we learned how to install and set up laravel spatie for laravel backup from scratch.
We assume this quick example will help you set up the laravel backup on the fly.
