Laravel Backup

repository·main·Indexed 27 days ago

https://github.com/spatie/laravel-backup

A backup solution for Laravel applications that automates the creation of zip files containing application files and database dumps (MySQL, PostgreSQL, SQLite, and Mongo). It supports multiple filesystems, health monitoring, automated cleanup via customizable strategies, and archive encryption. Requires PHP 8.4 and Laravel 12.0 or higher for the latest version, with legacy support for versions 3 through 6.

Tokens
13.6K
Snippets
35
Records
90
Agent score
87%

What's inside spatie/laravel-backup

  1. Overview of spatie/laravel-backup

    main

    spatie/laravel-backup is a Laravel package that creates application backups as zip files. Each backup contains specified application directories and a database dump.

    Key features include:

    • Multi-filesystem support: Backups can be stored on any Laravel-configured filesystem and can be sent to multiple filesystems simultaneously.
    • Notifications: Integration with Mail, Slack, or any Laravel notification provider to alert you if a backup fails.
    • Maintenance: Capabilities to clean up old backups and monitor backup health.
    • Overview: Tools to view an overview of all existing backups.
  2. Overview of Laravel Backup

    main

    Laravel Backup is a package that creates a zip file backup of your application. The backup includes a dump of your database and all files in specified directories. Backups can be stored on any filesystem configured in your Laravel application, and you can configure the package to back up to multiple filesystems simultaneously.

    Key features include:

    • Backup Execution: Easily create backups via Artisan commands.
    • Monitoring: A backup monitor to check the health of your backups.
    • Notifications: Support for multiple notification channels when backup issues occur.
    • Cleanup: Automatic cleanup of old backups to manage disk space.
  3. Run a new backup

    main

    To perform a new backup, execute the backup:run Artisan command. This creates a .zip file containing the directories you have specified in your configuration and a database dump (supporting MySQL, PostgreSQL, SQLite, and Mongo). If configured, the resulting .zip file will be automatically copied to your configured Laravel filesystems.

    php artisan backup:run
  4. Create a custom backup health check

    main

    You can implement custom logic to monitor the health of your backups by extending the Spatie\Backup\Tasks\Monitor\HealthCheck class.

    To create a check, you must implement the abstract checkHealth method, which receives a BackupDestination instance. If the health check fails, you must throw a Spatie\Backup\Exceptions\InvalidHealthCheck exception.

    The base class provides three helper methods to simplify throwing this exception:

    • fail($message): Throws the exception with the provided message.
    • failIf(bool $condition, string $message): Throws the exception if the condition is true.
    • failUnless(bool $condition, string $message): Throws the exception if the condition is false.
  5. Monitor backup health

    main

    The package provides health monitoring for your application's backups. A backup is flagged as unhealthy if:

    • The date of the last successful backup is too far in the past.
    • The backup file size becomes too large.

    You can also monitor backups belonging to other applications through this system.

  6. Schedule backup, cleanup, and monitoring tasks

    main

    To automate your backup lifecycle, add the following commands to your Laravel scheduler in routes/console.php or app/Console/Kernel.php:

    use Illuminate\Support\Facades\Schedule;
    
    // Clean up old backups daily at 01:00
    Schedule::command('backup:clean')->daily()->at('01:00');
    
    // Run the backup daily at 01:30
    Schedule::command('backup:run')->daily()->at('01:30');
    
    // Monitor backup health daily at 10:00
    Schedule::command('backup:monitor')->daily()->at('10:00');
    use Illuminate\Support\Facades\Schedule;
    
    Schedule::command('backup:clean')->daily()->at('01:00');
    Schedule::command('backup:run')->daily()->at('01:30');
    Schedule::command('backup:monitor')->daily()->at('10:00');
  7. Install and schedule the backup monitor

    main

    To monitor the health of backups across multiple applications, it is recommended to set up a dedicated Laravel installation (preferably on a separate server) using a central storage disk like S3.

    Instead of scheduling backup:run and backup:clean on the monitoring application, you should schedule the backup:monitor command. You can still schedule the standard backup commands if you wish to back up the monitoring application itself.

    // routes/console.php
    
    use Illuminate\\\Support\\\Facades\\\Schedule;
    
    Schedule::command('backup:monitor')->daily()->at('03:00');
  8. Migrate from v9 to v10

    main

    When upgrading from version 9 to version 10, several breaking changes must be addressed in your configuration, event listeners, and custom classes.

    Encryption Configuration

    In config/backup.php, if the encryption key was previously set to null or false, you must change it to 'none'. Supported values are now: 'none', 'default', 'aes128', 'aes192', and 'aes256'.

  9. Encrypt backup archives using environment variables

    main

    To prevent unauthorized access to your backups, you can enable client-side symmetric zip file password encryption. The simplest way to do this is by defining the BACKUP_ARCHIVE_PASSWORD environment variable in your .env file.

    Encryption is applied while the archive is being built. It only runs if a password is set and the encryption algorithm is not set to 'none'.