spatie/laravel-db-snapshots
Artisan commands to quickly create, load, list, and clean up database snapshots in Laravel. Dump and restore MySQL, PostgreSQL, and SQLite databases with named or latest snapshots—ideal for local/dev workflows and fast resets.
composer require spatie/laravel-db-snapshots
config/filesystems.php:
'snapshots' => [
'driver' => 'local',
'root' => database_path('snapshots'),
],
php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider"
Create a baseline database state for testing or rollback:
php artisan snapshot:create "initial-state"
Verify the snapshot exists:
php artisan snapshot:list
php artisan snapshot:create "feature-x-start"
php artisan snapshot:load "feature-x-start"
php artisan snapshot:cleanup --keep=3
php artisan snapshot:create "test-run-$(date +%s)"
Target specific tables (e.g., for large databases):
php artisan snapshot:create "users-only" --table=users,roles
Useful for testing migrations across environments:
php artisan snapshot:create "staging-db" --connection=staging
Listen for snapshot events to trigger custom logic (e.g., notifications):
// In EventServiceProvider
protected $listen = [
\Spatie\DbSnapshots\Events\CreatedSnapshot::class => [
\App\Listeners\LogSnapshotCreation::class,
],
];
Large Snapshots:
--stream for loading large files to avoid memory issues:
php artisan snapshot:load "big-dump" --stream
php artisan snapshot:create "compressed" --compress
Table Exclusions:
--exclude is ignored if --table is specified. Use --table for inclusion-only logic.Connection Conflicts:
config/database.php before loading snapshots.PostgreSQL Ownership:
dump.addExtraOption to skip owner settings:
'pgsql' => [
'dump' => ['addExtraOption' => '--no-owner'],
],
.sql file in storage/app/snapshots.snapshots disk is writable.Custom Dump Logic:
Override the dumper by binding a custom Spatie\DbSnapshots\Dumpers\Dumper implementation.
Pre/Post-Load Hooks:
Use events (LoadingSnapshot, LoadedSnapshot) to validate or transform data before loading.
Remote Storage:
Extend the snapshots disk to use S3 or other cloud storage for shared snapshots.
php artisan snapshot:create "feature-x-$(git rev-parse --short HEAD)"
snapshot:cleanup in Laravel’s app/Console/Kernel.php:
protected function schedule(Schedule $schedule) {
$schedule->command('snapshot:cleanup --keep=5')->daily();
}
exclude config to skip migrations, failed_jobs, etc.:
'exclude' => ['migrations', 'failed_jobs'],
How can I help you explore Laravel packages today?