##// END OF EJS Templates
Add comments, fix paths
Maarten de Keizer -
r1:d4e677d6dde8 default
parent child
Show More
@@ -1,40 +1,65
1 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 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 20 $config = parse_ini_file($config_file, true);
7 21 $envs = array();
8 22 foreach ($config as $name => $section)
9 23 {
10 24 if (substr($name, 0, strlen('env_')) === 'env_')
11 {
12 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 34 foreach ($envs as $name => $env_config)
18 35 echo '* ' . $name . PHP_EOL;
19 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 40 $source = trim(fgets(STDIN));
23 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 45 $dest = trim(fgets(STDIN));
27 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 50 $confirm = strtolower(trim(fgets(STDIN)));
31 51 echo PHP_EOL;
32 52 if (in_array($confirm, array('y', 'yes', 'j', 'ja', 'ok')) === false)
33 53 exit('Okay, exit!');
34 54
55 // create backup directory
35 56 if (file_exists($base_dir . DIRECTORY_SEPARATOR . 'backup') === false)
57 {
58 echo 'Creating backup directory ' . $base_dir . DIRECTORY_SEPARATOR . 'backup';
36 59 mkdir($base_dir . DIRECTORY_SEPARATOR . 'backup');
60 }
37 61
62 // dump source and save path
38 63 echo 'Dump source database:' . PHP_EOL;
39 64 $source_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $source . '_' . date('YmdHis') . '.sql';
40 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 68 system($command);
44 69 echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL;
45 70
71 // dump dest and save path
46 72 echo 'Dump source database:' . PHP_EOL;
47 73 $dest_dump = $file = $base_dir . DIRECTORY_SEPARATOR . 'backup' . DIRECTORY_SEPARATOR . 'dbbackup_' . $dest . '_' . date('YmdHis') . '.sql';
48 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 77 system($command);
52 78 echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL;
53 79
80 // load data
54 81 echo 'Load data on destination database:' . PHP_EOL;
55 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 83 echo ' load sql: ' . $source_dump . PHP_EOL;
@@ -58,6 +85,7 echo ' command: ' . $command . PHP_EOL;
58 85 system($command);
59 86 echo PHP_EOL . 'Complete' . PHP_EOL . PHP_EOL;
60 87
88 // run dvdgiessen dbsr
61 89 echo 'Replace domain information in database:' . PHP_EOL;
62 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 91 echo ' search for: ' . $envs[$source]['domain'] . PHP_EOL;
@@ -66,4 +94,6 echo ' command: ' . $command . PHP_EOL;
66 94 system($command);
67 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 99 echo PHP_EOL . 'Done!' . PHP_EOL . PHP_EOL; No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now