From d6d73090c6660d3cf669132e3e2b6715a1a07222 Mon Sep 17 00:00:00 2001 From: L3D Date: Mon, 4 Oct 2021 18:12:38 +0200 Subject: [PATCH] updating... + updated meta info + try to medernize j2 stuff + try to add error to mail subject RESOLVED https://github.com/roles-ansible/ansible_role_restic_archiver/issues/8 --- meta/main.yml | 3 +- templates/includes/mail_report_header.sh.j2 | 14 +++++ templates/includes/mounting.sh.j2 | 6 +++ templates/includes/restic_cache.sh.j2 | 2 + .../set_send_mail_on_error_variable.sh.j2 | 2 + templates/restic_forget_snapshots.j2 | 54 ++++--------------- vars/main.yml | 2 +- 7 files changed, 36 insertions(+), 47 deletions(-) create mode 100644 templates/includes/mail_report_header.sh.j2 create mode 100644 templates/includes/mounting.sh.j2 create mode 100644 templates/includes/restic_cache.sh.j2 create mode 100644 templates/includes/set_send_mail_on_error_variable.sh.j2 diff --git a/meta/main.yml b/meta/main.yml index 0a5c8fe..0cb5a98 100644 --- a/meta/main.yml +++ b/meta/main.yml @@ -8,8 +8,7 @@ galaxy_info: min_ansible_version: 2.11 platforms: - name: Debian - versions: - - buster + versions: all galaxy_tags: - backup - archive diff --git a/templates/includes/mail_report_header.sh.j2 b/templates/includes/mail_report_header.sh.j2 new file mode 100644 index 0000000..b9518d8 --- /dev/null +++ b/templates/includes/mail_report_header.sh.j2 @@ -0,0 +1,14 @@ +# This file is also designed to create a overview +# over the restic backup and +# send it by mail - if wanted +found_error=false +cat < /tmp/mailcontent + + + +Restic summary + + +

SUMMARY for Restic BACKUP

+{{ restic_archiver__additional_mail_msg | default ('') }} +EOT diff --git a/templates/includes/mounting.sh.j2 b/templates/includes/mounting.sh.j2 new file mode 100644 index 0000000..095e8d4 --- /dev/null +++ b/templates/includes/mounting.sh.j2 @@ -0,0 +1,6 @@ +if mountpoint -q {{ restic_archiver__mount_disk }} +then + echo "{{ restic_archiver__mount_disk }} is mounted" +else + mount -a +fi diff --git a/templates/includes/restic_cache.sh.j2 b/templates/includes/restic_cache.sh.j2 new file mode 100644 index 0000000..a78b4ad --- /dev/null +++ b/templates/includes/restic_cache.sh.j2 @@ -0,0 +1,2 @@ +# define cache directory +export XDG_CACHE_HOME={{ restic_archiver__cache_dir }} diff --git a/templates/includes/set_send_mail_on_error_variable.sh.j2 b/templates/includes/set_send_mail_on_error_variable.sh.j2 new file mode 100644 index 0000000..240598c --- /dev/null +++ b/templates/includes/set_send_mail_on_error_variable.sh.j2 @@ -0,0 +1,2 @@ +# Send a mail on error! +restic_archiver__send_mail_on_error=false diff --git a/templates/restic_forget_snapshots.j2 b/templates/restic_forget_snapshots.j2 index 4696729..b04ae42 100644 --- a/templates/restic_forget_snapshots.j2 +++ b/templates/restic_forget_snapshots.j2 @@ -1,38 +1,20 @@ #!/usr/bin/env bash # {{ ansible_managed }} # This file is to cleanup your backup archive and move some snapshots to a external storage. -{% if restic_archiver__cache_config | bool %} -# define cache directory -export XDG_CACHE_HOME={{ restic_archiver__cache_dir }} +{% if restic_archiver__cache_config | bool %} + {% incluede 'includes/restic_cache.sh.j2' %} {% endif %} {% if restic_archiver__mail_on_error | bool %} -# Send a mail on error! -restic_archiver__send_mail_on_error=false + {% incluede 'includes/set_send_mail_on_error_variable.sh.j2' %} {% endif %} - {% if restic_archiver__mail_report | default(false) %} -# This file is also designed to create a overview -# over the restic backup and -# send it by mail - if wanted -found_error=false -cat < /tmp/mailcontent - - - -Restic summary - - -

SUMMARY for Restic BACKUP

-{{ restic_archiver__additional_mail_msg | default ('') }} -EOT + {% incluede 'includes/mail_report_header.sh.j2' %} {% endif %} - {% for repo in restic_archiver__repos %} - {# define macro: how fast should we delete snapshots? @@ -77,21 +59,14 @@ EOT {%- endmacro %} {# - -START DUING STUFF INSIDE THE RESTIC REPO FOR LOOP - +START DOING STUFF INSIDE THE RESTIC REPO FOR LOOP #} { # try {% if restic_archiver__mount_required %} -if mountpoint -q {{ restic_archiver__mount_disk }} -then - echo "{{ restic_archiver__mount_disk }} is mounted" -else - mount -a -fi + {% incluede 'includes/mounting.sh.j2' %} {% endif %} # Settings for Server {{ repo['name'] | string }} @@ -128,7 +103,7 @@ $(restic {{ restic_archiver__default_opt }} check --quiet 2>/dev/null) latest backup Path -$(restic --quiet snapshots --last --json | jq -c '.[]' | while read i; do echo -e "$i" | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('\n', jsondata['hostname'], '\n', jsondata['time'], '\n', str(jsondata['paths'][0]), '\n\n')" ; done) +$(restic --quiet snapshots --latest 1 --json | jq -c '.[]' | while read i; do echo -e "$i" | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('\n', jsondata['hostname'], '\n', jsondata['time'], '\n', str(jsondata['paths'][0]), '\n\n')" ; done) @@ -154,11 +129,7 @@ BACKUP_NAME="{{ repo.name }}_archive" echo "init $BACKUP_NAME" {% if restic_archiver__mount_required %} -if mountpoint -q {{ restic_archiver__mount_disk }} -then - echo "{{ restic_archiver__mount_disk }} is mounted" -else - mount -a + {% incluede 'includes/mounting.sh.j2' %} fi {% endif %} @@ -172,12 +143,7 @@ export RESTIC_PASSWORD2='{{ repo['archive_password'] | regex_replace('\'', '\'\\ export RESTIC_REPOSITORY="{{ repo['location'] }}" export RESTIC_PASSWORD='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null {% if restic_archiver__mount_required %} -if mountpoint -q {{ restic_archiver__mount_disk }} -then - echo "{{ restic_archiver__mount_disk }} is mounted" -else - mount -a -fi + {% incluede 'includes/mounting.sh.j2' %} {% endif %} # transfer snapshots to archive @@ -241,7 +207,7 @@ mail -a "Content-type: text/html" -s "restic backup report" {{ restic_archiver__ {% if restic_archiver__mail_on_error | bool %} if [ "$restic_archiver__send_mail_on_error" == true ]; then -mail -s "restic backup report" {{ restic_archiver__mailaddress }} <<< 'Something went wrong while running restic backup archiver at {{ ansible_hostname }}' +mail -s "[ERROR] restic backup report" {{ restic_archiver__mailaddress }} <<< 'Something went wrong while running restic backup archiver at {{ ansible_hostname }}' fi {% endif %} diff --git a/vars/main.yml b/vars/main.yml index 06d2cf6..5860019 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,3 @@ --- -playbook_version_number: 46 # should be int +playbook_version_number: 47 # should be int playbook_version_path: 'role-restic_archiver_roles-ansible_github.com.version'