Kritim Yantra
Apr 16, 2025
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.
The Spatie Laravel-Backup package allows you to:
storage
directory or selected folders). If you don’t have Laravel 12 installed:
composer create-project laravel/laravel backup-demo
cd backup-demo
Install the package via Composer:
composer require spatie/laravel-backup
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
This creates config/backup.php
.
Edit config/backup.php
to customize:
'backup' => [
'databases' => [
'mysql',
'pgsql',
],
],
'backup' => [
'source' => [
'files' => [
'include' => [base_path('storage')],
'exclude' => [base_path('storage/debugbar')],
],
],
],
'cleanup' => [
'default_strategy' => [
'keep_all_backups_for_days' => 7,
'keep_daily_backups_for_days' => 16,
],
],
This keeps:
Run a full backup (database + files):
php artisan backup:run
Output:
Backup completed!
php artisan backup:run --only-db
php artisan backup:run --only-files
php artisan backup:list
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.
To delete old backups automatically:
$schedule->command('backup:clean')->daily();
composer require league/flysystem-aws-s3-v3
.env
:AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=your-bucket
config/backup.php
:'destination' => [
'disks' => ['s3'],
],
composer require spatie/flysystem-dropbox
config/filesystems.php
:'dropbox' => [
'driver' => 'dropbox',
'authorization_token' => env('DROPBOX_TOKEN'),
],
config/backup.php
:'disks' => ['dropbox'],
The package doesn’t include a built-in restore command, but you can manually:
storage/app/backups
or cloud). mysql -u user -p database_name < backup.sql
storage
).Get alerts when backups fail.
Update config/backup.php
:
'notifications' => [
'mail' => [
'to' => 'admin@example.com',
],
],
composer require laravel/slack-notification-channel
config/backup.php
:'notifications' => [
'slack' => [
'webhook_url' => env('SLACK_WEBHOOK'),
],
],
spatie/laravel-db-snapshots
for encrypted DB dumps). 'exclude_tables' => ['sessions', 'cache'],
'disks' => ['local', 's3'],
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!
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google