spatie/laravel-db-snapshots
Laravel package that adds Artisan commands to quickly create, load, list, and clean up database snapshots (dumps) for your app. Works with MySQL, PostgreSQL, and SQLite—ideal for resetting state in development or tests.
CreatingSnapshot, LoadedSnapshot, etc.) allow custom pre/post-processing (e.g., notifications, logging, or triggering sidecar services). This enables integration with monitoring tools (e.g., Datadog), workflow orchestration (e.g., GitHub Actions), or audit trails.--connection flag enables targeted snapshots (e.g., snapshot:create --connection=replica).--table/--exclude flags reduces snapshot size and speeds up operations, critical for large databases or partial state restoration.spatie/db-dumper (v4+).config/db-snapshots.php allow environment-specific overrides (e.g., compress: true in staging, false in dev).snapshot:create, snapshot:load) fit Laravel’s CLI ecosystem, enabling CI/CD automation (e.g., php artisan snapshot:create pre-prod-backup in a deploy script).local disk), enabling S3/Cloud storage for backups via config/filesystems.php.| Risk Area | Assessment | Mitigation |
|---|---|---|
| Large Database Performance | Streaming (--stream) and compression (--compress) mitigate memory/IO bottlenecks, but large tables may still cause timeouts. |
Benchmark with production-like data; use --stream for loads >1GB. Consider partial snapshots (e.g., --table=users,orders) or chunked loading (custom event listeners). |
| Schema Migrations | Snapshots do not include migrations. Loading a snapshot onto a mismatched schema (e.g., missing tables) will fail. | Enforce schema versioning (e.g., schema_migrations table) or use migration locks in CI/CD. |
| Concurrency | Not thread-safe. Concurrent snapshot:load operations may corrupt the database. |
Use database locks (e.g., DB::transaction() with lock_timeout) or queue delayed jobs for non-critical loads. |
| Cross-Environment Compatibility | Snapshots may fail if DBMS versions (e.g., PostgreSQL 12 → 15) or collations differ between environments. | Test snapshots across environments; use --no-owner (PostgreSQL) or --skip-triggers (MySQL) flags where needed. |
| Security | Snapshots contain plaintext data. Storing in cloud storage (e.g., S3) requires encryption. | Enable S3 server-side encryption or client-side encryption (e.g., Laravel Encryption). Restrict IAM permissions for snapshot storage. |
Use Case Prioritization:
snapshot:cleanup --keep=5).Environment Scope:
dev_2024-05-01.sql) or shared across environments?--connection or custom event listeners to filter data.)Performance SLAs:
snapshot:load in production? (Test with --stream and --compress.)Monitoring & Alerting:
Disaster Recovery:
php artisan migrate --seed && php artisan snapshot:create pre-migration).CreatedSnapshot to trigger webhooks (e.g., notify a backup service) or database observers.snapshot:load in PHPUnit/Pest tests to reset state (e.g., @beforeEach fn() => $this->loadSnapshot('fresh_db')).- name: Create snapshot before deploy
run: php artisan snapshot:create pre-deploy-$(date +%s)
snapshot:load in deployment scripts if health checks fail.snapshot_duration_seconds) via custom events.| Phase | Action | Tools/Commands |
|---|---|---|
| Assessment | Audit current backup strategy (e.g., mysqldump, custom scripts). Identify gaps (e.g., no selective table dumps, no compression). |
php artisan snapshot:list (post-install to verify compatibility). |
| Pilot | Test in non-production (e.g., staging) with: |
--table=users,orders).--compress) for large DBs.php artisan snapshot:create pilot-snapshot --compress; monitor performance. |
| Integration | Replace legacy backup scripts with Artisan commands. | Replace mysqldump cron jobs with php artisan schedule:run (if using Laravel Scheduler). |
| Automation | Embed snapshots in:temporary_directory_path to high-I/O storage.--stream for loads >500MB.snapshot:cleanup --keep=30 --daily). | php artisan snapshot:cleanup --dry-run to validate policies. || Component | Compatibility Notes | Workarounds |
|---|---|---|
| Laravel Versions | Supports Laravel 10–13 (and PHP 8.1+). | Use ^2.7 for Laravel 11/12; ^2.8 for Laravel 13. |
| Database Drivers | MySQL, PostgreSQL, SQLite, MariaDB. No SQL Server. | For SQL Server, consider spatie/db-dumper directly or a custom wrapper. |
| Storage Backends | Local, S3, FTP, etc. (via Laravel Filesystem). | Configure snapshots disk in config/filesystems.php. |
| Custom DB Configs | Supports `dump.addExtraOption |
How can I help you explore Laravel packages today?