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