How to Secure Your Laravel App with Automated Backups Using Spatie
This tutorial walks you through installing the Spatie Laravel‑Backup package, configuring email notifications, setting filename prefixes, limiting storage usage, enabling compression, monitoring backups, and automating the process with cron to protect your Laravel application from data loss.
Regular data backups are essential for any web application. This guide shows how to use the open‑source Spatie Laravel‑Backup library to create reliable backups for a Laravel project.
Step 1: Install the package
Open a terminal in your Laravel project directory and run:
composer require spatie/laravel-backupStep 2: Publish the configuration
Publish the default config file:
php artisan vendor:publish -provider="Spatie\Backup\BackupServiceProvider"Then edit config/backup.php with your preferred IDE.
Step 3: Configure email notifications
Set the recipient address for backup success/failure notifications:
'mail' => [
'to' => '[email protected]',
]If you do not need email alerts, leave the value empty.
Step 4: Optional filename prefix
Add a prefix to backup files if desired:
'filename_prefix' => 'backup-'Step 5: Define the application name
Make sure .env contains a meaningful APP_NAME value; backups will be stored in /storage/app/<APP_NAME>/backup-YYYY-MM-DD-HH-MM-SS.zip.
Step 6: Limit backup storage size
The default limit is 5 GB, which is often excessive. A recommended setting is:
'deleteOldestBackupsWhenUsingMoreMegabytesThan' => 800,Step 7: Enable compression
To reduce cloud storage usage, change the compressor setting:
'database_dump_compressor' => Spatie\DbDumper\Compressors\GzipCompressor::class,After enabling compression, the backup size drops significantly.
Step 8: Monitor backups
The package provides a command to check backup health: php artisan backup:monitor Running the command shows whether backups are recent and within size limits.
Step 9: Automate with cron
Schedule the backup command to run nightly using cron, so you never have to remember to back up manually.
Conclusion
Using Spatie’s Laravel‑Backup library saves time and money by ensuring your data is safely backed up and easily recoverable, eliminating excuses for neglecting backups.
GitHub repository: https://github.com/spatie/laravel-backup
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
