wnx/laravel-backup-restore
Restore database backups created by spatie/laravel-backup. Adds an interactive php artisan backup:restore command to pick a backup and optionally decrypt it, then run configurable post-restore health checks to validate the restored DB.
Installation:
composer require wnx/laravel-backup-restore
php artisan vendor:publish --tag="backup-restore-config"
Ensure spatie/laravel-backup is installed (this package depends on it).
First Use Case: Restore the latest backup interactively:
php artisan backup:restore
APP_NAME folder).config/backup.php password by default).--keep is used).config/backup.php (defines disks, connections, encryption).config/laravel-backup-restore.php (customize post-restore validation).php artisan backup:restore --help for flags like --disk, --backup=latest, or --no-interaction.Backup Selection:
--backup=latest for automation (e.g., cron jobs).--backup=app_backup_2024-01-01.sql.gz).php artisan backup:restore --backup=app_backup_2024-01-01 --disk=s3
Database Handling:
--reset (drops all tables).--keep for debugging:php artisan backup:restore --keep --password=mysecret
Non-Interactive Mode:
php artisan backup:restore --no-interaction --connection=mysql --password=mysecret
Health Checks:
Extend \Wnx\LaravelBackupRestore\HealthChecks\HealthCheck to validate critical data post-restore (e.g., user counts, schema integrity).
// app/HealthChecks/CriticalDataCheck.php
public function run(PendingRestore $pendingRestore): Result {
return Result::make($this)
->okUnless(\App\Models\User::count() < 10, 'Missing users!');
}
Register in config/laravel-backup-restore.php:
'health-checks' => [
\App\HealthChecks\CriticalDataCheck::class,
],
GitHub Actions:
Use the provided workflow template to validate backups nightly. Store secrets (e.g., BACKUP_ARCHIVE_PASSWORD) in GitHub.
Multi-Environment:
Override APP_NAME in .env to match backup folders (e.g., APP_NAME=staging_backup).
GTID Errors (MySQL):
ERROR 3546: @@GLOBAL.GTID_PURGED cannot be changed.config/backup.php to skip GTID:
'dump' => [
'add_extra_option' => '--set-gtid-purged=OFF',
],
Missing Backups:
NoDatabaseDumpsFound exception.--keep output or logs for the db-dumps directory path.APP_NAME matches the backup folder name.Encryption Issues:
BACKUP_ARCHIVE_PASSWORD in .env or CLI --password.--keep to inspect the decrypted folder (storage/app/backup-restore).Verbose Output:
Add --verbose to see download/restore steps:
php artisan backup:restore --verbose --backup=latest
Dry Runs:
Use --keep to inspect downloaded files before restoring.
Health Check Failures: Check the command output for failed assertions. Extend checks for granular validation.
Custom Backup Paths:
Override Wnx\LaravelBackupRestore\BackupFinder to scan non-standard locations.
Post-Restore Hooks:
Use Laravel’s restored event (if supported) or extend the command class to run scripts after health checks.
Compression Support:
The package supports .sql.gz and .bz2 out-of-the-box. For .zip, extend the Decompressor interface.
config/backup.php if --disk is omitted.config/backup.php if --connection is omitted.APP_NAME=myapp_backup).How can I help you explore Laravel packages today?