Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork100
Quickly dump and load databases
License
spatie/laravel-db-snapshots
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
This package provides Artisan commands to quickly dump and load databases in a Laravel application.
# Create a dumpphp artisan snapshot:create my-first-dump# Make some changes to your db# ...# Create another dumpphp artisan snapshot:create my-second-dump# Load up the first dumpphp artisan snapshot:load my-first-dump# Load up the latest dumpphp artisan snapshot:load --latest# List all snapshotsphp artisan snapshot:list# Remove old snapshots. Keeping only the most recentphp artisan snapshot:cleanup --keep=2
This package supports MySQL, PostgreSQL and SQLite.
We invest a lot of resources into creatingbest in class open source packages. You can support us bybuying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address onour contact page. We publish all received postcards onour virtual postcard wall.
We invest a lot of resources into creatingbest in class open source packages. You can support us bybuying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address onour contact page. We publish all received postcards onour virtual postcard wall.
For PHP 7.x and/or Laravel 6.x, use v1.x of this package.
You can install the package via Composer:
composer require spatie/laravel-db-snapshots
You should add a disk namedsnapshots toconfig/filesystems.php on which the snapshots will be saved. This would be a typical configuration:
// ...'disks' => [// ...'snapshots' => ['driver' =>'local','root' =>database_path('snapshots'), ],// ...
Optionally, you may publish the configuration file with:
php artisan vendor:publish --provider="Spatie\DbSnapshots\DbSnapshotsServiceProvider"This is the content of the published file:
return [/* * The name of the disk on which the snapshots are stored. */'disk' =>'snapshots',/* * The connection to be used to create snapshots. Set this to null * to use the default configured in `config/databases.php` */'default_connection' =>null,/* * The directory where temporary files will be stored. */'temporary_directory_path' =>storage_path('app/laravel-db-snapshots/temp'),/* * Create dump files that are gzipped */'compress' =>false,/* * Only these tables will be included in the snapshot. Set to `null` to include all tables. * * Default: `null` */'tables' =>null,/* * All tables will be included in the snapshot expect this tables. Set to `null` to include all tables. * * Default: `null` */'exclude' =>null,];
To create a snapshot (which is just a dump from the database) run:
php artisan snapshot:create my-first-dump
Giving your snapshot a name is optional. If you don't pass a name the current date time will be used:
# Creates a snapshot named something like `2017-03-17 14:31`php artisan snapshot:createIf you pass a connection but do not declare a name for the snapshot, the connection will be prepended
# Creates a snapshot named something like `logging_2017-03-17 14:31`php artisan snapshot:create --connection=loggingMaybe you only want to snapshot a couple of tables. You can do this by passing the--table multiple times or as a comma separated list:
# Both commands create a snapshot containing only the posts and users tables:php artisan snapshot:create --table=posts,usersphp artisan snapshot:create --table=posts --table=usersYou may want to exclude some tables from snapshot. You can do this by passing the--exclude multiple times or as a comma separated list:
# create snapshot from all tables excluding the users and postsphp artisan snapshot:create --exclude=posts,usersphp artisan snapshot:create --exclude=posts --exclude=usersNote: if you pass
--tableand--excludein the same time it will use--tableto create the snapshot and it's ignore the--exclude
When creating snapshots, you can optionally create compressed snapshots. To do this either pass the--compress option on the command line, or set thedb-snapshots.compress configuration option totrue:
# Creates a snapshot named my-compressed-dump.sql.gzphp artisan snapshot:create my-compressed-dump --compressAfter you've made some changes to the database you can create another snapshot:
php artisan snapshot:create my-second-dump
To load a previous dump issue this command:
php artisan snapshot:load my-first-dump
To load a previous dump to another DB connection:
php artisan snapshot:load my-first-dump --connection=connectionName
By default,snapshot:load will drop all existing tables in the database. If you don't want this behaviour, you can pass the--drop-tables=0 option:
php artisan snapshot:load my-first-dump --drop-tables=0
By default,snapshot:load will load the entire snapshot into memory which may cause problems when using large files. To avoid this, you can pass the--stream option to stream the snapshot to the database one statement at a time:
php artisan snapshot:load my-first-dump --stream
To list all the dumps run:
php artisan snapshot:list
A dump can be deleted with:
php artisan snapshot:delete my-first-dump
To remove all backups except the most recent 2
php artisan snapshot:cleanup --keep=2
If you need to pass extra options to the underlying db-dumper, add adump key to the databaseconnection with a key ofaddExtraOption and a value of the option. For example, to prevent the Postgres db dumper from setting the owner, you'd add:
'dump' => [ 'addExtraOption' => '--no-owner',],To thepgsql connection indatabase.php
There are several events fired which can be used to perform some logic of your own:
Spatie\DbSnapshots\Events\CreatingSnapshot: will be fired before a snapshot is createdSpatie\DbSnapshots\Events\CreatedSnapshot: will be fired after a snapshot has been createdSpatie\DbSnapshots\Events\LoadingSnapshot: will be fired before a snapshot is loadedSpatie\DbSnapshots\Events\LoadedSnapshot: will be fired after a snapshot has been loadedSpatie\DbSnapshots\Events\DeletingSnapshot: will be fired before a snapshot is deletedSpatie\DbSnapshots\Events\DeletedSnapshot: will be fired after a snapshot has been deleted
composertestPlease seeCHANGELOG for more information on what has changed recently.
Please seeCONTRIBUTING for details.
Please reviewour security policy on how to report security vulnerabilities.
The MIT License (MIT). Please seeLicense File for more information.
About
Quickly dump and load databases
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.
