From a6eb307770b9b7f495f55efe271ef985c97f1afd Mon Sep 17 00:00:00 2001 From: L3D Date: Wed, 17 Feb 2021 17:21:20 +0100 Subject: [PATCH] merge templates into one --- defaults/main.yml | 8 +-- tasks/cron.yml | 11 --- tasks/mail.yml | 10 +-- templates/restic_forget_snapshots.j2 | 83 ++++++++++++++++++++-- templates/restic_mailsummary.j2 | 102 --------------------------- vars/main.yml | 2 +- 6 files changed, 84 insertions(+), 132 deletions(-) delete mode 100644 templates/restic_mailsummary.j2 diff --git a/defaults/main.yml b/defaults/main.yml index 70ef392..285f6e1 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -20,11 +20,11 @@ restic_archiver__repos: {} # keep_within: 1 # how long should we store all backups by default -restic_archiver__keep: 9 -restic_archiver__keep_hourly: 28 -restic_archiver__keep_daily: 26 +restic_archiver__keep: 5 +restic_archiver__keep_hourly: 16 +restic_archiver__keep_daily: 14 restic_archiver__keep_weekly: 8 -restic_archiver__keep_monthly: 13 +restic_archiver__keep_monthly: 16 restic_archiver__keep_yearly: 12 # owner and user of all restic stuff diff --git a/tasks/cron.yml b/tasks/cron.yml index 7a64ba7..21068a0 100644 --- a/tasks/cron.yml +++ b/tasks/cron.yml @@ -26,14 +26,3 @@ minute: "{{ restic_archiver__minute }}" user: "{{ restic_archiver__owner }}" when: not restic_archiver__log_output - -- name: setup cronjob for restic mailsummary - become: true - cron: - name: "archive restic mailsumary" - job: /opt/restic-mailsummary.sh - cron_file: /etc/crontab - hour: "7" - minute: "30" - user: "{{ restic_archiver__owner }}" - when: restic_archiver__mailsummary diff --git a/tasks/mail.yml b/tasks/mail.yml index 36aceca..7fc447f 100644 --- a/tasks/mail.yml +++ b/tasks/mail.yml @@ -6,12 +6,4 @@ - jq - bsd-mailx state: present - -- name: deploy restic template - template: - src: templates/restic_mailsummary.j2 - dest: /opt/restic-mailsummary.sh - group: "{{ restic_archiver__owner }}" - owner: "{{ restic_archiver__group }}" - mode: 0700 - become: true + when: restic_archiver__mailsummary | bool diff --git a/templates/restic_forget_snapshots.j2 b/templates/restic_forget_snapshots.j2 index 02c61c7..00fc1c7 100644 --- a/templates/restic_forget_snapshots.j2 +++ b/templates/restic_forget_snapshots.j2 @@ -1,6 +1,25 @@ #!/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__mail_report | default(false) %} +# This file is also designed to create a overview +# over the restic backup and +# send it by mail - if wanted +cat < /tmp/mailcontent + + + +Restic summary + + +

SUMMARY for Restic BACKUP

