2020-02-23 12:02:10 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# IMPORT GENERIC HELPERS
|
|
|
|
#=================================================
|
|
|
|
|
|
|
|
source _common.sh
|
|
|
|
source /usr/share/yunohost/helpers
|
|
|
|
|
|
|
|
#=================================================
|
2024-05-25 09:34:34 +02:00
|
|
|
# STOP SYSTEMD SERVICE
|
2020-02-23 12:02:10 +01:00
|
|
|
#=================================================
|
2024-05-25 09:34:34 +02:00
|
|
|
ynh_script_progression --message="Stopping $app's systemd service..." --weight=1
|
2020-11-15 11:32:24 +01:00
|
|
|
|
2020-02-23 12:02:10 +01:00
|
|
|
#=================================================
|
2024-05-25 09:34:34 +02:00
|
|
|
# ENSURE DOWNWARD COMPATIBILITY
|
2020-02-23 12:02:10 +01:00
|
|
|
#=================================================
|
2024-05-25 09:34:34 +02:00
|
|
|
#ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
|
|
|
|
|
2022-03-13 17:45:45 +01:00
|
|
|
|
2021-02-28 16:38:35 +01:00
|
|
|
if grep "${app}.timer" /etc/yunohost/services.yml > /dev/null ; then
|
2020-02-23 12:02:10 +01:00
|
|
|
yunohost service remove $app.timer
|
2024-05-25 22:25:00 +02:00
|
|
|
ynh_systemd_action --service="$app.timer" --action="enable"
|
|
|
|
ynh_systemd_action --service="$app.timer" --action="start"
|
2020-02-23 12:02:10 +01:00
|
|
|
fi
|
|
|
|
|
2020-02-23 21:06:25 +01:00
|
|
|
#=================================================
|
2020-04-12 17:13:11 +02:00
|
|
|
# INSTALL RESTIC
|
2020-02-23 21:06:25 +01:00
|
|
|
#=================================================
|
2020-11-15 12:27:50 +01:00
|
|
|
ynh_script_progression --message="Installing restic binary" --weight=7
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2024-05-25 22:20:53 +02:00
|
|
|
ynh_setup_source --source_id=main --dest_dir="$install_dir"
|
|
|
|
chmod +x "$install_dir/restic"
|
2020-11-14 18:03:13 +01:00
|
|
|
|
2021-03-07 16:09:16 +01:00
|
|
|
#=================================================
|
|
|
|
# CREATE APP USER
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Creating user ${app}"
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2021-03-07 16:09:16 +01:00
|
|
|
id ${app} 2>/dev/null || useradd -m ${app}
|
|
|
|
ynh_script_progression --message="Configure ${app} user sudoer rights"
|
|
|
|
cat > /tmp/${app}_sudoer << EOSUDOER
|
2024-05-25 09:34:34 +02:00
|
|
|
${app} ALL = (root) NOPASSWD: /usr/bin/yunohost*, /bin/journalctl*, /usr/bin/find /etc/yunohost/apps -name backup, ${install_dir}/check_method_${app}
|
2021-03-07 16:09:16 +01:00
|
|
|
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}
|
|
|
|
|
|
|
|
|
2020-11-14 18:03:13 +01:00
|
|
|
#=================================================
|
|
|
|
# ACTIVATE BACKUP METHODS
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Activating backup methods"
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2020-11-15 12:27:50 +01:00
|
|
|
mkdir -p /etc/yunohost/hooks.d/backup_method
|
|
|
|
mkdir -p /usr/share/yunohost/backup_method
|
2020-11-14 18:03:13 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# SETUP THE BACKUP METHOD
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Setting up backup methods"
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2020-11-14 18:03:13 +01:00
|
|
|
ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
|
2024-05-25 09:34:34 +02:00
|
|
|
ynh_configure check_method "${install_dir}/check_method_${app}"
|
2020-11-14 18:03:13 +01:00
|
|
|
|
|
|
|
#=================================================
|
2021-03-21 12:52:05 +01:00
|
|
|
# SETUP LOG SCRIPTS
|
2020-11-14 18:03:13 +01:00
|
|
|
#=================================================
|
2021-03-21 12:52:05 +01:00
|
|
|
ynh_script_progression --message="Setting up backup log script"
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2024-05-25 09:34:34 +02:00
|
|
|
ynh_configure restic_log "${install_dir}/restic_log_${app}"
|
|
|
|
chmod +x "${install_dir}/restic_log_${app}"
|
|
|
|
chown ${app}: "${install_dir}/restic_log_${app}"
|
2020-11-15 12:27:50 +01:00
|
|
|
|
2021-03-21 12:52:05 +01:00
|
|
|
ynh_script_progression --message="Setting up check log script"
|
2024-05-25 09:34:34 +02:00
|
|
|
ynh_configure restic_check_log "${install_dir}/restic_check_log_${app}"
|
|
|
|
chmod +x "${install_dir}/restic_check_log_${app}"
|
|
|
|
chown ${app}: "${install_dir}/restic_check_log_${app}"
|
2021-03-21 12:52:05 +01:00
|
|
|
|
2020-11-15 12:27:50 +01:00
|
|
|
#=================================================
|
|
|
|
# CONFIGURE CRON
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring cron" --weight=5
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2020-11-15 12:27:50 +01:00
|
|
|
ynh_configure backup-with-restic "/usr/local/bin/backup-with-${app}"
|
2024-05-25 09:34:34 +02:00
|
|
|
ynh_configure check-restic "${install_dir}/check-${app}"
|
2021-03-21 10:24:30 +01:00
|
|
|
chmod +x "/usr/local/bin/backup-with-${app}"
|
2021-03-07 16:09:16 +01:00
|
|
|
chown ${app}: "/usr/local/bin/backup-with-${app}"
|
2024-05-25 09:34:34 +02:00
|
|
|
chmod +x "${install_dir}/check-${app}"
|
|
|
|
chmod +x "${install_dir}/check_method_${app}"
|
2020-11-15 12:27:50 +01:00
|
|
|
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"
|
2021-02-28 15:18:31 +01:00
|
|
|
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
|
2020-11-15 12:27:50 +01:00
|
|
|
systemctl start ${app}.timer
|
|
|
|
systemctl start ${app}_check.timer
|
|
|
|
systemctl start ${app}_check_read_data.timer
|
|
|
|
|
2021-02-28 16:38:35 +01:00
|
|
|
#=================================================
|
|
|
|
# UPGRADE SSH CONFIG
|
|
|
|
#=================================================
|
|
|
|
|
2021-03-07 16:09:16 +01:00
|
|
|
# old versions did not have delimiters in ~/.ssh/config
|
2021-02-28 16:38:35 +01:00
|
|
|
# making removal in multi-instance cases break the remaining
|
|
|
|
# instances.
|
|
|
|
# So we need to add the delimiters if they are missing
|
2021-02-28 20:11:40 +01:00
|
|
|
set +o errexit
|
|
|
|
set +o nounset
|
2021-03-07 16:09:16 +01:00
|
|
|
grep -q "begin ${app}" ${ssh_dir}/config
|
2021-02-28 16:38:35 +01:00
|
|
|
missing_delimiters="$?"
|
|
|
|
if [ "$missing_delimiters" -eq 1 ];then
|
|
|
|
# did not find delimiters so removing old configuration
|
2021-03-07 16:09:16 +01:00
|
|
|
sed -e "/Host ${server}/,+6d" ${ssh_dir}/config -i || true
|
|
|
|
cat << EOCONF >> ${ssh_dir}/config
|
2021-02-28 20:11:40 +01:00
|
|
|
# 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
|
2021-02-28 16:38:35 +01:00
|
|
|
EOCONF
|
|
|
|
fi
|
2022-03-13 17:45:45 +01:00
|
|
|
|
|
|
|
#=================================================
|
|
|
|
# GENERIC FINALIZATION
|
|
|
|
#=================================================
|
|
|
|
# SETUP LOGROTATE
|
|
|
|
#=================================================
|
|
|
|
ynh_script_progression --message="Configuring logrotate"
|
2023-01-16 19:29:00 +01:00
|
|
|
|
2022-03-13 17:45:45 +01:00
|
|
|
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"
|