jairocgr / datashot
A tool for taking partial database dumps and scripting datbases
Installs: 60
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/jairocgr/datashot
Requires
- php: >=7.0
- ext-pdo: *
- ext-zlib: *
- league/flysystem-aws-s3-v3: ^1
- league/flysystem-sftp: ^1
- symfony/console: ^5
- symfony/process: ^5
- vlucas/phpdotenv: ^5
Requires (Dev)
- phpunit/phpunit: ^7
README
A tool for taking partial and minified database snapshots for testing and development purpose.
Instead of taking a full database dump, you can filter which rows you want to dump in order to come up with a downsized database snapshot.
Requirements
To install and run datashot you must have:
- PHP >= 5.6 with PDO extension
zlibPHP extension for gzip compression- MySQL client (
mysqlandmysqldumpon path) - Gzip on path
- Composer dependency manager
Installing
Install it as a regular package via composer:
composer require jairocgr/datashot
Usage
You can call it as a command line tool:
php vendor/bin/datashot --help
Getting Started
With datashot you can filter which rows you want to dump to come up with a
much smaller database dump.
You can reduce ginormous multi-gigabyte databases in to a small gziped file ready to be restored in to staging and local develoment environments.
This kind of power come up handy for troubleshooting production bugs and all arround better development experience with real life data that best reflects your application usage than a mocked or seeded schema.
With datashot you can for instance take a database dump with only the orders
from the current quarter.
You can also perform other operations like:
- Fully
replicatedatabases from a host to another, restoreexistant dumps to a database hosts, and- upload/download dumps from a SFTP or S3 repositories via
cpcommand.
Configuration
The first step is to setup the datashot.config.php file in the root of your
application repository.
This is the configuration file where you set the database hosts, passwords and
the SQL WHERE clauses in order to slice the database down.
For a more complete and commented and configuration file see the sample
datashot.config.phpfile inside this repository root directory
Database Hosts
You have to configure all your database in order to be able to work with it.
return [ 'database_servers' => [ // A database server called 'live1' 'live1' => [ // Only mysql for now, maby postgres in the future 'driver' => 'mysql', // The env function will read from the environment or .env file 'socket' => env('MYSQL56_SOCKET', ''), 'host' => env('MYSQL56_HOST', 'localhost'), 'port' => env('MYSQL56_PORT', 3306), 'user' => env('MYSQL56_USER', 'root'), 'password' => env('MYSQL56_PASSWORD', 'root'), // If you mark it as a 'production' server, a confirmation question // will be pronted in every execution and no drop action will // be performed for safety reasons 'production' => TRUE ] ] ];
Repositories
You have to set the repositories where you will store the dumps.
return [ 'repositories' => [ 'local' => [ 'driver' => 'fs', 'path' => __DIR__ . '/snaps' // Local snaps directory ], 'remote' => [ 'driver' => 's3', 'bucket' => env('S3_BUCKET'), 'region' => env('S3_REGION'), // 'profile' => 'remote', 'access_key' => env('S3_ACESS_KEY'), 'secret_key' => env('S3_SECRET_ACESS_KEY'), 'base_path' => 'snaps' // Remote path will be like s3://bucket-name/snaps ], ], ];
Snappers
The snappers tell datashot how to slice down the database.
return [ 'snappers' => [ 'quick' => [ // If you want to snap the rows only // 'data_only' => TRUE, // If you wanna dump only the ddl, triggers, functions, etc. // 'no_data' => TRUE, // Custom made user-defined property for later interpolation 'cutoff' => '(NOW() - INTERVAL 3 MONTH)', // Table-specific where used to filter the rows witch will be dumped 'wheres' => [ // Interpolate the 'cutoff' parameter in the where clause for the // "logs" table 'logs' => "created_at > '{cutoff}'", // Bring only the active users 'users' => 'active = TRUE', ], ] ] ];
Your First Snapshot
To take a database snapshot using the previously configured file:
php vendor/bin/datashot snap myerp --from live1 --to remote:quick_snap --snapper quick
Then datashot will take a proper mysqldump from the scheme myerp that
is running inside the production server live1 and it will be using the quick
snapper to cut the logs and users table down.
Then it will upload a file called quick.gz to the remote s3 repository called
remote previously configured in the datashot.config.php configuration
file.
Restoring Snapshots
You can use datashot to download and restore your snapshots:
php vendor/bin/datashot restore remote:quick --to dev --database myerp_dev
The command above will download the quick snapshot previously taken, restore
the dump as myerp_dev schema at dev databaser server.
You can also restore the snapshot like a regular gziped SQL dump file:
gunzip < path/to/quick.gz | mysql -h localhost myerp
Hat Tipping
I tip my hat to ifsnop/mysqldump-php for providing insights on how to dump a mysql database via PHP/PDO.
License
This project is licensed under the MIT License - see the LICENSE file for details