This simple tool will allow you to make anonymized clone of your database. It's useful for cloning your production database into anonymized one for testing environment or your developers.
Thanks to possibility of running any custom SQL queries you can:
- Truncate tables which devs should not see - e.g. some orders table
- Anonymize users personal data - e.g. change names, emails to md5()
- Change users emails to not send them any messages in testing environment
Check sample/query-anonymize-users.sql
for real-life queries.
- PHP with PDO installed
- mysqldump
- mysql
The simplest way is to run:
$ php anon-dump.php sample/query.sql sample/config.php --force
The script will:
- Load config from file
sample/config.php
- Check if
config['clone']
database exists - if so due to--force
parameter, it will executeDROP DATABASE config['clone']
first - Create new database named
config['clone']
-CREATE DATABASE config['clone']
- Dump origin database (
config['database']
) usingmysqldump
and pipe output tomysql
- so basically clone origin database into new one - Run
sample/query.sql
queries on new database (config['clone']
) - Dump new database to stdout
Run:
$ php anon-dump.php
to see possible arguments.
sample/config.php:
<?php
return array(
'dsn' => 'mysql:host=127.0.0.1', // DB DSN - check for allowed formats at http://php.net/manual/en/pdo.construct.php (e.g., mysql:host=127.0.0.1)
'username' => '---', // DB username (e.g., root)
'password' => '---', // DB password (e.g., passwd)
'database' => '---', // origin DB name, (e.g., my-database)
'clone' => '---_anon', // clone DB name, (e.g., my-database-clone)
);
In anon-dump.php
you can define custom paths to mysqldump
and mysql
executables:
define('PATH_MYSQLDUMP', 'mysqldump');
define('PATH_MYSQL', 'mysql');