The `spatie/laravel-backup` package is a popular and robust solution for Laravel applications to create backups of their files and databases. It offers a flexible way to store these backups on various filesystems, clean up old backups, and notify developers about the backup status.
Key features of `spatie/laravel-backup` include:
1. Backup Files and Databases: It can back up all files within your application's root directory (or specified directories) and the contents of your database(s) supported by Laravel's database drivers (MySQL, PostgreSQL, SQLite, SQL Server).
2. Multiple Destinations: Backups can be stored on any filesystem configured in `config/filesystems.php`, including local disk, Amazon S3, Dropbox, SFTP, FTP, and more. You can even configure multiple backup destinations for redundancy.
3. Backup Cleanup: It intelligently removes old backups based on a sophisticated strategy, ensuring you don't run out of disk space while still retaining enough recovery points. This strategy considers factors like daily, weekly, monthly, and yearly backups.
4. Health Checks: The package can perform health checks to ensure backups are running correctly, disk space is sufficient, and the last backup isn't too old. It can also notify you if a health check fails.
5. Notifications: You can configure various notification channels (e.g., email, Slack, Pushover) to receive alerts when a backup starts, finishes successfully, fails, or when a health check fails.
6. Scheduling: It integrates seamlessly with Laravel's task scheduler, allowing you to define a schedule (e.g., daily at 2 AM) for your backups to run automatically.
7. Customization: Highly configurable, allowing you to specify which files/directories to include or exclude, which database connections to back up, compression settings, and more.
To use `spatie/laravel-backup`, you typically install it via Composer, publish its configuration file, configure your backup destinations and settings in `config/backup.php`, and then schedule the backup commands in your application's `App\Console\Kernel.php`.
Example Code
```php
// 1. Install the package via Composer
// composer require spatie/laravel-backup
// 2. Publish the configuration file
// php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider" --tag="backup-config"
// 3. Configure your .env file for database and file storage
// Example for MySQL and S3 storage:
// DB_CONNECTION=mysql
// DB_HOST=127.0.0.1
// DB_PORT=3306
// DB_DATABASE=your_database
// DB_USERNAME=your_user
// DB_PASSWORD=your_password
// FILESYSTEM_DISK=s3
// AWS_ACCESS_KEY_ID=your_access_key
// AWS_SECRET_ACCESS_KEY=your_secret_key
// AWS_DEFAULT_REGION=your_region
// AWS_BUCKET=your_s3_bucket_name
// AWS_USE_PATH_STYLE_ENDPOINT=false
// 4. Example configuration in config/backup.php (after publishing)
// This is a snippet, the full file has more options.
return [
'backup' => [
'name' => env('APP_NAME', 'laravel-app'), // Name of the application (used in backup filenames)
'source' => [
'files' => [
'include' => [
base_path(), // Backup entire application directory
],
'exclude' => [
base_path('vendor'), // Exclude vendor directory
base_path('node_modules'), // Exclude node_modules
storage_path('app/backup-temp'), // Exclude temporary backup directory
],
'follow_links' => false,
'ignore_unreadable_directories' => false,
'relative_path' => null,
],
'databases' => [
'mysql',
// 'pgsql',
// You can specify multiple database connections defined in config/database.php
],
],
'destination' => [
'filename_prefix' => '',
'disks' => [
'local', // Store a copy on the local disk
's3', // Store a copy on Amazon S3 (configured in config/filesystems.php)
// Add more disks here as configured in config/filesystems.php
],
],
],
'cleanup' => [
// ... cleanup strategies ...
],
'notifications' => [
// ... notification channels ...
],
'health_checks' => [
// ... health check configuration ...
],
];
// 5. Schedule your backups in App\Console\Kernel.php
// protected function schedule(Schedule $schedule)
// {
// $schedule->command('backup:run')->daily()->at('02:00');
// $schedule->command('backup:clean')->daily()->at('02:30');
// // Optional: health checks
// $schedule->command('backup:monitor')->daily()->at('03:00');
// }
// Remember to add this cron job to your server to run Laravel's scheduler:
// * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
// 6. Manually run a backup (for testing or ad-hoc backups)
// php artisan backup:run
// 7. Manually clean up old backups
// php artisan backup:clean
// 8. Monitor backup health
// php artisan backup:monitor
```








spatie/laravel-backup