spatie/laravel-db-snapshots
Artisan commands to quickly create, load, list, and clean up database snapshots in Laravel. Dump your DB, restore any snapshot (or the latest), and keep only recent dumps. Supports MySQL, PostgreSQL, and SQLite.
composer require spatie/laravel-db-snapshots
config/filesystems.php:
'disks' => [
'snapshots' => [
'driver' => 'local',
'root' => database_path('snapshots'),
],
],
php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider"
# Create a snapshot with default timestamp naming
php artisan snapshot:create
# Or explicitly name it
php artisan snapshot:create production-baseline
# List all snapshots
php artisan snapshot:list
php artisan snapshot:create feature-x-baseline
php artisan snapshot:load feature-x-baseline
php artisan snapshot:create feature-x-v2
# Include only specific tables
php artisan snapshot:create --table=users,posts
# Exclude tables
php artisan snapshot:create --exclude=logs,migrations
# Use different connections
php artisan snapshot:create --connection=staging
# Load into a specific connection
php artisan snapshot:load staging-backup --connection=production
# Keep only the last 5 snapshots
php artisan snapshot:cleanup --keep=5
# Example GitHub Actions workflow
jobs:
test:
steps:
- name: Load baseline snapshot
run: php artisan snapshot:load baseline
- name: Run tests
run: php artisan test
- name: Create snapshot after tests
run: php artisan snapshot:create test-run-$(date +%s)
// Listen for snapshot creation
DbSnapshots::creating(function (CreatingSnapshot $event) {
Log::info('Creating snapshot: '.$event->name);
});
// Post-load actions
DbSnapshots::loaded(function (LoadedSnapshot $event) {
Cache::clear();
});
Connection Mismatch:
snapshot:load matches the source connection of the snapshot.--connection flag or verify config/database.php.Large File Handling:
--stream flag:
php artisan snapshot:load big-snapshot --stream
Table Exclusions:
--exclude is ignored if --table is specified.'tables' => ['users', 'posts'], // Only these tables
'exclude' => ['logs'], // Exclude from above
Compression Issues:
--compress=false.Foreign Key Constraints:
php artisan snapshot:load --drop-tables=0 --stream
SET FOREIGN_KEY_CHECKS=0; -- Add this to your snapshot file
Inspect Snapshot Contents:
# View raw SQL (Linux/macOS)
zcat database/snapshots/my-snapshot.sql.gz | less
Check Disk Space:
snapshots disk has sufficient space (default: database_path('snapshots')).Event Debugging:
// Log event payloads
DbSnapshots::creating(fn($e) => Log::debug($e->payload));
Custom Temporary Directory:
'temporary_directory_path' => sys_get_temp_dir().'/laravel-snapshots',
Database-Specific Options:
// config/database.php
'connections' => [
'pgsql' => [
'dump' => [
'addExtraOption' => '--no-owner --no-privileges',
],
],
],
Environment-Specific Snapshots:
// config/db-snapshots.php
'disk' => env('DB_SNAPSHOT_DISK', 'snapshots'),
Custom Snapshot Storage:
Spatie\DbSnapshots\Contracts\SnapshotStorage to use S3, etc.Pre/Post Actions:
// app/Providers/AppServiceProvider.php
public function boot()
{
DbSnapshots::creating(fn($e) => $this->preSnapshotActions());
DbSnapshots::loaded(fn($e) => $this->postLoadActions());
}
Snapshot Naming Logic:
Spatie\DbSnapshots\SnapshotNameGenerator for custom naming.'compress' => true).--stream for snapshots >100MB.migrations, failed_jobs, and large logs tables.snapshots directory is writable by the web server.How can I help you explore Laravel packages today?