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

289 lines
14 KiB
Text
Raw Normal View History

2020-01-18 10:28:47 +01:00
#!/bin/bash
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
2022-08-31 02:25:01 +02:00
# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
2020-01-18 10:28:47 +01:00
source ../settings/scripts/_common.sh
2021-08-29 00:17:52 +02:00
source ../settings/scripts/ynh_add_swap
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 () {
2022-08-31 02:25:01 +02:00
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
#=================================================
2022-08-31 02:25:01 +02:00
ynh_script_progression --message="Loading installation 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)
db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
2022-08-04 17:28:39 +02:00
datadir=$(ynh_app_setting_get --app=$app --key=datadir)
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"
2022-08-04 17:28:39 +02:00
config_api_gateway="$final_path/live/api-gateway.env"
2021-07-08 11:50:04 +02:00
config_auth="$final_path/live/auth.env"
config_auth_worker="$final_path/live/auth-worker.env"
2022-08-04 17:28:39 +02:00
config_files="$final_path/live/files.env"
config_syncing_server="$final_path/live/syncing-server.env"
config_syncing_server_worker="$final_path/live/syncing-server-worker.env"
config_nginx="/etc/nginx/conf.d/$domain.d/$app.conf"
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
2022-08-31 02:25:01 +02:00
test ! -d $final_path \
|| ynh_die --message="There is already a directory: $final_path "
2020-01-18 10:28:47 +01:00
#=================================================
# STANDARD RESTORATION STEPS
2021-07-08 11:50:04 +02:00
#=================================================
# FIND AND OPEN A PORT
#=================================================
2022-08-31 02:25:01 +02:00
ynh_script_progression --message="Finding an available port..." --weight=1
2021-07-08 11:50:04 +02:00
# Find an available port
2022-08-04 17:28:39 +02:00
port_api_gateway=$(ynh_find_port --port=3000)
port_auth=$(ynh_find_port --port=$((port_api_gateway+1)))
2021-07-08 11:50:04 +02:00
port_auth_worker=$(ynh_find_port --port=$((port_auth+1)))
2022-08-04 17:28:39 +02:00
port_files=$(ynh_find_port --port=$((port_auth_worker+1)))
port_syncing_server=$(ynh_find_port --port=$((port_files+1)))
port_syncing_server_worker=$(ynh_find_port --port=$((port_syncing_server+1)))
ynh_app_setting_set --app=$app --key=port_api_gateway --value=$port_api_gateway
2021-07-08 11:50:04 +02:00
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
2022-08-04 17:28:39 +02:00
ynh_app_setting_set --app=$app --key=port_files --value=$port_files
ynh_app_setting_set --app=$app --key=port_syncing_server --value=$port_syncing_server
ynh_app_setting_set --app=$app --key=port_syncing_server_worker --value=$port_syncing_server_worker
2021-07-08 11:50:04 +02:00
2020-01-18 10:28:47 +01:00
#=================================================
2022-08-31 02:25:01 +02:00
# RECREATE THE DEDICATED USER
2020-01-18 10:28:47 +01:00
#=================================================
2022-08-31 02:25:01 +02:00
ynh_script_progression --message="Recreating the dedicated system user..." --weight=1
2020-01-18 10:28:47 +01:00
2022-08-31 02:25:01 +02:00
# Create the dedicated user (if not existing)
ynh_system_user_create --username=$app --home_dir="$final_path"
2020-01-18 10:28:47 +01:00
#=================================================
# 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"
2022-08-31 02:25:01 +02:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R $app:$app "$final_path"
2022-08-04 17:28:39 +02:00
#=================================================
# RESTORE THE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Restoring the data directory..." --weight=1
ynh_restore_file --origin_path="$datadir" --not_mandatory
mkdir -p "$datadir/uploads"
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R $app:$app "$datadir"
2021-08-29 00:17:52 +02:00
#=================================================
2022-08-31 02:25:01 +02:00
# RESTORE FAIL2BAN CONFIGURATION
2021-08-29 00:17:52 +02:00
#=================================================
2022-08-31 02:25:01 +02:00
ynh_script_progression --message="Restoring the Fail2Ban configuration..." --weight=1
2021-08-29 00:17:52 +02:00
2022-08-31 02:25:01 +02:00
ynh_restore_file --origin_path="/etc/fail2ban/jail.d/$app.conf"
ynh_restore_file --origin_path="/etc/fail2ban/filter.d/$app.conf"
ynh_systemd_action --action=restart --service_name=fail2ban
2021-08-29 00:17:52 +02:00
2022-08-31 02:25:01 +02:00
#=================================================
# SPECIFIC RESTORATION
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
2022-08-31 02:25:01 +02:00
ynh_install_nodejs --nodejs_version=$nodejs_version
2021-07-08 11:50:04 +02:00
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
2022-08-31 02:25:01 +02:00
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the NGINX web server configuration..." --weight=1
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
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
2022-08-31 02:25:01 +02:00
db_pwd=$(ynh_app_setting_get --app=$app --key=mysqlpwd)
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
2022-08-04 17:28:39 +02:00
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_api_gateway"
2021-07-08 11:50:04 +02:00
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"
2022-08-04 17:28:39 +02:00
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_files"
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server"
ynh_replace_string --match_string="^REDIS_URL.*$" --replace_string="REDIS_URL=redis://localhost:6379/$redis_db" --target_file="$config_syncing_server_worker"
# Syncing_Server Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server" --target_file="$config_syncing_server"
ynh_replace_string --match_string="^SYNCING_SERVER_JS_URL.*$" --replace_string="SYNCING_SERVER_JS_URL=http://localhost:$port_syncing_server" --target_file="$config_api_gateway"
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=http://localhost:$port_syncing_server" --target_file="$config_auth"
ynh_replace_string --match_string="^SYNCING_SERVER_URL.*$" --replace_string="SYNCING_SERVER_URL=http://localhost:$port_syncing_server" --target_file="$config_auth_worker"
2022-08-04 17:28:39 +02:00
# Syncing_Server_Worker Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_syncing_server_worker" --target_file="$config_syncing_server_worker"
2021-07-08 11:50:04 +02:00
# 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=http://localhost:$port_auth" --target_file="$config_api_gateway"
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_syncing_server"
ynh_replace_string --match_string="^AUTH_SERVER_URL.*$" --replace_string="AUTH_SERVER_URL=http://localhost:$port_auth" --target_file="$config_syncing_server_worker"
2021-07-08 11:50:04 +02:00
# 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"
2022-08-12 23:17:56 +02:00
ynh_replace_string_on_line --line="2" --match_string="proxy_pass.*$" --replace_string="proxy_pass http://127.0.0.1:$port_api_gateway/;" --target_file="$config_nginx"
2022-08-04 17:28:39 +02:00
# Files Port
ynh_replace_string --match_string="^PORT.*$" --replace_string="PORT=$port_files" --target_file="$config_files"
2022-08-12 23:17:56 +02:00
ynh_replace_string_on_line --line="17" --match_string="proxy_pass.*$" --replace_string="proxy_pass http://127.0.0.1:$port_files/;" --target_file="$config_nginx"
2021-03-06 21:39:49 +01:00
2022-08-31 02:25:01 +02:00
#=================================================
# RESTORE VARIOUS FILES
#=================================================
ynh_script_progression --message="Restoring various files..." --weight=1
ynh_restore_file --origin_path="/etc/cron.d/$app"
#=================================================
# ADD SWAP
#=================================================
ynh_script_progression --message="Adding swap..."
2022-08-31 04:24:01 +02:00
if [ ${PACKAGE_CHECK_EXEC:-0} -eq 0 ]; then
ynh_add_swap --size=$swap_needed
fi
2022-08-31 02:25:01 +02: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
2022-08-04 17:28:39 +02:00
ynh_restore_file --origin_path="/etc/systemd/system/$app-api-gateway.service"
2021-07-08 11:50:04 +02:00
ynh_restore_file --origin_path="/etc/systemd/system/$app-auth.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-auth-worker.service"
2022-08-04 17:28:39 +02:00
ynh_restore_file --origin_path="/etc/systemd/system/$app-files.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server.service"
ynh_restore_file --origin_path="/etc/systemd/system/$app-syncing-server-worker.service"
systemctl enable $app-api-gateway.service --quiet
2021-07-08 11:50:04 +02:00
systemctl enable $app-auth.service --quiet
systemctl enable $app-auth-worker.service --quiet
2022-08-04 17:28:39 +02:00
systemctl enable $app-files.service --quiet
systemctl enable $app-syncing-server.service --quiet
systemctl enable $app-syncing-server-worker.service --quiet
2020-01-18 10:28:47 +01:00
2022-08-31 02:25:01 +02:00
#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the logrotate configuration..." --weight=1
mkdir -p "/var/log/$app"
chown -R $app: "/var/log/$app"
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
2020-01-18 10:28:47 +01:00
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
2022-08-31 02:25:01 +02:00
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
2020-01-18 10:28:47 +01:00
2022-08-04 17:28:39 +02:00
yunohost service add "$app-api-gateway" --description="Standard Notes - API Gateway" --log="/var/log/$app/api-gateway.log"
2021-07-08 11:50:04 +02:00
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"
2022-08-04 17:28:39 +02:00
yunohost service add "$app-files" --description="Standard Notes - Files" --log="/var/log/$app/files.log"
yunohost service add "$app-syncing-server" --description="Standard Notes - Syncing Server" --log="/var/log/$app/syncing-server.log"
yunohost service add "$app-syncing-server-worker" --description="Standard Notes - Syncing Server - Worker" --log="/var/log/$app/syncing-server-worker.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 \
2022-08-31 02:25:01 +02:00
--service_name="$app-api-gateway" \
--action="start" \
--log_path="/var/log/$app/api-gateway.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2022-08-31 02:25:01 +02:00
--service_name="$app-auth" \
--action="start" \
--log_path="/var/log/$app/auth.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2022-08-31 02:25:01 +02:00
--service_name="$app-auth-worker" \
--action="start" \
--log_path="/var/log/$app/auth-worker.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
2021-07-08 11:50:04 +02:00
ynh_systemd_action \
2022-08-31 02:25:01 +02:00
--service_name="$app-files" \
--action="start" \
--log_path="/var/log/$app/files.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
2022-08-04 17:28:39 +02:00
ynh_systemd_action \
2022-08-31 02:25:01 +02:00
--service_name="$app-syncing-server" \
--action="start" \
--log_path="/var/log/$app/syncing-server.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
2022-08-04 17:28:39 +02:00
ynh_systemd_action \
2022-08-31 02:25:01 +02:00
--service_name="$app-syncing-server-worker" \
--action="start" \
--log_path="/var/log/$app/syncing-server-worker.log" \
--line_match='^.*Server started on port.*$|^.*Starting worker.*$'
2021-01-02 19:42:20 +01:00
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
#=================================================
2022-08-31 02:25:01 +02: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