2019-08-14 16:00:22 +02:00
|
|
|
# {{ ansible_managed }}
|
|
|
|
# Backup script for {{ item.src|default('stdin') }}
|
|
|
|
# Use this file to create a Backup and prune existing data.
|
|
|
|
set -euxo pipefail
|
|
|
|
|
|
|
|
export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }}
|
|
|
|
export RESTIC_PASSWORD={{ restic_repos[item.repo].password }}
|
|
|
|
BACKUP_NAME={{ item.name }}
|
|
|
|
{% if item.src is defined %}
|
|
|
|
BACKUP_SOURCE={{ item.src }}
|
|
|
|
{% endif %}
|
|
|
|
{#
|
|
|
|
Define Tags
|
|
|
|
#}
|
|
|
|
{% macro tags(tags) -%}
|
|
|
|
{% if tags is defined %}{% for tag in tags %} --tag {{ tag }}{% endfor %}{% endif %}
|
|
|
|
{%- endmacro %}
|
|
|
|
{#
|
|
|
|
Define Hostname
|
|
|
|
#}
|
|
|
|
{% macro hostname(h) -%}
|
|
|
|
{% if h is defined %} --hostname {{ h }}{% endif %}
|
|
|
|
{%- endmacro %}
|
|
|
|
{#
|
|
|
|
Define stdin filename
|
|
|
|
#}
|
|
|
|
{% macro stdin_filename(n) -%}
|
|
|
|
{% if n is defined %} --stdin-filename {{ n }}{% endif %}
|
|
|
|
{%- endmacro %}
|
|
|
|
{#
|
|
|
|
Define path
|
|
|
|
#}
|
|
|
|
{% macro path(repo) -%}
|
|
|
|
{% if repo.src is defined %}{{ repo.src }}{% else %}$HOME/{{ repo.stdin_filename }}{% endif %}
|
|
|
|
{%- endmacro %}
|
|
|
|
{#
|
|
|
|
Define retention pattern
|
|
|
|
#}
|
|
|
|
{% macro retention_pattern(repo) -%}
|
|
|
|
{% if repo.keep_last is defined %}--keep-last {{ item.keep_last }}{% endif %} \
|
|
|
|
{% if repo.keep_hourly is defined %}--keep-hourly {{ item.keep_hourly }}{% endif %} \
|
|
|
|
{% if repo.keep_daily is defined %}--keep-daily {{ item.keep_daily }}{% endif %} \
|
|
|
|
{% if repo.keep_weekly is defined %}--keep-weekly {{ item.keep_weekly }}{% endif %} \
|
|
|
|
{% if repo.keep_monthly is defined %}--keep-monthly {{ item.keep_monthly }}{% endif %} \
|
|
|
|
{% if repo.keep_yearly is defined %}--keep-yearly {{ item.keep_yearly }}{% endif %} \
|
|
|
|
{% if repo.keep_within is defined %}--keep-within {{ item.keep_within }}{% endif %} \
|
|
|
|
{% if repo.keep_tag is defined %}--keep-tag {{ item.keep_tag }}{% endif %}
|
|
|
|
{%- endmacro %}
|
|
|
|
{#
|
|
|
|
Define backup commands
|
|
|
|
#}
|
2019-08-14 16:16:16 +02:00
|
|
|
if [[ -z ${CRON+x} ]]; then
|
|
|
|
MODE_TAG="--tag manual"
|
|
|
|
else
|
|
|
|
MODE_TAG="--tag cron"
|
|
|
|
fi
|
2019-08-14 16:00:22 +02:00
|
|
|
{% if item.stdin is defined and item.stdin == true %}
|
2019-08-14 16:16:16 +02:00
|
|
|
{{ item.stdin_cmd }} | {{ restic_install_path }}/restic backup --stdin $MODE_TAG {{ tags(item.tags) }} {{ stdin_filename(item.stdin_filename) }} $@
|
2019-08-14 16:00:22 +02:00
|
|
|
{% else %}
|
2019-08-14 16:16:16 +02:00
|
|
|
{{ restic_install_path }}/restic backup $BACKUP_SOURCE $MODE_TAG {{ tags(item.tags) }} $@
|
2019-08-14 16:00:22 +02:00
|
|
|
{% endif %}
|
|
|
|
{#
|
|
|
|
Define stdin forget commands
|
|
|
|
#}
|
2019-08-14 16:16:16 +02:00
|
|
|
{{ restic_install_path }}/restic forget --path {{ path(item) }} {{ retention_pattern(item) }}
|