Laravel DB Snapshots is a package that allows you to create and manage snapshots (backups) of your database. This can be especially useful for maintaining backup copies of your database, restoring it to a previous state, or cloning it for testing and development environments.
Key Features:
- Create Snapshots: Quickly take snapshots of your database.
- Restore Snapshots: Restore the database to a previous state using a saved snapshot.
- Disk Storage: Store database snapshots on different disks (e.g., local, s3, etc.).
- Cross-Environment Usage: Easily clone or copy databases between environments (e.g., production to staging).
- Multiple Databases: Supports taking snapshots of multiple databases.
- Easy Integration: Simple Artisan commands for creating and restoring snapshots.
Installation
- Install the package via Composer:
bash
1composer require spatie/laravel-db-snapshots - Publish the configuration file:
bash
1php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider" --tag="config" - Ensure you have proper disk storage configured in your
config/filesystems.php
for saving your snapshots. You can store the snapshots on local storage, S3, or other configured disks.
Usage
Once the package is installed, you can use Artisan commands to create, list, and restore snapshots.
1. Create a Snapshot:
To create a snapshot of your database, run the following command:
1 |
php artisan snapshot:create my_snapshot |
This will create a snapshot with the name my_snapshot
. You can specify where to save the snapshot by choosing a disk:
1 |
php artisan snapshot:create my_snapshot --disk=s3 |
2. List All Snapshots:
To list all available snapshots, use the following command:
1 |
php artisan snapshot:list |
3. Restore a Snapshot:
To restore a specific snapshot, use the command below:
1 |
php artisan snapshot:restore my_snapshot |
This will restore the database using the snapshot named my_snapshot
. If the snapshot is stored on a specific disk, you can specify it:
1 |
php artisan snapshot:restore my_snapshot --disk=s3 |
4. Delete a Snapshot:
To delete a snapshot, run:
1 |
php artisan snapshot:delete my_snapshot |
This will remove the snapshot from the storage disk.
Configuration
In the config/db-snapshots.php
file, you can customize settings such as:
- Disk: Specify where to store the snapshots (
local
,s3
, etc.). - Database Connection: Define which database connection to use for snapshots (MySQL, SQLite, etc.).
- Compression: Enable compression for snapshots to save storage space.
Example configuration in db-snapshots.php
:
1 2 3 4 5 6 7 |
return [ 'disk' => env('DB_SNAPSHOTS_DISK', 'local'), 'compress' => false, 'connection' => env('DB_SNAPSHOTS_CONNECTION', 'mysql'), ]; |
Scheduling Snapshots
You can automate snapshot creation by scheduling it in your App\Console\Kernel.php
file:
1 2 3 4 |
protected function schedule(Schedule $schedule) { $schedule->command('snapshot:create daily_snapshot')->daily(); } |
This will automatically create a snapshot every day.
Restoring Snapshots to a Different Database
You can also restore snapshots to a different database connection by specifying the connection:
1 |
php artisan snapshot:restore my_snapshot --connection=mysql_other |
This is useful when moving data between environments, such as from production to staging.
Conclusion
Laravel DB Snapshots simplifies the process of creating and managing database backups. It provides an easy way to snapshot your database, store it securely on various disks, and restore it when needed. This is an excellent tool for ensuring data safety, facilitating smooth deployments, and providing a reliable fallback mechanism in case of issues.