1
0
Fork 0
mirror of https://github.com/roles-ansible/ansible_role_restic.git synced 2024-12-11 23:41:32 +01:00

Merge pull request #37 from rndmh3ro/add_journald_logging

add option to log to journald
This commit is contained in:
L3D 2021-10-20 01:49:43 +02:00 committed by GitHub
commit 1dceeed476
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 8 deletions

View file

@ -143,6 +143,7 @@ Available variables:
| `schedule_month` | no (`*`) | Month when the job is run. ( 1-12, *, */2, etc ) | | `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. | | `exclude` | no (`{}`) | Allows you to specify files to exclude. See [Exclude](#exclude) for reference. |
| `disable_logging` | no | Optionally disable logging | | `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_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``. | | `mail_address` | if `mail_on_error` is true | The mail addressto recive mails if you enabled ``mail_on_error``. |

View file

@ -7,8 +7,13 @@
{% set backup_result_log, backup_output_log = "/dev/null", "/dev/null" %} {% set backup_result_log, backup_output_log = "/dev/null", "/dev/null" %}
{% set forget_result_log, forget_output_log = "/dev/null", "/dev/null" %} {% set forget_result_log, forget_output_log = "/dev/null", "/dev/null" %}
{% else %} {% else %}
{% set backup_result_log, backup_output_log = restic_log_dir + "/" + item.name + "-backup-result.log", restic_log_dir + "/" + item.name + "-backup-output.log" %} {% if (item.log_to_journald is defined and item.log_to_journald) %}
{% set forget_result_log, forget_output_log = restic_log_dir + "/" + item.name + "-forget-result.log", restic_log_dir + "/" + item.name + "-forget-output.log" %} {% 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 %} {% endif %}
export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }} export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }}
@ -125,12 +130,12 @@ fi
{{ tags(item.tags) }} \ {{ tags(item.tags) }} \
{% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \ {% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \
$@ \ $@ \
{% endif %} | tee {{ backup_output_log }} {% endif %} {{ backup_output_log }}
if [[ $? -eq 0 ]] if [[ $? -eq 0 ]]
then 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 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 fi
@ -138,12 +143,12 @@ fi
{# {#
Define stdin forget commands 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 ]] if [[ $? -eq 0 ]]
then 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 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 %} {% 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'). 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 -%} {%- if item.src is defined -%}