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