mirror of
https://github.com/YunoHost-Apps/restic_ynh.git
synced 2024-09-03 20:16:22 +02:00
refactor: move check log generation to a dedicated script
This commit is contained in:
parent
060af8b71a
commit
634510449d
7 changed files with 86 additions and 7 deletions
|
@ -21,12 +21,14 @@ do_check() {
|
|||
LOGFILE=/var/log/restic_check_{{ app }}.log
|
||||
ERRFILE=/var/log/restic_check_{{ app }}.err
|
||||
current_date=$(date --iso-8601=seconds)
|
||||
echo -e "\n==============\n${current_date}\n${name}\n==============\n" | tee -a ${LOGFILE} | tee -a ${ERRFILE}
|
||||
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}"
|
||||
}
|
||||
|
|
65
conf/restic_check_log.j2
Normal file
65
conf/restic_check_log.j2
Normal file
|
@ -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
|
|
@ -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` | grep -v "sudo[\[0-9\]\+]")" | /usr/sbin/sendmail root'
|
||||
ExecStartPost=/opt/yunohost/__APP__/restic_check_log___APP__ 0
|
||||
User=__APP__
|
||||
Group=__APP__
|
||||
|
||||
|
|
|
@ -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__
|
||||
|
||||
|
|
|
@ -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
|
||||
#=================================================
|
||||
|
|
|
@ -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}"
|
||||
|
||||
#=================================================
|
||||
|
|
|
@ -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
|
||||
#=================================================
|
||||
|
|
Loading…
Add table
Reference in a new issue