From b3092e0bee04780562dee035ec8e72a17a0ce5c4 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sat, 2 Nov 2024 14:38:00 +0100 Subject: [PATCH] move database tasks to separate file --- README.md | 8 ++++++- recipe/databaseTasks.php | 51 ++++++++++++++++++++++++++++++++++++++++ recipe/oxid.php | 1 + recipe/shopTasks.php | 44 +--------------------------------- 4 files changed, 60 insertions(+), 44 deletions(-) create mode 100644 recipe/databaseTasks.php diff --git a/README.md b/README.md index fc73be5..5eb26f9 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,14 @@ See Deployer OXID recipe project for further instructions. ## available tasks +### OXID - dep `oxid:getVersion` [stage] - show version of current OXID installation -- dep `oxid:backupDatabase` [stage] - rotate old database backups and create a new one - dep `oxid:cloneDatabase` [stage] - dump contents from source to destination database - dep `oxid:runMigration` [stage] - run all database migrations + +### database +- dep `database:createBackup` [stage] - rotate old database backups and create a new one +- dep `database:clone` [stage] - dump contents from source to destination database + +### setup - dep `setup:createDbConfig` [stage] - generate database configuration file diff --git a/recipe/databaseTasks.php b/recipe/databaseTasks.php new file mode 100644 index 0000000..38d9b7c --- /dev/null +++ b/recipe/databaseTasks.php @@ -0,0 +1,51 @@ + {{deploy_path}}/database/rotatemap.conf'); + } + if (!test("[ -d {{deploy_path}}/database/backup ]")) { + run("mkdir -p {{deploy_path}}/database/backup"); + } + $source_name = parse_ini_file(get('db_conf_path'))['database']; + if (test("[ -f {{deploy_path}}/database/backup/backup.sql ]")) { + run("logrotate -f -s {{deploy_path}}/database/rotatemap.state {{deploy_path}}/database/rotatemap.conf"); + } + run("{{bin/mysqldump}} --defaults-extra-file={{db_conf_path}} --opt --no-create-db -f ".$source_name." $({{bin/mysql}} --defaults-extra-file={{db_conf_path}} -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;\") > {{deploy_path}}/database/backup/backup.sql"); + info('successfully dumped to {{deploy_path}}/database/backup/backup.sql'); + } else { + warning('missing config, task skipped'); + } +}); \ No newline at end of file diff --git a/recipe/oxid.php b/recipe/oxid.php index cef63ca..13d824e 100644 --- a/recipe/oxid.php +++ b/recipe/oxid.php @@ -6,6 +6,7 @@ require_once __DIR__.'/../../../deployer/deployer/recipe/composer.php'; require_once __DIR__.'/menu.php'; require_once __DIR__.'/hooks.php'; +require_once __DIR__.'/databaseTasks.php'; require_once __DIR__.'/deployTasks.php'; require_once __DIR__.'/setupTasks.php'; require_once __DIR__.'/shopTasks.php'; diff --git a/recipe/shopTasks.php b/recipe/shopTasks.php index 717ee42..443dc0b 100644 --- a/recipe/shopTasks.php +++ b/recipe/shopTasks.php @@ -9,7 +9,6 @@ task('oxid:runMigration', [ 'oxid:setShopsOnline' ]); - require_once 'inc/database.php'; desc('set shops offline'); @@ -59,46 +58,5 @@ task('oxid:getVersion', function () { desc('dump contents from source to destination database'); task('oxid:cloneDatabase', function () { - if (get('mysql_configured') && get('mysqldump_configured')) { - 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'); - $target_name = parse_ini_file(get('db_conf_path'))['database']; - if (askConfirmation('Do you really want to clone from "'.$source_name.'" to "'.$target_name.'"? The target database "'.$target_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"); - info('creating views'); - run('{{release_or_current_path}}/vendor/bin/oe-eshop-db_views_regenerate'); - info('successfully finished'); - } else { - info('abborted'); - } - } else { - warning('missing config, task skipped'); - } -}); - -desc('rotate old database backups and create a new one'); -task('oxid:backupDatabase', function () { - if (get('mysql_configured') && get('mysqldump_configured')) { - if (!test("[ -f {{deploy_path}}/database/rotatemap.conf ]")) { - run('printf "{{deploy_path}}/database/backup/backup.sql {\n rotate 5\n}" > {{deploy_path}}/database/rotatemap.conf'); - } - if (!test("[ -d {{deploy_path}}/database/backup ]")) { - run("mkdir -p {{deploy_path}}/database/backup"); - } - $source_name = parse_ini_file(get('db_conf_path'))['database']; - if (test("[ -f {{deploy_path}}/database/backup/backup.sql ]")) { - run("logrotate -f -s {{deploy_path}}/database/rotatemap.state {{deploy_path}}/database/rotatemap.conf"); - } - run("{{bin/mysqldump}} --defaults-extra-file={{db_conf_path}} --opt --no-create-db -f ".$source_name." $({{bin/mysql}} --defaults-extra-file={{db_conf_path}} -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;\") > {{deploy_path}}/database/backup/backup.sql"); - info('successfully dumped to {{deploy_path}}/database/backup/backup.sql'); - } else { - warning('missing config, task skipped'); - } + invoke('database:clone'); });