From 7cd26ca0f4912ddaa9034a12d8a26125d2f9a3dc Mon Sep 17 00:00:00 2001 From: Luca Zorzi Date: Sun, 13 Dec 2020 16:53:24 +0100 Subject: [PATCH] add: support for basic logging of backup result (#21) * Added support for basic logging of backup result * Added changelog entry * Added full output logging * Changed logging date format to include the time as well as the date * Fixed error on backup script creation if enable_logging was not specified for a backup job * Improved backup script by removing duplicate code * Added variable restic_log_dir to the readme --- README.md | 25 +++++++++++++------------ defaults/main.yml | 1 + templates/restic_script_Linux.j2 | 32 +++++++++++++++++++++++++++----- vars/defaults.yml | 1 + 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 0e416e7..ae074ad 100644 --- a/README.md +++ b/README.md @@ -62,18 +62,19 @@ ansible-galaxy install arillso.restic * bzip2 ## Role Variables -| Name | Default | Description | -| ---------------------- | -------------------- | --------------------------------------------------------------------------- | -| `restic_url` | `undefined` | The URL to download restic from. Use this variable to overwrite the default | -| `restic_version` | `'0.11.0'` | The version of Restic to install | -| `restic_download_path` | `'/opt/restic'` | Download location for the restic binary | -| `restic_install_path` | `'/usr/local/bin'` | Install location for the restic binary | -| `restic_script_dir` | `'~/restic'` | Location of the generated backup scripts | -| `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored | -| `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up | -| `restic_create_cron` | `false` | Should a cronjob be created for each backup | -| `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs | -| `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs | +| Name | Default | Description | +| ---------------------- | ----------------------------------- | --------------------------------------------------------------------------- | +| `restic_url` | `undefined` | The URL to download restic from. Use this variable to overwrite the default | +| `restic_version` | `'0.11.0'` | The version of Restic to install | +| `restic_download_path` | `'/opt/restic'` | Download location for the restic binary | +| `restic_install_path` | `'/usr/local/bin'` | Install location for the restic binary | +| `restic_script_dir` | `'~/restic'` | Location of the generated backup scripts | +| `restic_log_dir` | `'{{ restic_script_dir }}/log'` | Location of the logs of the backup scripts | +| `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored | +| `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up | +| `restic_create_cron` | `false` | Should a cronjob be created for each backup | +| `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs | +| `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs | ### Repos Restic stores data in repositories. You have to specify at least one repository diff --git a/defaults/main.yml b/defaults/main.yml index 2be548e..5d8bdc7 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -5,6 +5,7 @@ restic_version: '0.11.0' restic_download_path: '/opt/restic' restic_install_path: '/usr/bin' restic_script_dir: '~/restic' +restic_log_dir: '{{ restic_script_dir }}/log' restic_repos: {} restic_backups: [] restic_create_cron: false diff --git a/templates/restic_script_Linux.j2 b/templates/restic_script_Linux.j2 index da2985e..412f37e 100644 --- a/templates/restic_script_Linux.j2 +++ b/templates/restic_script_Linux.j2 @@ -3,6 +3,14 @@ # Backup script for {{ item.src|default('stdin') }} # Use this file to create a Backup and prune existing data with one execution. +{% if item.disable_logging is defined and item.disable_logging %} +{% 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" %} +{% endif %} + export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }} export RESTIC_PASSWORD='{{ restic_repos[item.repo].password | regex_replace('\'', '\'\\\'\'') }}' BACKUP_NAME={{ item.name }} @@ -24,7 +32,7 @@ export B2_ACCOUNT_KEY={{ restic_repos[item.repo].b2_account_key }} {% if item.src is defined %} BACKUP_SOURCE={{ item.src }} {% endif %} -set -euxo pipefail +set -uxo pipefail {# Define Tags #} @@ -102,20 +110,34 @@ if [[ -z ${CRON+x} ]]; then else MODE_TAG="--tag cron" fi + {% if item.stdin is defined and item.stdin == true %} {{ item.stdin_cmd }} | {{ restic_install_path }}/restic backup \ --stdin $MODE_TAG \ {{ tags(item.tags) }} \ {{ stdin_filename(item.stdin_filename) }} \ {% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \ - $@ + $@ \ {% else %} {{ restic_install_path }}/restic backup $BACKUP_SOURCE $MODE_TAG \ {{ tags(item.tags) }} \ {% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \ - $@ -{% endif %} + $@ \ +{% endif %} | tee {{ backup_output_log }} +if [[ $? -eq 0 ]] +then + 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 }} +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 %} +{{ 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 }} +if [[ $? -eq 0 ]] +then + 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 }} +fi diff --git a/vars/defaults.yml b/vars/defaults.yml index 67989fe..76ae62c 100644 --- a/vars/defaults.yml +++ b/vars/defaults.yml @@ -10,6 +10,7 @@ _platform_map: restic_create_paths: - '{{ restic_download_path }}/bin' - '{{ restic_script_dir }}' + - '{{ restic_log_dir }}' restic_bin_bath: '{{ restic_download_path }}/bin/restic-{{ restic_version }}'