From d0b367752a1f468aa22b397afc4da4bfba4edb7f Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 11 Sep 2017 15:35:16 +0530 Subject: [PATCH] scripts: add restore script --- scripts/backup | 1 + scripts/restore | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ test.sh | 16 +++++++-- 3 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 scripts/restore diff --git a/scripts/backup b/scripts/backup index 73bf523..fa1df5e 100755 --- a/scripts/backup +++ b/scripts/backup @@ -10,6 +10,7 @@ source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= + app=$YNH_APP_INSTANCE_NAME final_path="/var/www/$app" diff --git a/scripts/restore b/scripts/restore new file mode 100644 index 0000000..d187861 --- /dev/null +++ b/scripts/restore @@ -0,0 +1,96 @@ +#!/bin/bash +set -eu # exit on error ; treat unset variables as error + +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +domain=$(ynh_app_setting_get $app domain) +final_path="/var/www/$app" +data_path="/home/yunohost.app/$app" +db_name="$app" +db_user="mmuser" + +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= + +yunohost app checkurl "${domain}" -a "$app" \ + || ynh_die "Path not available: ${domain}" +test ! -d $final_path \ + || ynh_die "There is already a directory: $final_path " + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE THE NGINX CONFIGURATION +#================================================= + +ynh_restore_file "/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# RESTORE THE APP MAIN DIR +#================================================= + +ynh_restore_file "$final_path" + +#================================================= +# RESTORE THE MYSQL DATABASE +#================================================= + +db_pwd=$(ynh_app_setting_get $app mysqlpwd) +ynh_mysql_setup_db $db_user $db_name $db_pwd +ynh_mysql_connect_as $db_user $db_pwd $db_name < ./db.sql + +#================================================= +# RECREATE THE DEDICATED USER +#================================================= + +# Create the dedicated user (if not existing) +smtp_user="$app" +if ! ynh_system_user_exists "$smtp_user"; then + smtp_password=$(ynh_app_setting_get $app smtp_password) + useradd -M --shell /bin/false -p $(openssl passwd -1 "$smtp_password") "$smtp_user" +fi + +#================================================= +# RESTORE USER RIGHTS +#================================================= + +# Restore permissions on app files +chown -R www-data: $final_path +mkdir -p $data_path +chown -R www-data: $data_path + +#================================================= +# SPECIFIC RESTORATION +#================================================= +# REINSTALL DEPENDENCIES +#================================================= + +# Define and install dependencies +# TODO: use ynh_install_app_dependencies once we'll stop supporting Yunohost < 2.6.4 +command -v supervisorctl >/dev/null 2>&1 || sudo apt-get install -y supervisor + +#================================================= +# RESTORE SUPERVISOR CONF +#================================================= + +ynh_restore_file "/etc/supervisor/conf.d/$app.conf" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND START THE APP +#================================================= + +sudo service nginx reload +sudo supervisorctl reload diff --git a/test.sh b/test.sh index 6698a15..c8e8289 100755 --- a/test.sh +++ b/test.sh @@ -114,8 +114,18 @@ function test_simple_upgrade() { function test_simple_backup() { echo "--- Running simple backup test ---" - local BACKUP_DIR="$TESTS_DIR/backups" - _vagrant_ssh "sudo yunohost backup create --ignore-hooks --no-compress --apps $APP_NAME --output-directory $BACKUP_DIR $VERBOSE_OPT" + _vagrant_ssh "sudo yunohost backup create --ignore-hooks --apps $APP_NAME $VERBOSE_OPT" +} + +function test_simple_remove() { + echo "--- Running simple remove test ---" + _vagrant_ssh "sudo yunohost app remove $APP_NAME" +} + +function test_simple_restore() { + echo "--- Running simple restore test ---" + _vagrant_ssh "sudo yunohost backup list | cut -d ' ' -f 2 > backup_name" + _vagrant_ssh "sudo yunohost backup restore \$(cat backup_name) --force --ignore-hooks --apps $APP_NAME $VERBOSE_OPT" } function test_package_check() { @@ -134,5 +144,7 @@ setup test_simple_install test_simple_upgrade test_simple_backup +test_simple_remove +test_simple_restore test_package_check teardown