#!/bin/bash #================================================= # GENERIC START #================================================= # IMPORT GENERIC HELPERS #================================================= source _common.sh source /usr/share/yunohost/helpers #================================================= # LOAD SETTINGS #================================================= ynh_script_progression --message="Loading installation settings..." export app=$YNH_APP_INSTANCE_NAME export final_path="/opt/yunohost/${app}" export server=$(ynh_app_setting_get $app server) export port=$(ynh_app_setting_get $app port) export ssh_user=$(ynh_app_setting_get $app ssh_user) export backup_path=$(ynh_app_setting_get $app backup_path) export passphrase=$(ynh_app_setting_get $app passphrase) export on_calendar=$(ynh_app_setting_get $app on_calendar) export check_on_calendar=$(ynh_app_setting_get $app check_on_calendar) export check_read_data_on_calendar=$(ynh_app_setting_get $app check_read_data_on_calendar) export conf=$(ynh_app_setting_get $app conf) export data=$(ynh_app_setting_get $app data) export apps=$(ynh_app_setting_get $app apps) export allow_extra_space_use=$(ynh_app_setting_get $app allow_extra_space_use) #================================================= # CHECK VERSION #================================================= ynh_script_progression --message="Checking version..." upgrade_type=$(ynh_check_app_version_changed) #================================================= # BACKUP BEFORE UPGRADE THEN ACTIVE TRAP #================================================= ynh_script_progression --message="Backing up the app before upgrading (may take a while)..." # Backup the current version of the app ynh_backup_before_upgrade ynh_clean_setup () { ynh_clean_check_starting # Restore it if the upgrade fails ynh_restore_upgradebackup } # Exit if an error occurs during the execution of the script ynh_abort_if_errors if grep "${app}.timer" /etc/yunohost/services.yml > /dev/null ; then yunohost service remove $app.timer systemctl --quiet enable $app.timer systemctl start $app.timer fi #================================================= # INSTALL RESTIC #================================================= ynh_script_progression --message="Installing restic binary" --weight=7 install_restic #================================================= # CREATE APP USER #================================================= ynh_script_progression --message="Creating user ${app}" id ${app} 2>/dev/null || useradd -m ${app} ynh_script_progression --message="Configure ${app} user sudoer rights" cat > /tmp/${app}_sudoer << EOSUDOER ${app} ALL = (root) NOPASSWD: /usr/bin/yunohost*, /bin/journalctl*, /usr/bin/find /etc/yunohost/apps -name backup, ${final_path}/check_method_${app} EOSUDOER visudo -cf /tmp/${app}_sudoer && mv /tmp/${app}_sudoer /etc/sudoers.d/${app} ynh_script_progression --message="Move ssh keys from root to ${app} user's home" ynh_script_progression --message="Generate ssh config" set +o errexit set +o nounset export ssh_dir="/root/.ssh" export private_key="${ssh_dir}/id_${app}_ed25519" mkdir ${ssh_dir} 2>/dev/null || true touch ${ssh_dir}/config grep -q "begin ${app}" ${ssh_dir}/config missing_conf="$?" if [ "$missing_conf" -eq "1" ];then cat << EOCONF >> ${ssh_dir}/config # begin $app ssh config Host ${server} Hostname ${server} Port ${port} User ${ssh_user} IdentityFile ${private_key} StrictHostKeyChecking no UserKnownHostsFile /dev/null # end $app ssh config EOCONF fi chown -R ${app}: /home/${app} #================================================= # ACTIVATE BACKUP METHODS #================================================= ynh_script_progression --message="Activating backup methods" mkdir -p /etc/yunohost/hooks.d/backup_method mkdir -p /usr/share/yunohost/backup_method #================================================= # SETUP THE BACKUP METHOD #================================================= ynh_script_progression --message="Setting up backup methods" ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_configure check_method "${final_path}/check_method_${app}" #================================================= # SETUP LOG SCRIPTS #================================================= ynh_script_progression --message="Setting up backup log script" ynh_configure restic_log "${final_path}/restic_log_${app}" chmod +x "${final_path}/restic_log_${app}" chown ${app}: "${final_path}/restic_log_${app}" ynh_script_progression --message="Setting up check log script" ynh_configure restic_check_log "${final_path}/restic_check_log_${app}" chmod +x "${final_path}/restic_check_log_${app}" chown ${app}: "${final_path}/restic_check_log_${app}" #================================================= # CONFIGURE CRON #================================================= ynh_script_progression --message="Configuring cron" --weight=5 ynh_configure backup-with-restic "/usr/local/bin/backup-with-${app}" ynh_configure check-restic "${final_path}/check-${app}" chmod +x "/usr/local/bin/backup-with-${app}" chown ${app}: "/usr/local/bin/backup-with-${app}" chmod +x "${final_path}/check-${app}" chmod +x "${final_path}/check_method_${app}" ynh_add_systemd_config --service=${app} --template=systemd.service ynh_add_systemd_config --service=${app}_check --template=systemd_check.service ynh_add_systemd_config --service=${app}_check_read_data --template=systemd_check_read_data.service ynh_configure systemd.timer "/etc/systemd/system/${app}.timer" ynh_configure systemd_check.timer "/etc/systemd/system/${app}_check.timer" ynh_configure systemd_check_read_data.timer "/etc/systemd/system/${app}_check_read_data.timer" systemctl --quiet disable ${app}.service systemctl --quiet disable ${app}_check.service systemctl --quiet disable ${app}_check_read_data.service systemctl --quiet enable ${app}.timer systemctl --quiet enable ${app}_check.timer systemctl --quiet enable ${app}_check_read_data.timer systemctl start ${app}.timer systemctl start ${app}_check.timer systemctl start ${app}_check_read_data.timer #================================================= # UPGRADE SSH CONFIG #================================================= # old versions did not have delimiters in ~/.ssh/config # making removal in multi-instance cases break the remaining # instances. # So we need to add the delimiters if they are missing set +o errexit set +o nounset grep -q "begin ${app}" ${ssh_dir}/config missing_delimiters="$?" if [ "$missing_delimiters" -eq 1 ];then # did not find delimiters so removing old configuration sed -e "/Host ${server}/,+6d" ${ssh_dir}/config -i || true cat << EOCONF >> ${ssh_dir}/config # begin $app ssh config Host ${server} Hostname ${server} Port ${port} User ${ssh_user} IdentityFile ${private_key} StrictHostKeyChecking no UserKnownHostsFile /dev/null # end $app ssh config EOCONF fi #================================================= # GENERIC FINALIZATION #================================================= # SETUP LOGROTATE #================================================= ynh_script_progression --message="Configuring logrotate" ynh_use_logrotate --logfile=/var/log/restic_backup_${app}.log ynh_use_logrotate --logfile=/var/log/restic_backup_${app}.err ynh_use_logrotate --logfile=/var/log/restic_check_${app}.log ynh_use_logrotate --logfile=/var/log/restic_check_${app}.err #================================================= # END OF SCRIPT #================================================= ynh_script_progression --message="Upgrade of $app completed"