1
0
Fork 0
mirror of https://github.com/roles-ansible/ansible_role_restic_archiver.git synced 2024-08-16 10:09:49 +02:00
ansible_role_restic_archiver/templates/restic_forget_snapshots.j2

211 lines
6.6 KiB
Text
Raw Normal View History

2020-05-01 10:56:07 +02:00
#!/usr/bin/env bash
# {{ ansible_managed }}
# This file is to cleanup your backup archive and move some snapshots to a external storage.
2021-02-17 17:21:20 +01:00
{% 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 <<EOT > /tmp/mailcontent
<!DOCTYPE html>
<html>
<head>
<title>Restic summary</title>
</head>
<body>
<h1>SUMMARY for Restic BACKUP</h1>
{{ restic_archiver__additional_mail_msg | default ('') }}
EOT
{% endif %}
2020-12-09 17:26:16 +01:00
{% for repo in restic_archiver__repos %}
{#
2020-12-09 17:26:16 +01:00
define macro:
how fast should we delete snapshots?
Variables are defined via defaults!
2019-09-11 11:29:36 +02:00
#}
2020-12-09 17:26:16 +01:00
{% macro retention_pattern(repo) -%}
{% if repo.keep_last is defined and repo.keep_last != None -%}
--keep-last {{ repo.keep_last }}
{%- else -%}
--keep-last {{ restic_archiver__keep }}
{%- endif %} \
{% if repo.keep_hourly is defined and repo.keep_hourly != None -%}
--keep-hourly {{ repo.keep_hourly }}
{%- else -%}
--keep-hourly {{ restic_archiver__keep_hourly }}
{%- endif %} \
{% if repo.keep_daily is defined and repo.keep_daily != None -%}
--keep-daily {{ repo.keep_daily }}
{%- else -%}
--keep-daily {{ restic_archiver__keep_daily }}
{%- endif %} \
{% if repo.keep_weekly is defined and repo.keep_weekly != None -%}
--keep-weekly {{ repo.keep_weekly }}
{%- else -%}
--keep-weekly {{ restic_archiver__keep_weekly }}
{%- endif %} \
{% if repo.keep_monthly is defined and repo.keep_monthly != None -%}
--keep-monthly {{ repo.keep_monthly }}
{%- else -%}
--keep-monthly {{ restic_archiver__keep_monthly }}
{%- endif %} \
{% if repo.keep_yearly is defined and repo.keep_yearly != None -%}
--keep-yearly {{ repo.keep_yearly }}
{%- else -%}
--keep-yearly {{ restic_archiver__keep_yearly }}
{%- endif -%}
{% if repo.keep_within is defined and repo.keep_within != None %} \
--keep-within {{ repo.keep_within }} {% endif -%}
2020-12-10 19:44:40 +01:00
{%- if repo.prune|default(false) %} \
--prune {% endif %}
{%- endmacro %}
{% if restic_archiver__mail_report | default(false) %}
# collect info about last snapshot for mail report
restic {{ restic_archiver__default_opt }} stats latest | grep Total
{% endif %}
2020-12-14 12:39:11 +01:00
{% if restic_archiver__mount_required %}
if mountpoint -q {{ restic_archiver__mount_disk }}
then
echo "{{ restic_archiver__mount_disk }} is mounted"
else
mount -a
fi
2020-12-14 12:39:11 +01:00
{% endif %}
2020-12-14 15:08:37 +01:00
set -euxo pipefail
2020-12-09 17:26:16 +01:00
# Settings for Server {{ repo['name'] | string }}
export RESTIC_REPOSITORY="{{ repo['location'] }}"
2020-12-14 16:13:34 +01:00
export RESTIC_PASSWORD='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
2020-12-09 17:26:16 +01:00
BACKUP_NAME="{{ repo.name }}"
restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }}
2020-12-09 17:26:16 +01:00
2021-02-17 17:21:20 +01:00
{% if restic_archiver__mail_report | default(false) %}
cat <<EOT >> /tmp/mailcontent
<h2>{{ repo.name }}</h2>
$(restic {{ restic_archiver__default_opt }} check --quiet 2>/dev/null)
<h4>Latest Snapshots</h4>
<table style="width:100%">
<tr>
<th>Hostname</th>
<th>latest backup</th>
<th>Path</th>
</tr>
$(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('<tr>\n<td>', jsondata['hostname'], '</td>\n<td>', jsondata['time'], '</td>\n<td>', str(jsondata['paths'][0]), '</td>\n</tr>\n')" ; done)
</table>
<table style="width:100%">
<tr>
<th>name</th>
<th>total size</th>
<th>total files</th>
</tr>
<tr>
<td>snapshots</td>
$(restic --quiet stats --json | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('<td>', str(int(jsondata['total_size'] / 1024 / 1024 / 1024 * 1000 )/1000 ), 'GB </td>\n<td>', str(jsondata['total_file_count']), ' Files</td>')")
</tr>
EOT
{% else %}
restic {{ restic_archiver__default_opt }} check
2021-02-17 17:21:20 +01:00
{% endif %}
2020-12-10 19:44:40 +01:00
{% if repo.archive|default(false) %}
# ARCHIVE Settings for Server "{{ repo['name'] | string }}"
export RESTIC_REPOSITORY="{{ repo['archive_location'] }}"
2020-12-14 16:13:34 +01:00
export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
2020-12-10 19:44:40 +01:00
export RESTIC_REPOSITORY2="{{ repo['location'] }}"
2020-12-14 16:13:34 +01:00
export RESTIC_PASSWORD2='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
2020-12-10 19:44:40 +01:00
BACKUP_NAME="{{ repo.name }}_archive"
2020-12-14 12:53:31 +01:00
{% if restic_archiver__mount_required %}
2020-12-14 15:08:37 +01:00
set +euxo pipefail
2020-12-14 12:53:31 +01:00
if mountpoint -q {{ restic_archiver__mount_disk }}
then
echo "{{ restic_archiver__mount_disk }} is mounted"
else
mount -a
fi
2020-12-14 15:08:37 +01:00
set -euxo pipefail
2020-12-14 12:53:31 +01:00
{% endif %}
2021-02-17 17:21:20 +01:00
# 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
2020-12-10 19:44:40 +01:00
# ARCHIVE Settings for Server "{{ repo['name'] | string }}"
export RESTIC_REPOSITORY2="{{ repo['archive_location'] }}"
2020-12-14 16:13:34 +01:00
export RESTIC_PASSWORD2='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
2020-12-10 19:44:40 +01:00
export RESTIC_REPOSITORY="{{ repo['location'] }}"
2020-12-14 16:13:34 +01:00
export RESTIC_PASSWORD='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
2020-12-10 19:44:40 +01:00
# transfer snapshots to archive
restic {{ restic_archiver__default_opt }} copy
2020-12-10 19:44:40 +01:00
2020-12-14 10:26:29 +01:00
{% if repo.archive_cleanup %}
2020-12-10 19:44:40 +01:00
2020-12-14 12:53:31 +01:00
{% if restic_archiver__mount_required %}
2020-12-14 15:08:37 +01:00
set +euxo pipefail
2020-12-14 12:53:31 +01:00
if mountpoint -q {{ restic_archiver__mount_disk }}
then
echo "{{ restic_archiver__mount_disk }} is mounted"
else
mount -a
fi
2020-12-14 15:08:37 +01:00
set -euxo pipefail
2020-12-14 12:53:31 +01:00
{% endif %}
2020-12-14 10:26:29 +01:00
# ARCHIVE CLEANUP Settings for Server "{{ repo['name'] | string }}"
export RESTIC_REPOSITORY="{{ repo['archive_location'] }}"
2020-12-14 16:13:34 +01:00
export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null
2020-12-14 10:26:29 +01:00
BACKUP_NAME="{{ repo.name }}_archive"
2020-12-10 19:44:40 +01:00
restic {{ restic_archiver__default_opt }} forget {{ retention_pattern(repo) }}
2020-12-10 19:44:40 +01:00
{% endif %}
2021-02-17 17:21:20 +01:00
{% if restic_archiver__mail_report | default(false) %}
restic --quiet stats --json | python3 -c "import sys, json; jsondata=json.load(sys.stdin); print('<tr>\n<td>external_archive</td>\n<td>', str(int(jsondata['total_size'] / 1024 / 1024 / 1024 * 1000 )/1000 ), 'GB</td>\n<td> ', str(jsondata['total_file_count']), ' Files.</td>\n</tr>\n')" >> /tmp/mailcontent
2020-12-10 19:44:40 +01:00
{% endif %}
2021-02-17 17:21:20 +01:00
echo -e "</table>\n<br/><br/>" >> /tmp/mailcontent
restic {{ restic_archiver__default_opt }} check --quiet >> /tmp/mailcontent
{% else %}
restic {{ restic_archiver__default_opt }} check
2020-12-09 17:26:16 +01:00
2021-02-17 17:21:20 +01:00
{% endif %}
2020-12-09 17:26:16 +01:00
{% endfor %}
2020-12-10 19:44:40 +01:00
sync
2020-12-14 15:08:37 +01:00
set +euxo pipefail
2021-02-17 17:21:20 +01:00
cat <<EOT >> /tmp/mailcontent
<br/><br/><br/>
<br/><br/><br/>
<br/><br/><br/>
<code>
<pre>
$(df -h)
</pre>
</code>
</body>
</html>
EOT
mail -a "Content-type: text/html" -s "restic backup report" {{ restic_archiver__mailaddress }} < /tmp/mailcontent
{% if restic_archiver__umount_after_usage %}
2020-12-14 12:53:31 +01:00
umount {{ restic_archiver__mount_disk }}
2020-12-14 12:39:11 +01:00
{% endif %}