From a76bd63d89d984d06817b9765d23b4cdc0ec401a Mon Sep 17 00:00:00 2001 From: Lionel Coupouchetty-Ramouchetty Date: Sat, 14 Nov 2020 18:03:13 +0100 Subject: [PATCH] feat: summarize backup log --- conf/restic_log.j2 | 15 +++++++++++++++ conf/systemd.service | 2 +- manifest.json | 2 +- scripts/install | 7 +++++++ scripts/upgrade | 30 ++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 conf/restic_log.j2 diff --git a/conf/restic_log.j2 b/conf/restic_log.j2 new file mode 100644 index 0000000..86805fb --- /dev/null +++ b/conf/restic_log.j2 @@ -0,0 +1,15 @@ +#!/bin/bash +set -u + +invocation_id=$(systemctl show -p InvocationID --value restic.service) +hostname=$(hostname) +subject="YunoHost Restic backup log on ${hostname}" +backup_results=$(/bin/journalctl _SYSTEMD_INVOCATION_ID=${invocation_id} | grep -oP '(?<= )[a-zA-Z_-]+: \w+') +echo ${backup_results} | grep -iqE 'error|fail' +if [ "$?" -eq 0 ];then + subject="${subject} (FAIL)" +else + subject="${subject} (SUCCESS)" +fi + +echo "${backup_results}" | mail -s "${subject}" root diff --git a/conf/systemd.service b/conf/systemd.service index db07912..1647e27 100644 --- a/conf/systemd.service +++ b/conf/systemd.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=oneshot ExecStart=/usr/local/bin/backup-with-__APP__ -ExecStartPost=/bin/bash -c 'echo -e "Subject: YunoHost Restic backup log on $(hostname)\n$(/bin/journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value __APP__.service`)" | /usr/sbin/sendmail root' +ExecStartPost=/opt/yunohost/__APP__/restic_log User=root Group=root diff --git a/manifest.json b/manifest.json index 6fc49cb..25dee8e 100644 --- a/manifest.json +++ b/manifest.json @@ -6,7 +6,7 @@ "en": "Backup your server with restic.", "fr": "Sauvegardez votre serveur avec restic." }, - "version": "0.10.0~ynh3", + "version": "0.10.0~ynh4", "url": "https://restic.net/", "license": "BSD-2-Clause", "maintainer": { diff --git a/scripts/install b/scripts/install index 66ef0d9..38ecb91 100755 --- a/scripts/install +++ b/scripts/install @@ -50,6 +50,13 @@ 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" +#================================================= +# SETUP LOG SCRIPT +#================================================= +ynh_script_progression --message="Setting up log script" +ynh_configure restic_log "${final_path}/restic_log" +chmod u+x "${final_path}/restic_log" + #================================================= # CONFIGURE CRON #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 83f9d45..88d06a6 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -14,6 +14,7 @@ source /usr/share/yunohost/helpers #================================================= app=$YNH_APP_INSTANCE_NAME +export final_path="/opt/yunohost/${app}" #================================================= # CHECK IF AN UPGRADE IS NEEDED @@ -44,3 +45,32 @@ fi # INSTALL RESTIC #================================================= install_restic + +#================================================= +# CONFIGURE SERVICES +#================================================= +ynh_add_systemd_config --service=${app} --template=systemd.service +ynh_configure systemd.timer "/etc/systemd/system/${app}.timer" +systemctl disable ${app}.service +systemctl enable ${app}.timer + +#================================================= +# ACTIVATE BACKUP METHODS +#================================================= +ynh_script_progression --message="Activating backup methods" +mkdir -p /etc/yunohost/hooks.d/backup_method || true +mkdir -p /usr/share/yunohost/backup_method || true + +#================================================= +# 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" + +#================================================= +# SETUP LOG SCRIPT +#================================================= +ynh_script_progression --message="Setting up log script" +ynh_configure restic_log "${final_path}/restic_log" +chmod u+x "${final_path}/restic_log"