libreerp_ynh/scripts/restore

198 lines
6.9 KiB
Text
Raw Normal View History

2015-09-14 17:12:17 +02:00
#!/bin/bash
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
# Keep this path for calling _common.sh inside the execution's context of backup and restore scripts
2017-05-18 23:28:18 +02:00
source ../settings/scripts/_common.sh
2022-02-13 22:09:18 +01:00
source /usr/share/yunohost/helpers
#=================================================
# MANAGE SCRIPT FAILURE
#=================================================
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
ynh_clean_setup () {
2022-02-17 01:23:35 +01:00
ynh_clean_check_starting
2022-02-13 22:09:18 +01:00
}
# Exit if an error occurs during the execution of the script
2018-12-14 03:36:58 +01:00
ynh_abort_if_errors
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
#=================================================
# LOAD SETTINGS
#=================================================
ynh_script_progression --message="Loading installation settings..."
2017-05-18 23:28:18 +02:00
export app=$YNH_APP_INSTANCE_NAME
2022-02-13 22:09:18 +01:00
export domain=$(ynh_app_setting_get --app=$app --key=domain)
path_url=$(ynh_app_setting_get --app=$app --key=path)
export app_version=$(ynh_app_setting_get --app=$app --key=app_version)
export oca=$(ynh_app_setting_get --app=$app --key=oca)
export port=$(ynh_app_setting_get --app=$app --key=port)
export port_chat=$(ynh_app_setting_get --app=$app --key=port_chat)
export db_name=$(ynh_app_setting_get --app=$app --key=db_name)
db_user=$db_name
2022-02-17 01:23:35 +01:00
export db_pwd=$(ynh_app_setting_get --app=$app --key=psqlpwd)
2022-02-13 22:09:18 +01:00
export final_path=$(ynh_app_setting_get --app=$app --key=final_path)
export conf_file=$(ynh_app_setting_get --app=$app --key=conf_file)
2022-02-13 19:55:00 +01:00
export bin_file="$final_path/venv/bin/python $final_path/$appname/$FORKNAME-bin"
2018-12-16 19:41:40 +01:00
if [ "$app_version" = "9" ]; then
2022-02-13 19:55:00 +01:00
bin_file="$final_path/venv/bin/python $final_path/$appname/$FORKNAME.py"
2018-12-16 19:41:40 +01:00
fi
if [ "$app_version" = "8" ]; then
2022-02-13 19:55:00 +01:00
bin_file="$final_path/venv/bin/python $final_path/$appname/$FORKNAME.py"
2018-12-16 19:41:40 +01:00
fi
2022-02-16 01:26:09 +01:00
export preinstall=0
2017-05-18 23:28:18 +02:00
#=================================================
# CHECK IF THE APP CAN BE RESTORED
#=================================================
2022-02-13 22:09:18 +01:00
ynh_script_progression --message="Validating restoration parameters..."
test ! -d $final_path \
|| ynh_die --message="There is already a directory: $final_path "
2017-05-18 23:28:18 +02:00
#=================================================
2022-02-13 22:09:18 +01:00
# STANDARD RESTORATION STEPS
2017-05-18 23:28:18 +02:00
#=================================================
2022-02-13 22:09:18 +01:00
# RESTORE THE NGINX CONFIGURATION
#=================================================
ynh_script_progression --message="Restoring the NGINX web server configuration..."
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
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..."
# 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..."
ynh_restore_file --origin_path="$final_path/$appname"
ynh_restore_file --origin_path="$final_path/custom-addons"
ynh_restore_file --origin_path="$final_path/.local"
2022-02-17 01:23:35 +01:00
chmod 750 "$final_path"
chmod -R o-rwx "$final_path"
2022-02-13 22:09:18 +01:00
chown -R $app:$app "$final_path"
#=================================================
# SPECIFIC RESTORATION
#=================================================
# REINSTALL DEPENDENCIES
#=================================================
ynh_script_progression --message="Reinstalling dependencies..."
# Define and install dependencies
2022-02-17 01:23:35 +01:00
ynh_exec_warn_less ynh_install_app_dependencies $pkg_dependencies
#=================================================
# ADD SWAP
#=================================================
ynh_script_progression --message="Adding swap..."
ynh_add_swap --size=$swap_needed
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
#=================================================
# BUILD APP
#=================================================
ynh_script_progression --message="Building $app..."
if ! wkhtmltopdf --version | grep "wkhtmltopdf 0.12.4 (with patched qt)"; then
# The debian package has a bug so we deploy a more recent version
if [ -f '../manifest.json' ] ; then
ynh_setup_source /usr/
else
OLD_YNH_CWD=$YNH_CWD
YNH_CWD=$YNH_CWD/../settings/conf
ynh_setup_source /usr/
YNH_CWD=$OLD_YNH_CWD
fi
fi
pushd $final_path
if grep "python3" $final_path/$appname/$FORKNAME-bin ; then
python3 -m venv venv
venv/bin/pip3 install wheel
venv/bin/pip3 install -r $appname/requirements.txt
else
virtualenv venv
venv/bin/pip install wheel
venv/bin/pip install -r $appname/requirements.txt
fi
popd
#=================================================
# RESTORE THE POSTGRESQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the PostgreSQL database..."
# Make sure that postgresql is installed and running
ynh_psql_test_if_first_run
2017-05-18 23:28:18 +02:00
2022-02-17 01:23:35 +01:00
# Create the database
ynh_psql_setup_db --db_user=$db_user --db_name=$db_name --db_pwd=$db_pwd
# Make sure that its encoding is UTF-8
ynh_psql_execute_as_root --sql="update pg_database set encoding = pg_char_to_encoding('UTF8') where datname = '$db_name'"
# Restore the database contents
2022-02-13 22:09:18 +01:00
ynh_psql_connect_as --user=$db_user --password=$db_pwd --database=$db_name < ./db.sql
#=================================================
# RESTORE VARIOUS FILES
#=================================================
ynh_script_progression --message="Restoring various files..."
ynh_restore_file --origin_path="$conf_file"
2022-02-17 01:23:35 +01:00
chmod 400 "$conf_file"
chown $app:$app "$conf_file"
2022-02-13 22:09:18 +01:00
touch /var/log/$app.log
chown $app:$app /var/log/$app.log
#=================================================
# RESTORE SYSTEMD
#=================================================
ynh_script_progression --message="Restoring the systemd configuration..."
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..."
yunohost service add $app --log="/var/log/$app.log"
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
ynh_systemd_action --service_name=$app --action="start" --log_path="/var/log/$app.log"
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX
#=================================================
ynh_script_progression --message="Reloading NGINX web server..."
ynh_systemd_action --service_name=nginx --action=reload
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
#=================================================
# END OF SCRIPT
#=================================================
2017-05-18 23:28:18 +02:00
2022-02-13 22:09:18 +01:00
ynh_script_progression --message="Restoration completed for $app"