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

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
This commit is contained in:
Luca Zorzi 2020-12-13 16:53:24 +01:00 committed by GitHub
parent 12ac9b11ab
commit 7cd26ca0f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 17 deletions

View file

@ -63,12 +63,13 @@ ansible-galaxy install arillso.restic
## 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_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 |

View file

@ -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

View file

@ -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

View file

@ -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 }}'