Laravel 12 with Spatie Laravel-Backup: A Complete Guide
In this blog post, we'll explore how to use the Spatie Laravel-Backup package in Laravel 12 to automate database and file backups, store them securely, and restore them when needed.
Table of Contents
- Introduction to Spatie Laravel-Backup
- Installation & Setup
- Configuring Backup Options
- Running Backups Manually
- Scheduling Automatic Backups
- Storing Backups on Cloud (S3, Dropbox)
- Restoring Backups
- Monitoring & Notifications
- Advanced Features & Tips
- Conclusion
1. Introduction to Spatie Laravel-Backup
The Spatie Laravel-Backup package allows you to:
- Backup databases (MySQL, PostgreSQL, SQLite, MongoDB).
- Backup files (entire
storagedirectory or selected folders). - Store backups locally or on cloud (S3, Dropbox, FTP).
- Schedule automatic backups.
- Restore backups easily.
- Get notifications (Email, Slack, Discord) on backup success/failure.
2. Installation & Setup
Step 1: Install Laravel 12
If you don’t have Laravel 12 installed:
composer create-project laravel/laravel backup-demo
cd backup-demo
Step 2: Install Spatie Laravel-Backup
Install the package via Composer:
composer require spatie/laravel-backup
Step 3: Publish Config File
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
This creates config/backup.php.
3. Configuring Backup Options
Edit config/backup.php to customize:
Select Databases to Backup
'backup' => [
'databases' => [
'mysql',
'pgsql',
],
],
Include/Exclude Files
'backup' => [
'source' => [
'files' => [
'include' => [base_path('storage')],
'exclude' => [base_path('storage/debugbar')],
],
],
],
Set Backup Retention Period
'cleanup' => [
'default_strategy' => [
'keep_all_backups_for_days' => 7,
'keep_daily_backups_for_days' => 16,
],
],
This keeps:
- All backups for 7 days.
- Daily backups for 16 days.
4. Running Backups Manually
Run a full backup (database + files):
php artisan backup:run
Output:
Backup completed!
Backup Only Database
php artisan backup:run --only-db
Backup Only Files
php artisan backup:run --only-files
List All Backups
php artisan backup:list
5. Scheduling Automatic Backups
Add to app/Console/Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('02:00');
}
This runs backups daily at 2 AM.
Run Cleanup
To delete old backups automatically:
$schedule->command('backup:clean')->daily();
6. Storing Backups on Cloud (S3, Dropbox)
Using Amazon S3
- Install the AWS SDK:
composer require league/flysystem-aws-s3-v3 - Update
.env:AWS_ACCESS_KEY_ID=your-key AWS_SECRET_ACCESS_KEY=your-secret AWS_DEFAULT_REGION=us-east-1 AWS_BUCKET=your-bucket - Configure
config/backup.php:'destination' => [ 'disks' => ['s3'], ],
Using Dropbox
- Install Dropbox adapter:
composer require spatie/flysystem-dropbox - Configure in
config/filesystems.php:'dropbox' => [ 'driver' => 'dropbox', 'authorization_token' => env('DROPBOX_TOKEN'), ], - Update
config/backup.php:'disks' => ['dropbox'],
7. Restoring Backups
The package doesn’t include a built-in restore command, but you can manually:
- Download the backup (
storage/app/backupsor cloud). - Extract files (if needed).
- Restore the database:
mysql -u user -p database_name < backup.sql - Restore files (copy to
storage).
8. Monitoring & Notifications
Get alerts when backups fail.
Email Notifications
Update config/backup.php:
'notifications' => [
'mail' => [
'to' => 'admin@example.com',
],
],
Slack/Discord Notifications
- Install Slack/Discord adapter:
composer require laravel/slack-notification-channel - Configure in
config/backup.php:'notifications' => [ 'slack' => [ 'webhook_url' => env('SLACK_WEBHOOK'), ], ],
9. Advanced Features & Tips
- Encrypt Backups (Use
spatie/laravel-db-snapshotsfor encrypted DB dumps). - Exclude Tables:
'exclude_tables' => ['sessions', 'cache'], - Backup to Multiple Disks:
'disks' => ['local', 's3'],
10. Conclusion
The Spatie Laravel-Backup package is essential for:
✅ Automating database & file backups.
✅ Storing securely on cloud (S3, Dropbox).
✅ Scheduling & cleaning up old backups.
✅ Getting failure alerts.
Start using it today to secure your Laravel 12 app!
Happy backing up! 🚀
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
🛠 Features✔ Google & GitHub Authentication✔ User Registration & Login✔ Profile/Dashboard Access✔ Secure & Mi...
Python Flask Blog Project with Admin Panel
Are you ready to launch a powerful, modern, and fully customizable blog? Look no further! Introducing the Flask-Blog Pro...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
🚀 Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.