From a0c52127d5edc6179bb51189178ebd4ecba55caf Mon Sep 17 00:00:00 2001 From: Sebastian Gumprich Date: Tue, 19 Oct 2021 16:38:17 +0200 Subject: [PATCH] add option to log to journald --- README.md | 1 + templates/restic_script_Linux.j2 | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6f24e49..57340d6 100644 --- a/README.md +++ b/README.md @@ -143,6 +143,7 @@ Available variables: | `schedule_month` | no (`*`) | Month when the job is run. ( 1-12, *, */2, etc ) | | `exclude` | no (`{}`) | Allows you to specify files to exclude. See [Exclude](#exclude) for reference. | | `disable_logging` | no | Optionally disable logging | +| `log_to_journald` | no | Optionally switch logging to journald with the name of the backup job as the tag | | `mail_on_error` | no | Optionally send a mail if the backupjob will fail *(mailx is required)* | | `mail_address` | if `mail_on_error` is true | The mail addressto recive mails if you enabled ``mail_on_error``. | diff --git a/templates/restic_script_Linux.j2 b/templates/restic_script_Linux.j2 index f1ab0c3..93206db 100644 --- a/templates/restic_script_Linux.j2 +++ b/templates/restic_script_Linux.j2 @@ -7,8 +7,13 @@ {% set backup_result_log, backup_output_log = "/dev/null", "/dev/null" %} {% set forget_result_log, forget_output_log = "/dev/null", "/dev/null" %} {% else %} -{% set backup_result_log, backup_output_log = restic_log_dir + "/" + item.name + "-backup-result.log", restic_log_dir + "/" + item.name + "-backup-output.log" %} -{% set forget_result_log, forget_output_log = restic_log_dir + "/" + item.name + "-forget-result.log", restic_log_dir + "/" + item.name + "-forget-output.log" %} +{% if (item.log_to_journald is defined and item.log_to_journald) %} +{% set backup_result_log, backup_output_log = "| systemd-cat -t " + item.name, "| systemd-cat -t " + item.name %} +{% set forget_result_log, forget_output_log = "| systemd-cat -t " + item.name, "| systemd-cat -t " + item.name %} +{% else %} +{% set backup_result_log, backup_output_log = ">> " + restic_log_dir + "/" + item.name + "-backup-result.log", "| tee " + restic_log_dir + "/" + item.name + "-backup-output.log" %} +{% set forget_result_log, forget_output_log = ">> " + restic_log_dir + "/" + item.name + "-forget-result.log", "| tee " + restic_log_dir + "/" + item.name + "-forget-output.log" %} +{% endif %} {% endif %} export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }} @@ -125,12 +130,12 @@ fi {{ tags(item.tags) }} \ {% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \ $@ \ -{% endif %} | tee {{ backup_output_log }} +{% endif %} {{ backup_output_log }} if [[ $? -eq 0 ]] then - echo "$(date -u '+%Y-%m-%d %H:%M:%S') OK" >> {{ backup_result_log }} + echo "$(date -u '+%Y-%m-%d %H:%M:%S') OK" {{ backup_result_log }} else - echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" >> {{ backup_result_log }} + echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" {{ backup_result_log }} fi @@ -138,12 +143,12 @@ fi {# Define stdin forget commands #} -{{ restic_install_path }}/restic forget --path {{ path(item) }} {{ retention_pattern(item) }} {% if item.prune is defined and item.prune == true %}--prune{% endif %} | tee {{ forget_output_log }} +{{ restic_install_path }}/restic forget --path {{ path(item) }} {{ retention_pattern(item) }} {% if item.prune is defined and item.prune == true %}--prune{% endif %} {{ forget_output_log }} if [[ $? -eq 0 ]] then - echo "$(date -u '+%Y-%m-%d %H:%M:%S') OK" >> {{ forget_result_log }} + echo "$(date -u '+%Y-%m-%d %H:%M:%S') OK" {{ forget_result_log }} else - echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" >> {{ forget_result_log }} + echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" {{ forget_result_log }} {% if item.mail_on_error is defined and item.mail_on_error == true %} mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S'). {%- if item.src is defined -%}