Backup Laravel Application and Database Automatically
Summary: Set up automated backups using Laravel Spatie package.
Data loss and server failures can be disastrous to any web application. Laravel developers can prevent these disasters by setting up automatic backups for their application files and database. In this guide, we'll walk through configuring Laravel automatic backups using the popular Spatie Laravel Backup package.
Why Automated Backups are Essential
Backing up your Laravel application and database is crucial:
- Disaster Recovery: Instantly restore your application in case of accidental data loss or server failure.
- Peace of Mind: Focus on development without worrying about losing critical information.
- Compliance: Meet industry standards and data security requirements.
Prerequisites
- Laravel: This guide assumes you have a Laravel application set up.
- Composer: Dependency manager for PHP.
- Storage: Local disk or remote services like Amazon S3, Dropbox, etc.
- Database Credentials: Access to your Laravel database.
Step 1: Install Spatie Laravel Backup Package
Run the following composer command in your Laravel project root:
composer require spatie/laravel-backup
Step 2: Publish the Configuration
Publish the backup config file using the artisan command:
php artisan vendor:publish --provider="Spatie\Backup\BackupServiceProvider"
This creates config/backup.php
which controls backup settings.
Step 3: Configure backup.php
Open config/backup.php
and review these sections:
1. Source Files
'source' => [
'files' => [
'include' => [
base_path(), // Laravel root directory
],
'exclude' => [
base_path('vendor'),
base_path('node_modules'),
],
],
// ...
],
2. Database
By default, all configured databases will be included. For specific databases, adjust:
'database' => [
'connection' => [
'mysql', // or 'pgsql', 'sqlite', etc.
],
],
3. Destination
Set the disk(s) where backups should be stored:
'destination' => [
'disks' => [
'local', // or 's3', 'dropbox', etc.
],
],
If using a remote disk like S3, make sure to configure it in config/filesystems.php
.
4. Cleanup Strategy
Control how many backups to retain:
'cleanup' => [
'strategy' => \Spatie\Backup\Tasks\Cleanup\Strategies\DefaultStrategy::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,
],
],
Step 4: Test Your Backup
Run this command to take a test backup:
php artisan backup:run
You should see output confirming your application and DB have been successfully backed up. Check your configured disk (e.g., storage/app/backup
) to confirm the archive exists.
Step 5: Automate Backups via Scheduling
To enable automatic, daily backups, add the backup command to Laravel's scheduler.
Edit app/Console/Kernel.php
:
protected function schedule(Schedule $schedule)
{
$schedule->command('backup:run')->daily()->at('02:00');
}
This example runs the backup every day at 2:00 AM.
Don't forget to add the scheduler to your server's crontab:
* * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1
Step 6: Monitor and Notify (Optional)
Spatie provides notification support for backup failures via mail, Slack, or other channels.
In config/backup.php
, find the notifications
section and set up emails or Slack webhooks to get notified about backup success, failures, and health checks.
Step 7: Restore Backups
To restore from a backup, extract the files and database dump manually or automate via a simple script. Restoration is not yet automated by Spatie for security reasons, but it's straightforward:
- Unzip your backup archive
- Restore the database using
mysql
orpsql
- Sync your application files
Conclusion
With these steps, your Laravel application is now backed up automatically, protecting you against accidental data loss and server outages. The Spatie Laravel Backup package offers a powerful, flexible, and easy-to-set-up solution for secure, automated backups of both your application files and database.
Further Reading:
Stay safe, and keep those backups up-to-date!