Restore Database Backups in Laravel
Published on by Paul Redmond
The Laravel Backup Restore is a package to restore database backups made with Spatie's laravel-backup package:
Adding the finishing touches on laravel-backup-restore.
— Stefan Zweifel (@_stefanzweifel) May 31, 2023
Currently writing a blog post explaining everything.
v1 should be tagged by mid June.https://t.co/uRubDv4CDw
This package provides an Artisan command to restore a backup as well as some customizable health checks and backup integrity checks. Here's an example of the artisan command from the project's README:
php artisan backup:restore --disk=s3 --backup=latest --connection=mysql --password=my-secret-password --reset
After the backup is restored, this package will run some health checks to make sure the backup was restored successfully. It checks for things like making sure the database has tables. You can add your own custom checks as well that you can configure to run after a backup:restore:
namespace App\HealthChecks; use Wnx\LaravelBackupRestore\PendingRestore;use Wnx\LaravelBackupRestore\HealthChecks\HealthCheck; class MyCustomHealthCheck extends HealthCheck{ public function run(PendingRestore $pendingRestore): Result { $result = Result::make($this); // We assume that your app generates sales every day. // This check ensures that the database contains sales from yesterday. $newSales = \App\Models\Sale::query() ->whereBetween('created_at', [ now()->subDay()->startOfDay(), now()->subDay()->endOfDay() ]) ->exists(); // If no sales were created yesterday, we consider the restore as failed. if ($newSales === false) { return $result->failed('Database contains no sales from yesterday.'); } return $result->ok(); }}
Be sure to check out the package's readme for an example of a GitHub action you can use to validate your backup integrity incrementally.
You can learn more about this package, get full installation instructions, and view the source code on GitHub.