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.
composer require spatie/laravel-db-snapshots
snapshots disk in config/filesystems.php:
'snapshots' => [
'driver' => 'local',
'root' => database_path('snapshots'),
],
php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider"
Create your first snapshot:
php artisan snapshot:create "initial-state"
Verify it exists:
php artisan snapshot:list
php artisan snapshot:create "before-feature-x"
php artisan snapshot:create "after-feature-x"
php artisan snapshot:load "before-feature-x"
php artisan snapshot:create "ci-test-state-$(date +%s)"
php artisan snapshot:cleanup --keep=1
php artisan snapshot:create "users-only" --table=users,roles
php artisan snapshot:create "exclude-temp" --exclude=temp_data
Configure database.php for custom dump options (e.g., PostgreSQL):
'pgsql' => [
'driver' => 'pgsql',
'dump' => [
'addExtraOption' => '--no-owner',
],
],
Listen to snapshot events in EventServiceProvider:
protected $listen = [
\Spatie\DbSnapshots\Events\CreatingSnapshot::class => [
\App\Listeners\LogSnapshotCreation::class,
],
];
Large snapshots:
--stream for loading large files:
php artisan snapshot:load "big-snapshot" --stream
--drop-tables=0 for incremental loads.Connection mismatches:
--connection when loading:
php artisan snapshot:load "snapshot-name" --connection=staging
Compression quirks:
.gz files are handled correctly in CI (e.g., GitHub Actions):
php artisan snapshot:load "compressed-snapshot" --stream
chmod -R 775 database_path('snapshots')
php artisan snapshot:load "debug-snapshot" --stream --verbose
Custom snapshot logic:
Extend Spatie\DbSnapshots\Commands\CreateSnapshotCommand for pre/post hooks.
Custom storage:
Implement Spatie\DbSnapshots\Contracts\SnapshotRepository for S3/remote storage.
Database-specific tweaks:
Override Spatie\DbSnapshots\Dumpers\MysqlDumper for custom SQL generation.
default_connection in config/db-snapshots.php to avoid ambiguity.--table overrides --exclude; use --exclude only when no --table is specified.compress: true in config for storage savings, but benchmark load times.--table to reduce snapshot size for CI environments.php artisan snapshot:cleanup --keep=5
How can I help you explore Laravel packages today?