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.

21CTO
21CTO
21CTO
How to Secure Your Laravel App with Automated Backups Using Spatie

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-backup

Step 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

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

automationPHPBackupLaravelSpatie
21CTO
Written by

21CTO

21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.