diff --git a/check-yunohost-package.sh b/check-yunohost-package.sh index 5bb4961..370e747 100755 --- a/check-yunohost-package.sh +++ b/check-yunohost-package.sh @@ -1,4 +1,4 @@ #!/bin/bash vagrant up -vagrant ssh -c "/package_check/package_check.sh /restic_ynh" +vagrant ssh -c "lxd init --auto;/package_check/package_check.sh /restic_ynh" vagrant halt diff --git a/conf/check_method.j2 b/conf/check_method.j2 index 5fa488b..7640672 100644 --- a/conf/check_method.j2 +++ b/conf/check_method.j2 @@ -20,13 +20,15 @@ do_check() { export RESTIC_REPOSITORY=${RESTIC_REPOSITORY_BASE}/$name LOGFILE=/var/log/restic_check_{{ app }}.log ERRFILE=/var/log/restic_check_{{ app }}.err - current_date=$(date +"%d_%m_%y_%H:%M") - echo -e "\n==============\n${current_date}\n==============\n" | tee -a ${LOGFILE} | tee -a ${ERRFILE} + current_date=$(date --iso-8601=seconds) + echo -e "\n$current_date" | tee -a ${LOGFILE} | tee -a ${ERRFILE} + echo -e "BEGIN REPO CHECK: ${name}" | tee -a ${LOGFILE} | tee -a ${ERRFILE} if [ "$check_read_data" -eq "1" ];then $RESTIC_COMMAND check --read-data > >(tee -a $LOGFILE) 2> >(tee -a $ERRFILE >&2) else $RESTIC_COMMAND check > >(tee -a $LOGFILE) 2> >(tee -a $ERRFILE >&2) fi + echo -e "END REPO CHECK: ${name}" | tee -a ${LOGFILE} | tee -a ${ERRFILE} check_return_code="$?" return "${check_return_code}" } diff --git a/conf/restic_check_log.j2 b/conf/restic_check_log.j2 new file mode 100644 index 0000000..cb7e065 --- /dev/null +++ b/conf/restic_check_log.j2 @@ -0,0 +1,65 @@ +#!/bin/bash +set -u + +function parse_log { + # do not consider unset variables as an error + set +u + log="$@" + current_repo='' + repo_ok=0 + check_log='' + echo -e "$log" | while read l;do + matched_repo=$(echo $l | grep -oP "(?<=BEGIN REPO CHECK: ).*") + if [ ! -z "$matched_repo" ];then + current_repo="$matched_repo" + fi + end_matched_repo=$(echo $l | grep -oP "(?<=END REPO CHECK: ).*") + if [ ! -z "$end_matched_repo" ];then + if [ "$repo_ok" -eq "1" ];then + echo -e "\n==> [OK] $current_repo" + else + echo -e "\n==> [ERROR] $current_repo" + echo -e "$current_log\n$l" + fi + current_repo="" + repo_ok=0 + current_log="" + continue + fi + echo $l | grep -q "no errors were found" + if [ "$?" -eq 0 ];then + repo_ok=1 + fi + if [ "$current_repo" != "" ];then + if [ -z "$current_log" ];then + current_log="${l}" + else + current_log="$current_log\n${l}" + fi + fi + done + set -u +} + +COMPLETE_CHECK=${1:-0} + +hostname=$(hostname) +if [ "$COMPLETE_CHECK" -eq "0" ];then + subject="YunoHost Restic check log on ${hostname}" + invocation_id=$(systemctl show -p InvocationID --value {{ app }}_check.service) +else + subject="YunoHost Restic complete check log on ${hostname}" + invocation_id=$(systemctl show -p InvocationID --value {{ app }}_check_read_data.service) +fi +check_log=$(sudo /bin/journalctl _SYSTEMD_INVOCATION_ID=${invocation_id}) +parsed_log=$(parse_log "$check_log") +echo "$parsed_log" | grep -q '\[ERROR\]' +if [ "$?" -eq "0" ];then + subject="${subject} (FAILED)" +else + subject="${subject} (SUCCESS)" +fi + +parsed_log="$parsed_log\n\nTo get more detailed info check the log files /var/log/restic_check_{{ app }}.log and /var/log/restic_check_{{ app }}.err" + +echo -e "${parsed_log}" | mail -s "${subject}" root diff --git a/conf/systemd_check.service b/conf/systemd_check.service index 043d309..0b21ab5 100644 --- a/conf/systemd_check.service +++ b/conf/systemd_check.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=oneshot ExecStart=__FINALPATH__/check-__APP__ -ExecStartPost=/bin/bash -c 'echo -e "Subject: YunoHost Restic check log on $(hostname)\n$(sudo /bin/journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value __APP___check.service`)" | /usr/sbin/sendmail root' +ExecStartPost=/opt/yunohost/__APP__/restic_check_log___APP__ 0 User=__APP__ Group=__APP__ diff --git a/conf/systemd_check_read_data.service b/conf/systemd_check_read_data.service index bb5b7e0..7cad957 100644 --- a/conf/systemd_check_read_data.service +++ b/conf/systemd_check_read_data.service @@ -5,7 +5,7 @@ After=network.target [Service] Type=oneshot ExecStart=__FINALPATH__/check-__APP__ "1" -ExecStartPost=/bin/bash -c 'echo -e "Subject: YunoHost Restic complete check log on $(hostname)\n$(/bin/journalctl _SYSTEMD_INVOCATION_ID=`systemctl show -p InvocationID --value __APP___check_read_data.service`)" | /usr/sbin/sendmail root' +ExecStartPost=/opt/yunohost/__APP__/restic_check_log___APP__ 1 User=__APP__ Group=__APP__ diff --git a/manifest.json b/manifest.json index f328d3c..7103064 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.12.0~ynh6", + "version": "0.12.0~ynh7", "url": "https://restic.net/", "license": "BSD-2-Clause", "maintainer": { diff --git a/scripts/install b/scripts/install index f0f786f..8a8446d 100755 --- a/scripts/install +++ b/scripts/install @@ -62,13 +62,18 @@ ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_configure check_method "${final_path}/check_method_${app}" #================================================= -# SETUP LOG SCRIPT +# SETUP LOG SCRIPTS #================================================= -ynh_script_progression --message="Setting up log script" +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 #================================================= diff --git a/scripts/remove b/scripts/remove index c30cd14..a393666 100755 --- a/scripts/remove +++ b/scripts/remove @@ -38,6 +38,8 @@ ynh_secure_remove "/usr/local/bin/backup-with-${app}" ynh_secure_remove "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_secure_remove "${final_path}/check_method_${app}" ynh_secure_remove "${final_path}/check-${app}" +ynh_secure_remove "${final_path}/restic_log_${app}" +ynh_secure_remove "${final_path}/restic_check_log_${app}" ynh_secure_remove "${final_path}" #================================================= diff --git a/scripts/upgrade b/scripts/upgrade index 6aef29f..4cecda6 100755 --- a/scripts/upgrade +++ b/scripts/upgrade @@ -111,13 +111,18 @@ ynh_configure backup_method "/etc/yunohost/hooks.d/backup_method/05-${app}_app" ynh_configure check_method "${final_path}/check_method_${app}" #================================================= -# SETUP LOG SCRIPT +# SETUP LOG SCRIPTS #================================================= -ynh_script_progression --message="Setting up log script" +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 #=================================================