From 185ca464b30e3b5918fc3b6e059c18111d417976 Mon Sep 17 00:00:00 2001 From: Jimmy Monin Date: Sat, 7 Apr 2018 12:02:34 +0200 Subject: [PATCH] Add remove, backup and restore scripts --- scripts/backup | 64 ++++++++++++++++++++++++ scripts/remove | 93 +++++++++++++++++++++++++++++++++++ scripts/restore | 126 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 scripts/backup create mode 100644 scripts/remove create mode 100644 scripts/restore diff --git a/scripts/backup b/scripts/backup new file mode 100644 index 0000000..6a84785 --- /dev/null +++ b/scripts/backup @@ -0,0 +1,64 @@ +#!/bin/bash + + +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Fetch helpers file if not in current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +domain=$(ynh_app_setting_get $app domain) +final_path=$(ynh_app_setting_get "$app" final_path) +db_name=$(ynh_app_setting_get $app db_name) + +#================================================= +# STANDARD BACKUP STEPS +#================================================= +# BACKUP APP MAIN DIR +#================================================= + +ynh_backup "$final_path" + +#================================================= +# BACKUP NGINX CONFIGURATION +#================================================= + +ynh_backup "/etc/nginx/conf.d/$domain.d/$app.conf" + +#================================================= +# BACKUP SYSTEMD +#================================================= + +ynh_backup "/etc/systemd/system/$app-puma.service" +ynh_backup "/etc/systemd/system/$app-sidekiq.service" + +#================================================= +# BACKUP THE POSTGRESQL DATABASE +#================================================= + +ynh_psql_dump_db "$db_name" > ${YNH_CWD}/db.sql + +#================================================= +# BACKUP LOG FILE +#================================================= + +ynh_backup "/var/log/$app" diff --git a/scripts/remove b/scripts/remove new file mode 100644 index 0000000..925e711 --- /dev/null +++ b/scripts/remove @@ -0,0 +1,93 @@ +#!/bin/bash + +#================================================= +# GENERIC START +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +domain=$(ynh_app_setting_get $app domain) +db_name=$(ynh_app_setting_get $app db_name) +final_path=$(ynh_app_setting_get $app final_path) + +#================================================= +# STANDARD REMOVE +#================================================= +# STOP AND REMOVE SERVICE +#================================================= + +ynh_remove_systemd_config discourse-puma +ynh_remove_systemd_config discourse-sidekiq + +# STOP AND REMOVE SERVICE +#================================================= + +# Remove the dedicated systemd config +# ynh_remove_systemd_config + +# service $app stop +# rm "/etc/init.d/$app" +# update-rc.d -f $app remove + +#================================================= +# REMOVE SERVICE FROM ADMIN PANEL +#================================================= + +if yunohost service status | grep -q $app +then + echo "Remove $app services" + yunohost service remove $app-puma + yunohost service remove $app-sidekiq +fi + +#================================================= +# REMOVE DEPENDENCIES +#================================================= + +# Remove metapackage and its dependencies +ynh_remove_app_dependencies + +#================================================= +# REMOVE THE POSTGRES DATABASE +#================================================= + +ynh_psql_remove_db $db_name $db_name + +#================================================= +# REMOVE APP MAIN DIR +#================================================= + +# Remove the app directory securely +ynh_secure_remove "$final_path" + +#================================================= +# REMOVE NGINX CONFIGURATION +#================================================= + +# Remove the dedicated nginx config +ynh_remove_nginx_config + +#================================================= +# REMOVE LOGROTATE CONFIGURATION +#================================================= + +# Remove the app-specific logrotate config +ynh_remove_logrotate + +#================================================= +# GENERIC FINALIZATION +#================================================= +# REMOVE DEDICATED USER +#================================================= + +# Delete a system user +ynh_system_user_delete $app diff --git a/scripts/restore b/scripts/restore new file mode 100644 index 0000000..387fd6d --- /dev/null +++ b/scripts/restore @@ -0,0 +1,126 @@ +#!/bin/bash + +#================================================= +# IMPORT GENERIC HELPERS +#================================================= + +if [ ! -e _common.sh ]; then + # Fetch helpers file if not in current directory + cp ../settings/scripts/_common.sh ./_common.sh + chmod a+rx _common.sh +fi +source _common.sh +source /usr/share/yunohost/helpers + +#================================================= +# MANAGE SCRIPT FAILURE +#================================================= + +# Exit if an error occurs during the execution of the script +ynh_abort_if_errors + +#================================================= +# LOAD SETTINGS +#================================================= + +app=$YNH_APP_INSTANCE_NAME + +domain=$(ynh_app_setting_get $app domain) +path_url=$(ynh_app_setting_get $app path) +is_public=$(ynh_app_setting_get $app is_public) +final_path=$(ynh_app_setting_get $app final_path) +db_name=$(ynh_app_setting_get $app db_name) +db_pwd=$(ynh_app_setting_get $app db_pwd) + +#================================================= +# CHECK IF THE APP CAN BE RESTORED +#================================================= + +ynh_webpath_available $domain $path_url \ + || ynh_die "Path not available: ${domain}${path_url}" +test ! -d $final_path \ + || ynh_die "There is already a directory: $final_path " + +#================================================= +# STANDARD RESTORATION STEPS +#================================================= +# RESTORE ALL FILES +#================================================= + +# Restore all config and data +ynh_restore + +#================================================= +# REINSTALL DEPENDENCIES +#================================================= + +ynh_install_app_dependencies "$pkg_dependencies" + +#================================================= +# REINSTALL BUNDLE GEM +#================================================= + +(cd "$final_path" +gem install bundler) + +#================================================= +# RESTORE THE POSTGRESQL DATABASE +#================================================= + +ynh_psql_test_if_first_run +ynh_psql_test_if_first_run +ynh_psql_setup_db "$db_name" "$db_name" "$db_pwd" +# Set extensions +ynh_psql_execute_as_root "\connect $db_name +CREATE EXTENSION IF NOT EXISTS hstore; CREATE EXTENSION IF NOT EXISTS pg_trgm;" +# Restore dump +ynh_psql_execute_file_as_root ./db.sql "$db_name" + +#================================================= +# RESTORE SYSTEMD +#================================================= + +systemctl daemon-reload +systemctl enable $app-puma.service +systemctl enable $app-sidekiq.service + +#================================================= +# SETUP LOGROTATE +#================================================= + +ynh_use_logrotate + +#================================================= +# RECREATE OF THE DEDICATED USER +#================================================= + +ynh_system_user_create $app # Recreate the dedicated user, if not existing + +#================================================= +# RESTORE USER RIGHTS +#================================================= + +chown -R $app: $final_path +chown -R $app: /var/log/$app + +#================================================= +# ADVERTISE SERVICE IN ADMIN PANEL +#================================================= + +yunohost service add $app-puma --log "/var/log/$app/puma.stderr.log" +yunohost service add $app-sidekiq --log "/var/www/$app/log/production.log" + +#================================================= +# GENERIC FINALIZATION +#================================================= +# RELOAD NGINX AND DISCOURSE +#================================================= + +# Wait for discourse-puma to be fully started +# As discourse-sidekiq is a dependency, it is automatically started before +ynh_check_starting_systemd "Use Ctrl-C to stop" "$app-puma" "120" + +systemctl reload nginx + +# Additional pause to avoid 502 errors in package_check after reinstall... +sleep 120s