#!/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