1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/homeassistant_ynh.git synced 2024-09-03 19:26:16 +02:00
homeassistant_ynh/scripts/restore
2021-12-07 20:47:31 +01:00

86 lines
3.2 KiB
Bash

#!/bin/bash
# to test the functionnality :
# yunohost backup create -n "homeassistant-test" --apps homeassistant
# yunohost app remove homeassistant --purge
# yunohost backup restore "homeassistant-test"
# yunohost backup delete "homeassistant-test"
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers
# manage script failure
ynh_clean_setup () {
ynh_clean_check_starting
}
ynh_abort_if_errors
# retrieve arguments
app=$YNH_APP_INSTANCE_NAME
domain=$(ynh_app_setting_get --app="$app" --key=domain)
port=$(ynh_app_setting_get --app="$app" --key=port)
final_path=$(ynh_app_setting_get --app="$app" --key=final_path)
data_path=$(ynh_app_setting_get --app="$app" --key=data_path)
log_file=$(ynh_app_setting_get --app="$app" --key=log_file)
path_url=$(ynh_app_setting_get --app="$app" --key=path_url)
python=$(ynh_app_setting_get --app="$app" --key=python)
# check domain/path availability
ynh_script_progression --message="Validating recovery parameters..."
[ ! -d "$final_path" ] || ynh_die --message="This path already contains a folder"
# restore port
ynh_script_progression --message="Restoring the port and opening it..."
ynh_exec_warn_less yunohost firewall allow TCP $port
# restore dedicated system user
ynh_script_progression --message="Restoring dedicated user and rights folders..."
ynh_system_user_create --username="$app"
ynh_restore_file --origin_path="/etc/sudoers.d/$app"
# restore source
ynh_script_progression --message="Restoring the app..."
ynh_restore_file --origin_path="$final_path"
# restore data
ynh_script_progression --message="Restoring the data..."
ynh_restore_file --origin_path="$data_path"
# restore log
ynh_script_progression --message="Restoring the app..."
ynh_restore_file --origin_path="$(dirname "$log_file")"
# add required packages
ynh_script_progression --message="Restoring the packages dependencies..."
myynh_install_dependencies --python="$python"
# restore the systemd service
ynh_script_progression --message="Restoring the dedicated service..."
ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
# add service in admin panel
yunohost service add "$app" --log "$log_file" --description "Home Assistant server" --needs_exposed_ports $port
# set permissions
myynh_set_permissions
# restart the app
ynh_script_progression --message="Starting the Home Assistant server..."
sed --in-place "/ExecStart/s/$/ --verbose/" "/etc/systemd/system/$app.service"
ynh_systemd_action --service_name="$app" --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=900
# remove --verbose from service and restart
ynh_replace_string --match_string=" --verbose" --replace_string="" --target_file="/etc/systemd/system/$app.service"
ynh_store_file_checksum --file="/etc/systemd/system/$app.service"
systemctl daemon-reload
ynh_systemd_action --service_name="$app" --action=restart
# restore logrotate
ynh_script_progression --message="Restoring logrotate..."
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
# restore nginx
ynh_script_progression --message="Restoring nginx web server..."
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_systemd_action --service_name=nginx --action=reload
ynh_script_progression --message="Recovery of $app completed" --last