From 5b4a66a5f0c1a07ea93c76fc70374c19e1932e53 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Tue, 29 Oct 2024 11:28:04 +0100 Subject: [PATCH] add clone database task --- deploy.php | 32 +++++++++++++++++++++++++++++--- inventory.yml | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/deploy.php b/deploy.php index 6704156..205b1dd 100644 --- a/deploy.php +++ b/deploy.php @@ -35,7 +35,7 @@ task('shop:setShopsOffline', function() { $query = "UPDATE oxshops SET oxactive = '0', OXREGISTERSUBJECT = CONCAT('..', OXREGISTERSUBJECT) WHERE oxactive = '1';"; run('{{bin/mysql}} --defaults-extra-file={{db_conf_path}} {{db_name}} -e "'.$query.'"'); } else { - info('missing config, task skipped'); + warning('missing config, task skipped'); } }); @@ -61,7 +61,7 @@ task('shop:setShopsOnline', function() { $query = "UPDATE oxshops SET oxactive = '1', OXREGISTERSUBJECT = SUBSTR(OXREGISTERSUBJECT, 3) WHERE oxactive = '0' AND OXREGISTERSUBJECT LIKE '..%';"; run('{{bin/mysql}} --defaults-extra-file={{db_conf_path}} {{db_name}} -e "'.$query.'"'); } else { - info('missing config, task skipped'); + warning('missing config, task skipped'); } }); @@ -95,7 +95,7 @@ task('setup:createDbConfig', function () { } }); -desc('show OXID shop version'); +desc('show version of current OXID installation'); task('shop:getVersion', function () { if (test("[ -f {{current_path}}/composer.lock ]")) { cd('{{current_path}}'); @@ -106,3 +106,29 @@ task('shop:getVersion', function () { ); } }); + +desc('dump contents from source to destination database'); +task('shop:cloneDatabase', function () { + if (has('bin/mysql') && strlen(get('bin/mysql')) && + has('bin/mysqldump') && strlen(get('bin/mysqldump')) && + has('db_name') && strlen(get('db_name')) && + has('db_conf_path') && strlen(get('db_conf_path')) && test("[ -f {{db_conf_path}} ]") + ) { + info('Note: Use a dedicated read only user for accessing the source database.'); + info('Using the following source database:'); + $source_host = ask('source database host', 'localhost'); + $source_port = ask('source database port', '3306'); + $source_name = ask('source database name'); + $source_user = ask('source database user'); + $source_pass = askHiddenResponse('source database password'); + if (askConfirmation('Do you really want to clone from "'.$source_name.'" to "{{db_name}}"? The target database "{{db_name}}" will be overwritten!')) { + info('cloning database'); + $auth = "-h".$source_host." -P".$source_port." -u".$source_user." -p'".$source_pass."'"; + run("{{bin/mysqldump}} ".$auth." --opt --no-create-db -f ".$source_name." $({{bin/mysql}} ".$auth." -ANe\"SET group_concat_max_len = 10485760; SELECT GROUP_CONCAT(table_name SEPARATOR ' ') FROM information_schema.tables WHERE table_schema='".$source_name."' AND engine IS NOT NULL;\") | {{bin/mysql}} --defaults-extra-file={{db_conf_path}} -f {{db_name}}"); + } else { + info('abborted'); + } + } else { + warning('missing config, task skipped'); + } +}); diff --git a/inventory.yml b/inventory.yml index 84e891e..436c40d 100644 --- a/inventory.yml +++ b/inventory.yml @@ -4,6 +4,7 @@ config: repository: gitfhfac@git.d3data.de:Customers/my.shop.com.git bin/php: /usr/local/php8.2/bin/php bin/mysql: /usr/local/mysql/bin/mysql + bin/mysqldump: /usr/local/mysql/bin/mysqldump # bin/composer: /usr/local/php8.1/bin/php /home/phfhcwoc/bin/composer.phar # optional for custom composer version .base: &base