dbmigrate.php
98 lines
| 4.5 KiB
| text/x-php
|
PhpLexer
/ bin / dbmigrate.php
Maarten de Keizer
|
r0 | <?php | |
Maarten de Keizer
|
r1 | /** | |
* DB Migrate script | |||
* | |||
* Export source database as SQL file | |||
* Backup destination database as SQL file | |||
* Import source export into destination database | |||
* Replace values in destination database | |||
* | |||
* DB Migrate: Makes DTAP for Wordpress projects easy! | |||
*/ | |||
Maarten de Keizer
|
r0 | ||
Maarten de Keizer
|
r1 | // get the project root directory | |
$base_dir = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'); | |||
// get the config file | |||
Maarten de Keizer
|
r0 | $config_file = $base_dir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'dbmigrate' . DIRECTORY_SEPARATOR . 'env.ini'; | |
Maarten de Keizer
|
r1 | if (file_exists($config_file) === false) | |
exit('Config file does not exists. ' . $config_file); | |||
Maarten de Keizer
|
r0 | $config = parse_ini_file($config_file, true); | |
$envs = array(); | |||
foreach ($config as $name => $section) | |||
{ | |||
if (substr($name, 0, strlen('env_')) === 'env_') | |||
$envs[substr($name, strlen('env_'))] = $section; | |||
} | |||
Maarten de Keizer
|
r1 | // check if config is valid | |
if (count($envs) < 2) | |||
exit('Define at leaste 2 environments in the config!'); | |||
// list envs | |||
echo 'Available environments: ' . PHP_EOL; | |||
Maarten de Keizer
|
r0 | foreach ($envs as $name => $env_config) | |
echo '* ' . $name . PHP_EOL; | |||
echo PHP_EOL; | |||
Maarten de Keizer
|
r1 | // ask source env | |
echo 'Select the source env: '; | |||
Maarten de Keizer
|
r0 | $source = trim(fgets(STDIN)); | |
echo PHP_EOL; | |||
Maarten de Keizer
|
r1 | // ask dest env | |
echo 'Select the destination env: '; | |||
Maarten de Keizer
|
r0 | $dest = trim(fgets(STDIN)); | |
echo PHP_EOL; | |||
Maarten de Keizer
|
r1 | // ask confirmation | |
echo 'Please make sure:' . PHP_EOL . 'You want to export the ' . $source . ' database to the ' . $dest . ' database, all data in ' . $dest . ' will be removed! [Y|N]? '; | |||
Maarten de Keizer
|
r0 | $confirm = strtolower(trim(fgets(STDIN))); | |
echo PHP_EOL; | |||
if (in_array($confirm, array('y', 'yes', 'j', 'ja', 'ok')) === false) | |||
exit('Okay, exit!'); | |||
Maarten de Keizer
|
r1 | // create backup directory | |
Maarten de Keizer
|
r0 | if (file_exists($base_dir . DIRECTORY_SEPARATOR . 'backup') === false) | |
Maarten de Keizer
|
r1 | { | |
echo 'Creating backup directory ' . $base_dir . DIRECTORY_SEPARATOR . 'backup'; | |||
Maarten de Keizer
|
r0 | mkdir($base_dir . DIRECTORY_SEPARATOR . 'backup'); | |
Maarten de Keizer
|
r1 | } | |
Maarten de Keizer
|
r0 | ||
Maarten de Keizer
|
r1 | // dump source and save path | |
Maarten de Keizer
|
r0 | echo 'Dump source database:' . PHP_EOL; | |
$source_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $source . '_' . date('YmdHis') . '.sql'; | |||
$command = 'mysqldump --add-drop-table --comments --host=' . $envs[$source]['host'] . ' --no-create-db --password=' . $envs[$source]['password'] . ' --port=' . $envs[$source]['port'] . ' --result-file=' . $file . ' --default-character-set=utf8 --user=' . $envs[$source]['user'] . ' ' . $envs[$source]['name']; | |||
echo ' target file: ' . $file . PHP_EOL; | |||
echo ' command: ' . $command . PHP_EOL; | |||
system($command); | |||
echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |||
Maarten de Keizer
|
r1 | // dump dest and save path | |
Maarten de Keizer
|
r0 | echo 'Dump source database:' . PHP_EOL; | |
$dest_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $dest . '_' . date('YmdHis') . '.sql'; | |||
$command = 'mysqldump --add-drop-table --comments --host=' . $envs[$dest]['host'] . ' --no-create-db --password=' . $envs[$dest]['password'] . ' --port=' . $envs[$dest]['port'] . ' --result-file=' . $file . ' --default-character-set=utf8 --user=' . $envs[$dest]['user'] . ' ' . $envs[$dest]['name']; | |||
echo ' target file: ' . $file . PHP_EOL; | |||
echo ' command: ' . $command . PHP_EOL; | |||
system($command); | |||
echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |||
Maarten de Keizer
|
r1 | // load data | |
Maarten de Keizer
|
r0 | echo 'Load data on destination database:' . PHP_EOL; | |
$command = 'mysql --host=' . $envs[$dest]['host'] . ' --password=' . $envs[$dest]['password'] . ' --port=' . $envs[$dest]['port'] . ' --default-character-set=utf8 --user=' . $envs[$dest]['user'] . ' --database=' . $envs[$dest]['name'] . ' < ' . $source_dump; | |||
echo ' load sql: ' . $source_dump . PHP_EOL; | |||
echo ' command: ' . $command . PHP_EOL; | |||
system($command); | |||
echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |||
Maarten de Keizer
|
r1 | // run dvdgiessen dbsr | |
Maarten de Keizer
|
r0 | echo 'Replace domain information in database:' . PHP_EOL; | |
$command = 'php vendor\dvdgiessen\dbsr\index.php --host ' . $envs[$dest]['host'] . ' --user ' . $envs[$dest]['user'] . ' --password ' . $envs[$dest]['password'] . ' --database ' . $envs[$dest]['name'] . ' --charset utf8 -- ' . $envs[$source]['domain'] . ' ' . $envs[$dest]['domain']; | |||
echo ' search for: ' . $envs[$source]['domain'] . PHP_EOL; | |||
echo ' replace with: ' . $envs[$dest]['domain'] . PHP_EOL; | |||
echo ' command: ' . $command . PHP_EOL; | |||
system($command); | |||
echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |||
Maarten de Keizer
|
r1 | // info user | |
echo 'A copy of the old database of ' . $dest . ' is saved in ' . $dest_dump . PHP_EOL; | |||
Maarten de Keizer
|
r0 | echo PHP_EOL . 'Done!' . PHP_EOL . PHP_EOL; |