1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/gotosocial_ynh.git synced 2024-09-03 19:16:06 +02:00
gotosocial_ynh/scripts/restore
2022-12-17 22:05:20 +01:00

213 lines
9.4 KiB
Bash
Executable file

#!/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
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
ynh_clean_setup () {
#### Remove this function if there's nothing to clean before calling the remove script.
true
}
# Exit if an error occurs during the execution of the script
ynh_abort_if_errors
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..." --weight=1
app="$YNH_APP_INSTANCE_NAME"
final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
landing_page_user=$(ynh_app_setting_get --app="$app" --key=landing_page_user)
domain=$(ynh_app_setting_get --app="$app" --key=domain)
port=$(ynh_app_setting_get --app="$app" --key=port)
path_url=$(ynh_app_setting_get --app="$app" --key=path_url)
client_max_body_size=$(ynh_app_setting_get --app="$app" --key=client_max_body_size)
db_name=$(ynh_app_setting_get --app="$app" --key=db_name)
db_user=$(ynh_app_setting_get --app="$app" --key=db_user)
db_pwd=$(ynh_app_setting_get --app="$app" --key=db_pwd)
datadir=$(ynh_app_setting_get --app="$app" --key=datadir)
instance_expose_peers=$(ynh_app_setting_get --app="$app" --key=instance_expose_peers)
instance_expose_suspended=$(ynh_app_setting_get --app="$app" --key=instance_expose_suspended)
instance_expose_public_timeline=$(ynh_app_setting_get --app="$app" --key=instance_expose_public_timeline)
instance_deliver_to_shared_inboxes=$(ynh_app_setting_get --app="$app" --key=instance_deliver_to_shared_inboxes)
accounts_registration_open=$(ynh_app_setting_get --app="$app" --key=accounts_registration_open)
accounts_approval_required=$(ynh_app_setting_get --app="$app" --key=accounts_approval_required)
accounts_reason_required=$(ynh_app_setting_get --app="$app" --key=accounts_reason_required)
accounts_allow_custom_css=$(ynh_app_setting_get --app="$app" --key=accounts_allow_custom_css)
media_image_max_size=$(ynh_app_setting_get --app="$app" --key=media_image_max_size)
media_video_max_size=$(ynh_app_setting_get --app="$app" --key=media_video_max_size)
media_description_min_chars=$(ynh_app_setting_get --app="$app" --key=media_description_min_chars)
media_description_max_chars=$(ynh_app_setting_get --app="$app" --key=media_description_max_chars)
media_remote_cache_days=$(ynh_app_setting_get --app="$app" --key=media_remote_cache_days)
media_emoji_local_max_size=$(ynh_app_setting_get --app="$app" --key=media_emoji_local_max_size)
media_emoji_remote_max_size=$(ynh_app_setting_get --app="$app" --key=media_emoji_remote_max_size)
storage_backend=$(ynh_app_setting_get --app="$app" --key=storage_backend)
storage_s3_endpoint=$(ynh_app_setting_get --app="$app" --key=storage_s3_endpoint)
storage_s3_proxy=$(ynh_app_setting_get --app="$app" --key=storage_s3_proxy)
storage_s3_access_key=$(ynh_app_setting_get --app="$app" --key=storage_s3_access_key)
storage_s3_secret_key=$(ynh_app_setting_get --app="$app" --key=storage_s3_secret_key)
storage_s3_bucket=$(ynh_app_setting_get --app="$app" --key=storage_s3_bucket)
statuses_max_chars=$(ynh_app_setting_get --app="$app" --key=statuses_max_chars)
statuses_cw_max_chars=$(ynh_app_setting_get --app="$app" --key=statuses_cw_max_chars)
statuses_poll_max_options=$(ynh_app_setting_get --app="$app" --key=statuses_poll_max_options)
statuses_poll_option_max_chars=$(ynh_app_setting_get --app="$app" --key=statuses_poll_option_max_chars)
statuses_media_max_files=$(ynh_app_setting_get --app="$app" --key=statuses_media_max_files)
advanced_cookies_samesite=$(ynh_app_setting_get --app="$app" --key=advanced_cookies_samesite)
advanced_rate_limit_requests=$(ynh_app_setting_get --app="$app" --key=advanced_rate_limit_requests)
#=================================================
# CHECK IF THE APP CAN BE RESTORED
#=================================================
ynh_script_progression --message="Validating restoration parameters..." --weight=1
test ! -d "$final_path" \
|| ynh_die --message="There is already a directory: $final_path "
#=================================================
# STANDARD RESTORATION STEPS
#=================================================
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the NGINX configuration..." --weight=1
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
#=================================================
# RECREATE THE DEDICATED USER
#=================================================
ynh_script_progression --message="Recreating the dedicated system user..." --weight=1
# Create the dedicated user (if not existing)
ynh_system_user_create --username="$app" --home_dir="$final_path"
#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_script_progression --message="Restoring the app main directory..." --weight=1
ynh_restore_file --origin_path="$final_path"
# FIXME: this should be managed by the core in the future
# Here, as a packager, you may have to tweak the ownerhsip/permissions
# such that the appropriate users (e.g. maybe www-data) can access
# files in some cases.
# But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder -
# this will be treated as a security issue.
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
chown -R "$app:www-data" "$final_path"
#=================================================
# RESTORE THE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Restoring the data directory..." --weight=5
ynh_restore_file --origin_path="$datadir" --not_mandatory
mkdir -p "$datadir"
# FIXME: this should be managed by the core in the future
# Here, as a packager, you may have to tweak the ownerhsip/permissions
# such that the appropriate users (e.g. maybe www-data) can access
# files in some cases.
# But FOR THE LOVE OF GOD, do not allow r/x for "others" on the entire folder -
# this will be treated as a security issue.
chmod 750 "$datadir"
chmod -R o-rwx "$datadir"
chown -R "$app:www-data" "$datadir"
#=================================================
# SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Reinstalling dependencies..." --weight=5
# Define and install dependencies
ynh_exec_warn_less ynh_install_app_dependencies "$pkg_dependencies"
#=================================================
# RESTORE THE POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the PostgreSQL database..." --weight=5
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user="$db_user" --db_name="$db_name" --db_pwd="$db_pwd"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS unaccent;" --database="$db_name"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS pg_trgm;" --database="$db_name"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS citext;" --database="$db_name"
ynh_psql_execute_as_root --sql="CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\";" --database="$db_name"
ynh_psql_execute_file_as_root --file="./db.sql" --database="$db_name"
#=================================================
# RESTORE VARIOUS FILES
#=================================================
mkdir -p "/var/log/$app"
#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd configuration..." --weight=1
ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
systemctl enable "$app.service" --quiet
#=================================================
# INTEGRATE SERVICE IN YUNOHOST
#=================================================
ynh_script_progression --message="Integrating service in YunoHost..." --weight=1
yunohost service add "$app" --description="Gotosocial server" --log="/var/log/$app/$app.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..." --weight=1
ynh_systemd_action --service_name="$app" --action="start" --log_path="/var/log/$app/$app.log"
#=================================================
# RESTORE THE LOGROTATE CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the logrotate configuration..." --weight=1
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..." --weight=1
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Restoration completed for $app" --last