#!/usr/bin/env bash # {{ ansible_managed }} # This file is to cleanup your backup archive and move some snapshots to a external storage. {% for repo in restic_archiver__repos %} {# define macro: how fast should we delete snapshots? Variables are defined via defaults! #} {% 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 -%} {%- if repo.prune|default(false) %} \ --prune {% endif %} {%- endmacro %} {% if restic_archiver__mount_required %} if mountpoint -q {{ restic_archiver__mount_disk }} then echo "{{ restic_archiver__mount_disk }} is mounted" else mount -a fi {% endif %} set -euxo pipefail # Settings for Server {{ repo['name'] | string }} export RESTIC_REPOSITORY="{{ repo['location'] }}" export RESTIC_PASSWORD='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null BACKUP_NAME="{{ repo.name }}" restic forget {{ retention_pattern(repo) }} restic check {% if repo.archive|default(false) %} # ARCHIVE Settings for Server "{{ repo['name'] | string }}" export RESTIC_REPOSITORY="{{ repo['archive_location'] }}" export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null 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 init --copy-chunker-params fi {% if restic_archiver__mount_required %} set +euxo pipefail if mountpoint -q {{ restic_archiver__mount_disk }} then echo "{{ restic_archiver__mount_disk }} is mounted" else mount -a fi set -euxo pipefail {% endif %} # ARCHIVE Settings for Server "{{ repo['name'] | string }}" export RESTIC_REPOSITORY2="{{ repo['archive_location'] }}" export RESTIC_PASSWORD2='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null export RESTIC_REPOSITORY="{{ repo['location'] }}" export RESTIC_PASSWORD='{{ repo['password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null # transfer snapshots to archive restic copy {% if repo.archive_cleanup %} {% if restic_archiver__mount_required %} set +euxo pipefail if mountpoint -q {{ restic_archiver__mount_disk }} then echo "{{ restic_archiver__mount_disk }} is mounted" else mount -a fi set -euxo pipefail {% endif %} # ARCHIVE CLEANUP Settings for Server "{{ repo['name'] | string }}" export RESTIC_REPOSITORY="{{ repo['archive_location'] }}" export RESTIC_PASSWORD='{{ repo['archive_password'] | regex_replace('\'', '\'\\\'\'') }}' 2>/dev/null BACKUP_NAME="{{ repo.name }}_archive" restic forget {{ retention_pattern(repo) }} {% endif %} restic check {% endif %} {% endfor %} sync set +euxo pipefail {% if restic_archiver__umount_after_usage %} umount {{ restic_archiver__mount_disk }} {% endif %}