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
2023-02-15 18:15:47 +01:00

114 lines
4.4 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"
#=================================================
# GENERIC START
#=================================================
# IMPORT GENERIC HELPERS
#=================================================
source ../settings/scripts/_common.sh
source /usr/share/yunohost/helpers
#=================================================
# RESTORE THE APP MAIN DIR
#=================================================
ynh_script_progression --message="Restoring the app main directory..."
ynh_restore_file --origin_path="$install_dir"
#=================================================
# RESTORE THE DATA DIRECTORY
#=================================================
ynh_script_progression --message="Restoring the data directory..."
ynh_restore_file --origin_path="$data_dir" --not_mandatory
mkdir -p $data_dir
#=================================================
# RESTORE THE MYSQL DATABASE
#=================================================
ynh_script_progression --message="Restoring the MySQL database..." --weight=1
ynh_mysql_connect_as --user=$db_user --password="$db_pwd" --database=$db_name < ./db.sql
#=================================================
# RESTORE SYSTEM CONFIGURATIONS
#=================================================
ynh_restore_file --origin_path="/etc/nginx/conf.d/$domain.d/$app.conf"
ynh_restore_file --origin_path="/etc/systemd/system/$app.service"
# add --verbose to track restart
sed -i 's/ExecStart=.*/& --verbose/g' "/etc/systemd/system/$app.service"
systemctl enable $app.service --quiet
yunohost service add $app --description="Home Assistant server" --log="$log_file"
ynh_restore_file --origin_path="/etc/logrotate.d/$app"
ynh_script_progression --message="Restoring the Fail2Ban configuration..." --weight=1
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
ynh_restore_file --origin_path="/etc/sudoers.d/$app"
ynh_restore_file --origin_path="$(dirname "$log_file")"
#=================================================
# SPECIFIC RESTORATION
#=================================================
# CHECK PYTHON VERSION AND COMPILE IF NEEDED
#=================================================
ynh_script_progression --message="Restoring Python..." --weight=1
myynh_install_python --python="$python"
#=================================================
# IF NEEDED UPDATE THE PYHTON LINK IN THE VIRUTAL ENV.
#=================================================
ynh_script_progression --message="Updating the pyhton link in the pyhton virtual environment..."
hass_python_link=$(head -1 /var/www/homeassistant/bin/hass | sed 's/#!//')
if [ ! -e "$hass_python_link" ] ; then
hass_python_new_dest=$(which `basename "$hass_python_link"`)
ln -sfn "$hass_python_new_dest" "$hass_python_link"
fi
#=================================================
# SET FILE OWNERSHIP / PERMISSIONS
#=================================================
myynh_set_permissions
#=================================================
# START SYSTEMD SERVICE
#=================================================
ynh_script_progression --message="Starting a systemd service..."
# start systemd service with --verbose
ynh_systemd_action --service_name=$app --action=start --line_match="Home Assistant initialized" --log_path="$log_file" --timeout=3600
# remove --verbose from service
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
#=================================================
# GENERIC FINALIZATION
#=================================================
# RELOAD NGINX AND PHP-FPM
#=================================================
ynh_script_progression --message="Reloading NGINX web server and PHP-FPM..."
ynh_systemd_action --service_name=nginx --action=reload
#=================================================
# END OF SCRIPT
#=================================================
ynh_script_progression --message="Restoration completed for $app" --last