diff --git a/.buildpath b/.buildpath new file mode 100644 --- /dev/null +++ b/.buildpath @@ -0,0 +1,5 @@ + + + + + diff --git a/.project b/.project new file mode 100644 --- /dev/null +++ b/.project @@ -0,0 +1,28 @@ + + + markei-dbmigrate + + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.dltk.core.scriptbuilder + + + + + + org.eclipse.php.core.PHPNature + org.eclipse.wst.common.project.facet.core.nature + + diff --git a/.settings/org.eclipse.php.core.prefs b/.settings/org.eclipse.php.core.prefs new file mode 100644 --- /dev/null +++ b/.settings/org.eclipse.php.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +include_path=0;/markei-dbmigrate diff --git a/.settings/org.eclipse.wst.common.project.facet.core.xml b/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 --- /dev/null +++ b/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/LICENSE b/LICENSE new file mode 100644 --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ + Copyright (c) 2014 Markei.nl + + Permission is hereby granted, free of charge, to any person + obtaining a copy of this software and associated documentation + files (the "Software"), to deal in the Software without + restriction, including without limitation the rights to use, + copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following + conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/bin/dbmigrate.php b/bin/dbmigrate.php new file mode 100644 --- /dev/null +++ b/bin/dbmigrate.php @@ -0,0 +1,69 @@ + $section) +{ + if (substr($name, 0, strlen('env_')) === 'env_') + { + $envs[substr($name, strlen('env_'))] = $section; + } +} + +echo 'Available environments:' . PHP_EOL; +foreach ($envs as $name => $env_config) + echo '* ' . $name . PHP_EOL; +echo PHP_EOL; + +echo 'Select the source env:' . PHP_EOL; +$source = trim(fgets(STDIN)); +echo PHP_EOL; + +echo 'Select the destination env:' . PHP_EOL; +$dest = trim(fgets(STDIN)); +echo PHP_EOL; + +echo 'Please make sure: You want to export the ' . $source . ' database to ' . $dest . '? Y|N' . PHP_EOL; +$confirm = strtolower(trim(fgets(STDIN))); +echo PHP_EOL; +if (in_array($confirm, array('y', 'yes', 'j', 'ja', 'ok')) === false) + exit('Okay, exit!'); + +if (file_exists($base_dir . DIRECTORY_SEPARATOR . 'backup') === false) + mkdir($base_dir . DIRECTORY_SEPARATOR . 'backup'); + +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; + +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; + +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; + +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; + +echo PHP_EOL . 'Done!' . PHP_EOL . PHP_EOL; \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 --- /dev/null +++ b/composer.json @@ -0,0 +1,14 @@ +{ + "name": "markei/dbmigrate", + "version": "1", + "description": "Script for migrating databases between environments, easy for use in a situation of Wordpress and DTAP", + "license": "MIT", + "authors": [ + { + "name": "Maarten de Keizer", + "email": "m.de.keizer@markei.nl", + "role": "Lead Developer" + } + ], + "bin": ["bin/dbmigratie.php"] +}