+{{ restic_archiver__additional_mail_msg | default ('') }} +EOT +{% endif %} + + {% for repo in restic_archiver__repos %} {# @@ -71,7 +90,38 @@ BACKUP_NAME="{{ repo.name }}" restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }} +{% if restic_archiver__mail_report | default(false) %} +cat <> /tmp/mailcontent + +

{{ repo.name }}

+ +$(restic {{ restic_archiver__default_opt }} check --quiet 2>/dev/null) + + +

Latest Snapshots

+ + + + + + +$(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\n\n\n\n')" ; done) +
Hostnamelatest backupPath
', jsondata['hostname'], '', jsondata['time'], '', str(jsondata['paths'][0]), '
+ + + + + + + + + +$(restic --quiet stats --json | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('\n')") + +EOT +{% else %} restic {{ restic_archiver__default_opt }} check +{% endif %} {% if repo.archive|default(false) %} # ARCHIVE Settings for Server "{{ repo['name'] | string }}" @@ -81,10 +131,6 @@ export RESTIC_REPOSITORY2="{{ repo['location'] }}" export RESTIC_PASSWORD2='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null BACKUP_NAME="{{ repo.name }}_archive" -# init repo if it does not exist -if ([ -z "$(restic cat config)" ]) 2>/dev/null; then - restic {{ restic_archiver__default_opt }} init --copy-chunker-params -fi {% if restic_archiver__mount_required %} set +euxo pipefail @@ -96,6 +142,11 @@ else fi set -euxo pipefail {% endif %} + +# init repo if it does not exist +if ([ -z "$(restic cat config)" ]) 2>/dev/null; then + restic {{ restic_archiver__default_opt }} init --copy-chunker-params +fi # ARCHIVE Settings for Server "{{ repo['name'] | string }}" export RESTIC_REPOSITORY2="{{ repo['archive_location'] }}" export RESTIC_PASSWORD2='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null @@ -125,13 +176,35 @@ BACKUP_NAME="{{ repo.name }}_archive" restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }} {% endif %} -restic {{ restic_archiver__default_opt }} check +{% if restic_archiver__mail_report | default(false) %} +restic --quiet stats --json | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('\n\n\n\n\n')" >> /tmp/mailcontent {% endif %} +echo -e "
nametotal sizetotal files
snapshots', str(int(jsondata['total_size'] / 1024 / 1024 / 1024 * 1000 )/1000 ), 'GB ', str(jsondata['total_file_count']), ' Files
external_archive', str(int(jsondata['total_size'] / 1024 / 1024 / 1024 * 1000 )/1000 ), 'GB ', str(jsondata['total_file_count']), ' Files.
\n

" >> /tmp/mailcontent +restic {{ restic_archiver__default_opt }} check --quiet >> /tmp/mailcontent +{% else %} +restic {{ restic_archiver__default_opt }} check +{% endif %} {% endfor %} sync set +euxo pipefail + +cat <> /tmp/mailcontent +


+


+


+ +
+$(df -h)
+
+
+ + +EOT + +mail -a "Content-type: text/html" -s "restic backup report" {{ restic_archiver__mailaddress }} < /tmp/mailcontent + {% if restic_archiver__umount_after_usage %} umount {{ restic_archiver__mount_disk }} {% endif %} diff --git a/templates/restic_mailsummary.j2 b/templates/restic_mailsummary.j2 deleted file mode 100644 index 24cd35e..0000000 --- a/templates/restic_mailsummary.j2 +++ /dev/null @@ -1,102 +0,0 @@ -#!/usr/bin/env bash -# {{ ansible_managed }} -# This file is designed to create a overview -# overof the restic backup and -# send it by mail - if wanted -cat < /tmp/mailcontent - - - -Restic summary - - -

SUMMARY for Restic BACKUP

-{{ restic_archiver__additional_mail_msg | default ('') }} -EOT - -{% for repo in restic_archiver__repos %} - -# Settings for Server {{ repo['name'] | string }} -export RESTIC_REPOSITORY="{{ repo['location'] }}" 2>/dev/null -export RESTIC_PASSWORD='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null -BACKUP_NAME="{{ repo.name }}" 2>/dev/null -{# - - STARTING MAIL OUTPUT - -#} - -cat <> /tmp/mailcontent - -

{{ repo.name }}

- -$(restic check --quiet 2>/dev/null) - - -

Latest Snapshots

- - - - - - -$(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\n\n\n\n')" ; done) -
Hostnamelatest backupPath
', jsondata['hostname'], '', jsondata['time'], '', str(jsondata['paths'][0]), '
- - - - - - - - - -$(restic --quiet stats --json | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('\n')") - -EOT - -{% if repo.archive|default(false) %} - -{% if restic_archiver__mount_required %} -if mountpoint -q {{ restic_archiver__mount_disk }} -then - echo "{{ restic_archiver__mount_disk }} is mounted" -else - mount -a 2>/dev/null -fi -{% endif %} - - -# ARCHIVE Settings for Server "{{ repo['name'] | string }}" -echo "EXTERNAL_BACKUP: {{ repo.name }}" 2>/dev/null -export RESTIC_REPOSITORY="{{ repo['archive_location'] }}" 2>/dev/null -export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null -BACKUP_NAME="{{ repo.name }}_archive" 2>/dev/null - -restic --quiet stats --json | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('\n\n\n\n\n')" >> /tmp/mailcontent -{% endif %} -echo -e "
nametotal sizetotal files
snapshots', str(int(jsondata['total_size'] / 1024 / 1024 / 1024 * 1000 )/1000 ), 'GB ', str(jsondata['total_file_count']), ' Files
external archive', str(int(jsondata['total_size'] / 1024 / 1024 / 1024 * 1000 )/1000 ), 'GB ', str(jsondata['total_file_count']), ' Files.
\n

" >> /tmp/mailcontent -restic check --quiet >> /tmp/mailcontent - -{% endfor %} -sync - -cat <> /tmp/mailcontent -


-


-


- -
-$(df -h)
-
-
- - -EOT - -mail -a "Content-type: text/html" -s "restic backup report" {{ restic_archiver__mailaddress }} < /tmp/mailcontent - -{% if restic_archiver__umount_after_usage %} -umount {{ restic_archiver__mount_disk }} -{% endif %} - diff --git a/vars/main.yml b/vars/main.yml index cc04910..a4cd779 100644 --- a/vars/main.yml +++ b/vars/main.yml @@ -1,3 +1,3 @@ --- -playbook_version_number: 39 # should be int +playbook_version_number: 40 # should be int playbook_version_path: 'role-restic_archiver_roles-ansible_github.com.version'