Laravel 12 with Spatie Laravel-Backup: A Complete Guide

Author

Kritim Yantra

Apr 16, 2025

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

  1. Introduction to Spatie Laravel-Backup
  2. Installation & Setup
  3. Configuring Backup Options
  4. Running Backups Manually
  5. Scheduling Automatic Backups
  6. Storing Backups on Cloud (S3, Dropbox)
  7. Restoring Backups
  8. Monitoring & Notifications
  9. Advanced Features & Tips
  10. Conclusion

1. Introduction to Spatie Laravel-Backup

The Spatie Laravel-Backup package allows you to:

  • Backup databases (MySQL, PostgreSQL, SQLite, MongoDB).
  • Backup files (entire storage directory 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

  1. Install the AWS SDK:
    composer require league/flysystem-aws-s3-v3
    
  2. Update .env:
    AWS_ACCESS_KEY_ID=your-key
    AWS_SECRET_ACCESS_KEY=your-secret
    AWS_DEFAULT_REGION=us-east-1
    AWS_BUCKET=your-bucket
    
  3. Configure config/backup.php:
    'destination' => [
        'disks' => ['s3'],
    ],
    

Using Dropbox

  1. Install Dropbox adapter:
    composer require spatie/flysystem-dropbox
    
  2. Configure in config/filesystems.php:
    'dropbox' => [
        'driver' => 'dropbox',
        'authorization_token' => env('DROPBOX_TOKEN'),
    ],
    
  3. Update config/backup.php:
    'disks' => ['dropbox'],
    

7. Restoring Backups

The package doesn’t include a built-in restore command, but you can manually:

  1. Download the backup (storage/app/backups or cloud).
  2. Extract files (if needed).
  3. Restore the database:
    mysql -u user -p database_name < backup.sql
    
  4. 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

  1. Install Slack/Discord adapter:
    composer require laravel/slack-notification-channel
    
  2. Configure in config/backup.php:
    'notifications' => [
        'slack' => [
            'webhook_url' => env('SLACK_WEBHOOK'),
        ],
    ],
    

9. Advanced Features & Tips

  • Encrypt Backups (Use spatie/laravel-db-snapshots for 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! 🚀

Tags

Laravel Php

Comments

No comments yet. Be the first to comment!

Please log in to post a comment:

Sign in with Google

Related Posts