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

@ -62,18 +62,19 @@ ansible-galaxy install arillso.restic
* bzip2 * bzip2
## Role Variables ## Role Variables
| Name | Default | Description | | Name | Default | Description |
| ---------------------- | -------------------- | --------------------------------------------------------------------------- | | ---------------------- | ----------------------------------- | --------------------------------------------------------------------------- |
| `restic_url` | `undefined` | The URL to download restic from. Use this variable to overwrite the default | | `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_version` | `'0.11.0'` | The version of Restic to install |
| `restic_download_path` | `'/opt/restic'` | Download location for the restic binary | | `restic_download_path` | `'/opt/restic'` | Download location for the restic binary |
| `restic_install_path` | `'/usr/local/bin'` | Install 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_script_dir` | `'~/restic'` | Location of the generated backup scripts |
| `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored | | `restic_log_dir` | `'{{ restic_script_dir }}/log'` | Location of the logs of the backup scripts |
| `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up | | `restic_repos` | `{}` | A dictionary of repositories where snapshots are stored |
| `restic_create_cron` | `false` | Should a cronjob be created for each backup | | `restic_backups` | `{}` (or `[]`) | A list of dictionaries specifying the files and directories to be backed up |
| `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs | | `restic_create_cron` | `false` | Should a cronjob be created for each backup |
| `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs | | `restic_dir_owner` | `'{{ansible_user}}'` | The owner of all created dirs |
| `restic_dir_group` | `'{{ansible_user}}'` | The group of all created dirs |
### Repos ### Repos
Restic stores data in repositories. You have to specify at least one repository Restic stores data in repositories. You have to specify at least one repository

View file

@ -5,6 +5,7 @@ restic_version: '0.11.0'
restic_download_path: '/opt/restic' restic_download_path: '/opt/restic'
restic_install_path: '/usr/bin' restic_install_path: '/usr/bin'
restic_script_dir: '~/restic' restic_script_dir: '~/restic'
restic_log_dir: '{{ restic_script_dir }}/log'
restic_repos: {} restic_repos: {}
restic_backups: [] restic_backups: []
restic_create_cron: false restic_create_cron: false

View file

@ -3,6 +3,14 @@
# Backup script for {{ item.src|default('stdin') }} # Backup script for {{ item.src|default('stdin') }}
# Use this file to create a Backup and prune existing data with one execution. # 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_REPOSITORY={{ restic_repos[item.repo].location }}
export RESTIC_PASSWORD='{{ restic_repos[item.repo].password | regex_replace('\'', '\'\\\'\'') }}' export RESTIC_PASSWORD='{{ restic_repos[item.repo].password | regex_replace('\'', '\'\\\'\'') }}'
BACKUP_NAME={{ item.name }} BACKUP_NAME={{ item.name }}
@ -24,7 +32,7 @@ export B2_ACCOUNT_KEY={{ restic_repos[item.repo].b2_account_key }}
{% if item.src is defined %} {% if item.src is defined %}
BACKUP_SOURCE={{ item.src }} BACKUP_SOURCE={{ item.src }}
{% endif %} {% endif %}
set -euxo pipefail set -uxo pipefail
{# {#
Define Tags Define Tags
#} #}
@ -102,20 +110,34 @@ if [[ -z ${CRON+x} ]]; then
else else
MODE_TAG="--tag cron" MODE_TAG="--tag cron"
fi fi
{% if item.stdin is defined and item.stdin == true %} {% if item.stdin is defined and item.stdin == true %}
{{ item.stdin_cmd }} | {{ restic_install_path }}/restic backup \ {{ item.stdin_cmd }} | {{ restic_install_path }}/restic backup \
--stdin $MODE_TAG \ --stdin $MODE_TAG \
{{ tags(item.tags) }} \ {{ tags(item.tags) }} \
{{ stdin_filename(item.stdin_filename) }} \ {{ stdin_filename(item.stdin_filename) }} \
{% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \ {% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \
$@ $@ \
{% else %} {% else %}
{{ restic_install_path }}/restic backup $BACKUP_SOURCE $MODE_TAG \ {{ restic_install_path }}/restic backup $BACKUP_SOURCE $MODE_TAG \
{{ 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 %} {% 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 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_create_paths:
- '{{ restic_download_path }}/bin' - '{{ restic_download_path }}/bin'
- '{{ restic_script_dir }}' - '{{ restic_script_dir }}'
- '{{ restic_log_dir }}'
restic_bin_bath: '{{ restic_download_path }}/bin/restic-{{ restic_version }}' restic_bin_bath: '{{ restic_download_path }}/bin/restic-{{ restic_version }}'