1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/snserver_ynh.git synced 2024-09-03 20:26:22 +02:00
snserver_ynh/scripts/restore

261 lines
12 KiB
Text
Raw Normal View History

2020-01-18 10:28:47 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
#Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
source ../settings/scripts/_common.sh
2021-08-29 00:17:52 +02:00
source ../settings/scripts/ynh_add_swap
2021-05-01 21:37:41 +02:00
source ../settings/scripts/ynh_install_ruby__2
2021-03-06 21:39:49 +01:00
source ../settings/scripts/ynh_redis
2020-01-18 10:28:47 +01:00
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
2020-12-27 23:18:55 +01:00
#### Remove this function if there's nothing to clean before calling the remove script.
true
2020-01-18 10:28:47 +01:00
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Loading settings..." --weight=2
2020-01-18 10:28:47 +01:00
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
final_path=$(ynh_app_setting_get --app=$app --key=final_path)
2021-07-08 11:50:04 +02:00
final_path_www=$(ynh_app_setting_get --app=$app --key=final_path_www)
2020-01-18 10:28:47 +01:00
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
2020-12-20 14:22:36 +01:00
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
2020-01-18 10:28:47 +01:00
2021-03-06 21:39:49 +01:00
redis_db=$(ynh_redis_get_free_db)
ynh_app_setting_set --app=$app --key=redis_db --value="$redis_db"
2021-07-08 11:50:04 +02:00
config_syncing_server_js="$final_path/live/syncing-server-js.env"
config_syncing_server_js_worker="$final_path/live/syncing-server-js-worker.env"
config_auth="$final_path/live/auth.env"
config_auth_worker="$final_path/live/auth-worker.env"
config_api_gateway="$final_path/live/api-gateway.env"
2021-03-06 21:39:49 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# CHECK IF THE APP CAN BE RESTORED
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Validating restoration parameters..." --weight=2
2020-01-18 10:28:47 +01:00
2021-07-08 11:50:04 +02:00
test ! -d $final_path || ynh_die --message="There is already a directory: $final_path "
test ! -d $final_path_www || ynh_die --message="There is already a directory: $final_path_www "
2020-01-18 10:28:47 +01:00
#=================================================
# STANDARD RESTORATION STEPS
2021-07-08 11:50:04 +02:00
#=================================================
# FIND AND OPEN A PORT
#=================================================
ynh_script_progression --message="Finding an available port..." --weight=1
# Find an available port
port_syncing_server_js=$(ynh_find_port --port=3000)
port_syncing_server_js_worker=$(ynh_find_port --port=$((port_syncing_server_js+1)))
port_auth=$(ynh_find_port --port=$((port_syncing_server_js_worker+1)))
port_auth_worker=$(ynh_find_port --port=$((port_auth+1)))
port_api_gateway=$(ynh_find_port --port=$((port_auth_worker+1)))
ynh_app_setting_set --app=$app --key=port_syncing_server_js --value=$port_syncing_server_js
ynh_app_setting_set --app=$app --key=port_syncing_server_js_worker --value=$port_syncing_server_js_worker
ynh_app_setting_set --app=$app --key=port_auth --value=$port_auth
ynh_app_setting_set --app=$app --key=port_auth_worker --value=$port_auth_worker
ynh_app_setting_set --app=$app --key=port_api_gateway --value=$port_api_gateway
2020-01-18 10:28:47 +01:00
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Restoring the app main directory..." --weight=1
2020-01-18 10:28:47 +01:00
ynh_restore_file --origin_path="$final_path"
2021-07-08 11:50:04 +02:00
ynh_restore_file --origin_path="$final_path_www"
2020-01-18 10:28:47 +01:00
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Recreating the dedicated system user..." --weight=1
2020-01-18 10:28:47 +01:00
# Create the dedicated user (if not existing)
2020-12-20 14:22:36 +01:00
ynh_system_user_create --username=$app --home_dir=$final_path
2020-01-18 10:28:47 +01:00
#=================================================
# RESTORE USER RIGHTS
#=================================================
# Restore permissions on app files
2021-05-01 22:04:34 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
2021-07-08 11:50:04 +02:00
chown -R $app:$app "$final_path"
2021-05-01 22:04:34 +02:00
2021-07-08 11:50:04 +02:00
chmod 750 "$final_path_www"
chmod -R o-rwx "$final_path_www"
chown -R "www-data":"www-data" "$final_path_www"
2021-05-01 22:04:34 +02:00
2020-12-27 23:18:55 +01:00
mkdir -p "/var/log/$app"
chown -R $app: "/var/log/$app"
2020-01-18 10:28:47 +01:00
#=================================================
# SPECIFIC RESTORATION
2021-08-29 00:17:52 +02:00
#=================================================
#=================================================
# ADD SWAP
#=================================================
ynh_script_progression --message="Adding swap..."
ynh_add_swap --size=$swap_needed
2020-01-18 10:28:47 +01:00
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Reinstalling dependencies..." --weight=17
2020-01-18 10:28:47 +01:00
# Define and install dependencies
ynh_install_app_dependencies $pkg_dependencies
2021-07-08 11:50:04 +02:00
ynh_install_nodejs --nodejs_version=$NODEJS_VERSION
ynh_install_extra_app_dependencies --repo="deb https://dl.yarnpkg.com/debian/ stable main" --package="yarn" --key="https://dl.yarnpkg.com/debian/pubkey.gpg"
2020-12-20 14:22:36 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# RESTORE THE MYSQL DATABASE
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Restoring the MySQL database..." --weight=2
2020-01-18 10:28:47 +01:00
ynh_mysql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd
ynh_mysql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql
2021-03-06 21:39:49 +01:00
#=================================================
2021-07-08 11:50:04 +02:00
# MODIFY CONFIG
2021-03-06 21:39:49 +01:00
#=================================================
2021-07-08 11:50:04 +02:00
ynh_script_progression --message="Modify config files..." --weight=2
# Redis Port
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_js"
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_js_worker"
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_auth"
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_auth_worker"
# Syncing_Server_JS Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_js" --target_file="$config_syncing_server_js"
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server_js" --target_file="$config_auth"
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server_js" --target_file="$config_auth_worker"
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=$port_syncing_server_js" --target_file="$config_api_gateway"
# Syncing_Server_JS_Worker Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_js_worker" --target_file="$config_syncing_server_js_worker"
# Auth Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_auth" --target_file="$config_auth"
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_syncing_server_js"
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_syncing_server_js_worker"
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=$port_auth" --target_file="$config_api_gateway"
# Auth_Worker Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_auth_worker" --target_file="$config_auth_worker"
# API-Gateway Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_api_gateway" --target_file="$config_api_gateway"
2021-03-06 21:39:49 +01:00
2020-01-18 10:28:47 +01:00
#=================================================
# RESTORE SYSTEMD
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Restoring the systemd configuration..." --weight=1
2020-01-18 10:28:47 +01:00
2021-07-08 11:50:04 +02:00
ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server-js.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server-js-worker.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-auth.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-auth-worker.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-api-gateway.service"
systemctl enable $app-syncing-server-js.service --quiet
systemctl enable $app-syncing-server-js-worker.service --quiet
systemctl enable $app-auth.service --quiet
systemctl enable $app-auth-worker.service --quiet
systemctl enable $app-api-gateway.service --quiet
2020-01-18 10:28:47 +01:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Integrate $app service in Yunohost..." --weight=1
2020-01-18 10:28:47 +01:00
2021-07-08 11:50:04 +02:00
yunohost service add "$app-syncing-server-js" --description="Standard Notes - Syncing Server" --log="/var/log/$app/syncing-server-js.log"
yunohost service add "$app-syncing-server-js-worker" --description="Standard Notes - Syncing Server - Worker" --log="/var/log/$app/syncing-server-js-worker.log"
yunohost service add "$app-auth" --description="Standard Notes - Auth" --log="/var/log/$app/auth.log"
yunohost service add "$app-auth-worker" --description="Standard Notes - Auth - Worker" --log="/var/log/$app/auth-worker.log"
yunohost service add "$app-api-gateway" --description="Standard Notes - API Gateway" --log="/var/log/$app/api-gateway.log"
2020-01-18 10:28:47 +01:00
#=================================================
# START SYSTEMD SERVICE
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Starting a systemd service..." --weight=1
2020-01-18 10:28:47 +01:00
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2021-07-08 22:27:05 +02:00
--service_name="$app-syncing-server-js" \
--action="start" \
--log_path="/var/log/$app/syncing-server-js.log" \
--line_match='{"message":"Server started on port '$port_syncing_server_js'","level":"info"}'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2021-07-08 22:27:05 +02:00
--service_name="$app-syncing-server-js-worker" \
--action="start" \
--log_path="/var/log/$app/syncing-server-js-worker.log" \
--line_match='{"message":"Starting worker...","level":"info"}'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2021-07-08 22:27:05 +02:00
--service_name="$app-auth" \
--action="start" \
--log_path="/var/log/$app/auth.log" \
--line_match='{"message":"Server started on port '$port_auth'","level":"info"}'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2021-07-08 22:27:05 +02:00
--service_name="$app-auth-worker" \
--action="start" \
--log_path="/var/log/$app/auth-worker.log" \
--line_match='{"message":"Starting worker...","level":"info"}'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2021-07-08 22:27:05 +02:00
--service_name="$app-api-gateway" \
--action="start" \
--log_path="/var/log/$app/api-gateway.log" \
--line_match='{"message":"Server started on port '$port_api_gateway'","level":"info"}'
2020-01-18 10:28:47 +01:00
#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
2020-12-24 12:11:42 +01:00
ynh_script_progression --message="Configuring log rotation..." --weight=1
2020-01-18 10:28:47 +01:00
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
2021-01-02 19:42:20 +01:00
#=================================================
# RESTORE FAIL2BAN CONFIGURATION
#=================================================
2021-01-03 15:45:28 +01:00
ynh_script_progression --message="Restoring the fail2ban configuration..." --weight=1
2021-01-02 19:42:20 +01:00
ynh_restore_file "/etc/fail2ban/jail.d/$app.conf"
ynh_restore_file "/etc/fail2ban/filter.d/$app.conf"
ynh_systemd_action --action=restart --service_name=fail2ban
2020-01-18 10:28:47 +01:00
#=================================================
# GENERIC FINALIZATION
#=================================================
2020-12-23 15:19:09 +01:00
# RELOAD NGINX
2020-01-18 10:28:47 +01:00
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Reloading nginx web server..." --weight=2
2020-01-18 10:28:47 +01:00
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
2020-12-23 15:19:09 +01:00
ynh_script_progression --message="Restoration completed for $app" --last