mirror of
https://github.com/YunoHost-Apps/restic_ynh.git
synced 2024-09-03 20:16:22 +02:00
Fix upgrade script
This commit is contained in:
parent
d31ec14cb7
commit
1df315cb74
1 changed files with 64 additions and 125 deletions
189
scripts/upgrade
189
scripts/upgrade
|
@ -12,159 +12,98 @@ source /usr/share/yunohost/helpers
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Stopping $app's systemd service..." --weight=1
|
ynh_script_progression --message="Stopping $app's systemd service..." --weight=1
|
||||||
|
|
||||||
|
if grep "${app}.timer" /etc/yunohost/services.yml > /dev/null ; then
|
||||||
|
yunohost service remove $app.timer
|
||||||
|
ynh_systemd_action --service="$app" --action="stop"
|
||||||
|
ynh_systemd_action --service="$app.timer" --action="disable"
|
||||||
|
fi
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# ENSURE DOWNWARD COMPATIBILITY
|
# ENSURE DOWNWARD COMPATIBILITY
|
||||||
#=================================================
|
#=================================================
|
||||||
#ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
|
#ynh_script_progression --message="Ensuring downward compatibility..." --weight=1
|
||||||
|
|
||||||
|
# old versions did not have delimiters in ~/.ssh/config making removal in
|
||||||
if grep "${app}.timer" /etc/yunohost/services.yml > /dev/null ; then
|
# multi-instance cases break the remaining instances.
|
||||||
yunohost service remove $app.timer
|
# So we need to add the delimiters if they are missing
|
||||||
ynh_systemd_action --service="$app.timer" --action="enable"
|
# The config will be re-added afterwards
|
||||||
ynh_systemd_action --service="$app.timer" --action="start"
|
if ! grep -q "begin ${app}" $ssh_dir/config; then
|
||||||
|
# did not find delimiters so removing old configuration
|
||||||
|
sed -e "/Host ${server}/,+6d" "$ssh_dir/config" -i || true
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# INSTALL RESTIC
|
# INSTALL RESTIC
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Installing restic binary" --weight=7
|
ynh_script_progression --message="Installing Restic..." --weight=7
|
||||||
|
|
||||||
ynh_setup_source --source_id=main --dest_dir="$install_dir"
|
ynh_setup_source --source_id=main --dest_dir="$install_dir"
|
||||||
chmod +x "$install_dir/restic"
|
chmod +x "$install_dir/restic"
|
||||||
|
|
||||||
#=================================================
|
# This function will only create it if required
|
||||||
# CREATE APP USER
|
_gen_and_save_public_key
|
||||||
#=================================================
|
|
||||||
ynh_script_progression --message="Creating user ${app}"
|
|
||||||
|
|
||||||
id ${app} 2>/dev/null || useradd -m ${app}
|
# Set ssh config if it is missing
|
||||||
ynh_script_progression --message="Configure ${app} user sudoer rights"
|
if ! grep -q "begin $app" "/root/.ssh/config"; then
|
||||||
cat > /tmp/${app}_sudoer << EOSUDOER
|
_set_ssh_config
|
||||||
${app} ALL = (root) NOPASSWD: /usr/bin/yunohost*, /bin/journalctl*, /usr/bin/find /etc/yunohost/apps -name backup, ${install_dir}/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
|
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
|
# SETUP THE BACKUP METHOD
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Setting up backup methods"
|
ynh_script_progression --message="Setting up backup method..." --weight=1
|
||||||
|
|
||||||
ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
|
mkdir -p /etc/yunohost/hooks.d/backup
|
||||||
ynh_configure check_method "${install_dir}/check_method_${app}"
|
mkdir -p /etc/yunohost/hooks.d/backup_method
|
||||||
|
mkdir -p /usr/share/yunohost/backup_method
|
||||||
|
|
||||||
|
## Backup method
|
||||||
|
_ynh_add_config_j2 --template="backup_method.j2" --destination="/etc/yunohost/hooks.d/backup_method/05-${app}_app"
|
||||||
|
chmod go=--- "/etc/yunohost/hooks.d/backup_method/05-${app}_app"
|
||||||
|
|
||||||
|
_ynh_add_config_j2 --template="backup-with-restic.j2" --destination="$install_dir/backup-with-restic"
|
||||||
|
chmod u+x "$install_dir/backup-with-restic"
|
||||||
|
|
||||||
|
## Check method
|
||||||
|
_ynh_add_config_j2 --template="check_method.j2" --destination="$install_dir/check_method_restic"
|
||||||
|
|
||||||
|
_ynh_add_config_j2 --template="check-restic.j2" --destination="$install_dir/check-restic"
|
||||||
|
chmod u+x "$install_dir/check-restic"
|
||||||
|
|
||||||
|
## Backup log script
|
||||||
|
_ynh_add_config_j2 --template="restic_log.j2" --destination="$install_dir/restic_log"
|
||||||
|
chmod u+x "$install_dir/restic_log"
|
||||||
|
|
||||||
|
# Check log script
|
||||||
|
_ynh_add_config_j2 --template="restic_check_log.j2" --destination="$install_dir/restic_check_log"
|
||||||
|
chmod u+x "$install_dir/restic_check_log"
|
||||||
|
|
||||||
|
chown -R "$app:$app" "$install_dir"
|
||||||
|
|
||||||
#=================================================
|
#=================================================
|
||||||
# SETUP LOG SCRIPTS
|
# SYSTEM CONFIGURATION
|
||||||
#=================================================
|
#=================================================
|
||||||
ynh_script_progression --message="Setting up backup log script"
|
ynh_script_progression --message="Adding system configurations related to $app..." --weight=1
|
||||||
|
|
||||||
ynh_configure restic_log "${install_dir}/restic_log_${app}"
|
# Systemd services and timers
|
||||||
chmod +x "${install_dir}/restic_log_${app}"
|
for suffix in "${systemd_services_suffixes[@]}"; do
|
||||||
chown ${app}: "${install_dir}/restic_log_${app}"
|
ynh_add_systemd_config --service="$app$suffix" --template="systemd$suffix.service"
|
||||||
|
_ynh_add_config_j2 --template="systemd$suffix.timer.j2" --destination="/etc/systemd/system/$app$suffix.timer"
|
||||||
|
ynh_systemd_action --service_name="${app}$suffix.service" --action="disable"
|
||||||
|
systemctl enable --quiet "${app}$suffix.timer"
|
||||||
|
ynh_systemd_action --service_name="${app}$suffix.timer" --action="start"
|
||||||
|
|
||||||
ynh_script_progression --message="Setting up check log script"
|
yunohost service add "$app$suffix" --description="Restic backup program ($app$suffix)" \
|
||||||
ynh_configure restic_check_log "${install_dir}/restic_check_log_${app}"
|
--test_status="systemctl show $app$suffix.service -p ActiveState --value | grep -v failed"
|
||||||
chmod +x "${install_dir}/restic_check_log_${app}"
|
done
|
||||||
chown ${app}: "${install_dir}/restic_check_log_${app}"
|
|
||||||
|
|
||||||
#=================================================
|
ynh_add_config --template="sudoer" --destination="/etc/sudoers.d/$app"
|
||||||
# CONFIGURE CRON
|
chown root:root "/etc/sudoers.d/$app"
|
||||||
#=================================================
|
|
||||||
ynh_script_progression --message="Configuring cron" --weight=5
|
|
||||||
|
|
||||||
ynh_configure backup-with-restic "/usr/local/bin/backup-with-${app}"
|
ynh_use_logrotate --logfile="/var/log/restic_backup_${app}.log"
|
||||||
ynh_configure check-restic "${install_dir}/check-${app}"
|
ynh_use_logrotate --logfile="/var/log/restic_backup_${app}.err"
|
||||||
chmod +x "/usr/local/bin/backup-with-${app}"
|
ynh_use_logrotate --logfile="/var/log/restic_check_${app}.log"
|
||||||
chown ${app}: "/usr/local/bin/backup-with-${app}"
|
ynh_use_logrotate --logfile="/var/log/restic_check_${app}.err"
|
||||||
chmod +x "${install_dir}/check-${app}"
|
|
||||||
chmod +x "${install_dir}/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
|
# END OF SCRIPT
|
||||||
|
|
Loading…
Add table
Reference in a new issue