| @@ -1,99 +1,99 | |||||
| 1 | <?php |
|
1 | <?php | |
| 2 | /** |
|
2 | /** | |
| 3 | * DB Migrate script |
|
3 | * DB Migrate script | |
| 4 | * |
|
4 | * | |
| 5 | * Export source database as SQL file |
|
5 | * Export source database as SQL file | |
| 6 | * Backup destination database as SQL file |
|
6 | * Backup destination database as SQL file | |
| 7 | * Import source export into destination database |
|
7 | * Import source export into destination database | |
| 8 | * Replace values in destination database |
|
8 | * Replace values in destination database | |
| 9 | * |
|
9 | * | |
| 10 | * DB Migrate: Makes DTAP for Wordpress projects easy! |
|
10 | * DB Migrate: Makes DTAP for Wordpress projects easy! | |
| 11 | */ |
|
11 | */ | |
| 12 |
|
12 | |||
| 13 | // get the project root directory |
|
13 | // get the project root directory | |
| 14 | $base_dir = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'); |
|
14 | $base_dir = realpath(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..'); | |
| 15 |
|
15 | |||
| 16 | // get the config file |
|
16 | // get the config file | |
| 17 | $config_file = $base_dir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'dbmigrate' . DIRECTORY_SEPARATOR . 'env.ini'; |
|
17 | $config_file = $base_dir . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'dbmigrate' . DIRECTORY_SEPARATOR . 'env.ini'; | |
| 18 | if (file_exists($config_file) === false) |
|
18 | if (file_exists($config_file) === false) | |
| 19 | exit('Config file does not exists. ' . $config_file); |
|
19 | exit('Config file does not exists. ' . $config_file); | |
| 20 | $config = parse_ini_file($config_file, true); |
|
20 | $config = parse_ini_file($config_file, true); | |
| 21 | $envs = array(); |
|
21 | $envs = array(); | |
| 22 | foreach ($config as $name => $section) |
|
22 | foreach ($config as $name => $section) | |
| 23 | { |
|
23 | { | |
| 24 | if (substr($name, 0, strlen('env_')) === 'env_') |
|
24 | if (substr($name, 0, strlen('env_')) === 'env_') | |
| 25 | $envs[substr($name, strlen('env_'))] = $section; |
|
25 | $envs[substr($name, strlen('env_'))] = $section; | |
| 26 | } |
|
26 | } | |
| 27 |
|
27 | |||
| 28 | // check if config is valid |
|
28 | // check if config is valid | |
| 29 | if (count($envs) < 2) |
|
29 | if (count($envs) < 2) | |
| 30 | exit('Define at leaste 2 environments in the config!'); |
|
30 | exit('Define at leaste 2 environments in the config!'); | |
| 31 |
|
31 | |||
| 32 | // list envs |
|
32 | // list envs | |
| 33 | echo 'Available environments: ' . PHP_EOL; |
|
33 | echo 'Available environments: ' . PHP_EOL; | |
| 34 | foreach ($envs as $name => $env_config) |
|
34 | foreach ($envs as $name => $env_config) | |
| 35 | echo '* ' . $name . PHP_EOL; |
|
35 | echo '* ' . $name . PHP_EOL; | |
| 36 | echo PHP_EOL; |
|
36 | echo PHP_EOL; | |
| 37 |
|
37 | |||
| 38 | // ask source env |
|
38 | // ask source env | |
| 39 | echo 'Select the source env: '; |
|
39 | echo 'Select the source env: '; | |
| 40 | $source = trim(fgets(STDIN)); |
|
40 | $source = trim(fgets(STDIN)); | |
| 41 | echo PHP_EOL; |
|
41 | echo PHP_EOL; | |
| 42 |
|
42 | |||
| 43 | // ask dest env |
|
43 | // ask dest env | |
| 44 | echo 'Select the destination env: '; |
|
44 | echo 'Select the destination env: '; | |
| 45 | $dest = trim(fgets(STDIN)); |
|
45 | $dest = trim(fgets(STDIN)); | |
| 46 | echo PHP_EOL; |
|
46 | echo PHP_EOL; | |
| 47 |
|
47 | |||
| 48 | // ask confirmation |
|
48 | // ask confirmation | |
| 49 | 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]? '; |
|
49 | 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]? '; | |
| 50 | $confirm = strtolower(trim(fgets(STDIN))); |
|
50 | $confirm = strtolower(trim(fgets(STDIN))); | |
| 51 | echo PHP_EOL; |
|
51 | echo PHP_EOL; | |
| 52 | if (in_array($confirm, array('y', 'yes', 'j', 'ja', 'ok')) === false) |
|
52 | if (in_array($confirm, array('y', 'yes', 'j', 'ja', 'ok')) === false) | |
| 53 | exit('Okay, exit!'); |
|
53 | exit('Okay, exit!'); | |
| 54 |
|
54 | |||
| 55 | // create backup directory |
|
55 | // create backup directory | |
| 56 | if (file_exists($base_dir . DIRECTORY_SEPARATOR . 'backup') === false) |
|
56 | if (file_exists($base_dir . DIRECTORY_SEPARATOR . 'backup') === false) | |
| 57 | { |
|
57 | { | |
| 58 | echo 'Creating backup directory ' . $base_dir . DIRECTORY_SEPARATOR . 'backup'; |
|
58 | echo 'Creating backup directory ' . $base_dir . DIRECTORY_SEPARATOR . 'backup'; | |
| 59 | mkdir($base_dir . DIRECTORY_SEPARATOR . 'backup'); |
|
59 | mkdir($base_dir . DIRECTORY_SEPARATOR . 'backup'); | |
| 60 | } |
|
60 | } | |
| 61 |
|
61 | |||
| 62 | // dump source and save path |
|
62 | // dump source and save path | |
| 63 | echo 'Dump source database:' . PHP_EOL; |
|
63 | echo 'Dump source database:' . PHP_EOL; | |
| 64 | $source_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $source . '_' . date('YmdHis') . '.sql'; |
|
64 | $source_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $source . '_' . date('YmdHis') . '.sql'; | |
| 65 | $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']; |
|
65 | $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']; | |
| 66 | echo ' target file: ' . $file . PHP_EOL; |
|
66 | echo ' target file: ' . $file . PHP_EOL; | |
| 67 | echo ' command: ' . $command . PHP_EOL; |
|
67 | echo ' command: ' . $command . PHP_EOL; | |
| 68 | system($command); |
|
68 | system($command); | |
| 69 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; |
|
69 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |
| 70 |
|
70 | |||
| 71 | // dump dest and save path |
|
71 | // dump dest and save path | |
| 72 | echo 'Dump source database:' . PHP_EOL; |
|
72 | echo 'Dump source database:' . PHP_EOL; | |
| 73 | $dest_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $dest . '_' . date('YmdHis') . '.sql'; |
|
73 | $dest_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $dest . '_' . date('YmdHis') . '.sql'; | |
| 74 | $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']; |
|
74 | $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']; | |
| 75 | echo ' target file: ' . $file . PHP_EOL; |
|
75 | echo ' target file: ' . $file . PHP_EOL; | |
| 76 | echo ' command: ' . $command . PHP_EOL; |
|
76 | echo ' command: ' . $command . PHP_EOL; | |
| 77 | system($command); |
|
77 | system($command); | |
| 78 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; |
|
78 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |
| 79 |
|
79 | |||
| 80 | // load data |
|
80 | // load data | |
| 81 | echo 'Load data on destination database:' . PHP_EOL; |
|
81 | echo 'Load data on destination database:' . PHP_EOL; | |
| 82 | $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; |
|
82 | $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; | |
| 83 | echo ' load sql: ' . $source_dump . PHP_EOL; |
|
83 | echo ' load sql: ' . $source_dump . PHP_EOL; | |
| 84 | echo ' command: ' . $command . PHP_EOL; |
|
84 | echo ' command: ' . $command . PHP_EOL; | |
| 85 | system($command); |
|
85 | system($command); | |
| 86 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; |
|
86 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |
| 87 |
|
87 | |||
| 88 | // run dvdgiessen dbsr |
|
88 | // run dvdgiessen dbsr | |
| 89 | echo 'Replace domain information in database:' . PHP_EOL; |
|
89 | echo 'Replace domain information in database:' . PHP_EOL; | |
| 90 | $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']; |
|
90 | $command = 'php vendor\dvdgiessen\dbsr\index.php --host ' . $envs[$dest]['host'] . ' --port ' . $envs[$dest]['port'] . ' --user ' . $envs[$dest]['user'] . ' --password ' . $envs[$dest]['password'] . ' --database ' . $envs[$dest]['name'] . ' --charset utf8 -- ' . $envs[$source]['domain'] . ' ' . $envs[$dest]['domain']; | |
| 91 | echo ' search for: ' . $envs[$source]['domain'] . PHP_EOL; |
|
91 | echo ' search for: ' . $envs[$source]['domain'] . PHP_EOL; | |
| 92 | echo ' replace with: ' . $envs[$dest]['domain'] . PHP_EOL; |
|
92 | echo ' replace with: ' . $envs[$dest]['domain'] . PHP_EOL; | |
| 93 | echo ' command: ' . $command . PHP_EOL; |
|
93 | echo ' command: ' . $command . PHP_EOL; | |
| 94 | system($command); |
|
94 | system($command); | |
| 95 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; |
|
95 | echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL; | |
| 96 |
|
96 | |||
| 97 | // info user |
|
97 | // info user | |
| 98 | echo 'A copy of the old database of ' . $dest . ' is saved in ' . $dest_dump . PHP_EOL; |
|
98 | echo 'A copy of the old database of ' . $dest . ' is saved in ' . $dest_dump . PHP_EOL; | |
| 99 | echo PHP_EOL . 'Done!' . PHP_EOL . PHP_EOL; No newline at end of file |
|
99 | echo PHP_EOL . 'Done!' . PHP_EOL . PHP_EOL; | |
General Comments 0
You need to be logged in to leave comments.
Login now
