ansible_role_restic/templates/restic_script_Linux.j2

264 lines
9.9 KiB
Text
Raw Normal View History

2020-05-01 10:56:07 +02:00
#!/usr/bin/env bash
# {{ ansible_managed }}
# Backup script for {{ item.src|default('stdin') }}
2019-09-11 11:31:51 +02:00
# Use this file to create a Backup and prune existing data with one execution.
pid="/var/run/restic_backup_{{ item.name | regex_replace('\'', '\'\\\'\'') }}.pid"
trap "rm -f $pid" SIGSEGV
trap "rm -f $pid" SIGINT
if [ -e $pid ]; then
2022-01-10 11:17:20 +01:00
echo "A nother version of this restic backup script is already running!"
{% if item.mail_on_error is defined and item.mail_on_error == true %}
mail -s "starting restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Another restic backup process is already running. We canceled starting a new restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S').
{%- if item.src is defined -%}
{{ ' ' }}We tried to backup '{{ item.src }}'.
{%- endif -%}
{{ ' ' }}Please repair the restic-{{ item.name | replace(' ', '') }} job."
{% endif %}
exit # pid file exists, another instance is running, so now we politely exit
else
echo $$ > $pid # pid file doesn't exit, create one and go on
fi
# your normal workflow here...
{% if item.disable_logging is defined and item.disable_logging %}
{% set backup_result_log, backup_output_log = "/dev/null", "/dev/null" %}
{% set forget_result_log, forget_output_log = "/dev/null", "/dev/null" %}
{% else %}
2021-10-19 16:38:17 +02:00
{% if (item.log_to_journald is defined and item.log_to_journald) %}
2021-10-20 10:38:01 +02:00
{% set backup_result_log, backup_output_log = "| systemd-cat -t " + item.name, "2>&1 | systemd-cat -t " + item.name %}
{% set forget_result_log, forget_output_log = "| systemd-cat -t " + item.name, "2>&1 | systemd-cat -t " + item.name %}
2021-10-19 16:38:17 +02:00
{% else %}
{% set backup_result_log, backup_output_log = ">> " + restic_log_dir + "/" + item.name + "-backup-result.log", "| tee " + restic_log_dir + "/" + item.name + "-backup-output.log" %}
{% set forget_result_log, forget_output_log = ">> " + restic_log_dir + "/" + item.name + "-forget-result.log", "| tee " + restic_log_dir + "/" + item.name + "-forget-output.log" %}
{% endif %}
{% endif %}
2022-01-18 17:20:02 +01:00
{% if restic__cache_config | bool -%}
export XDG_CACHE_HOME={{ restic__cache_dir }}
{% endif %}
export RESTIC_REPOSITORY={{ restic_repos[item.repo].location }}
2020-06-03 08:52:02 +02:00
export RESTIC_PASSWORD='{{ restic_repos[item.repo].password | regex_replace('\'', '\'\\\'\'') }}'
BACKUP_NAME={{ item.name }}
2019-12-17 14:19:25 +01:00
{% if restic_repos[item.repo].aws_access_key is defined %}
2020-05-24 15:13:26 +02:00
export AWS_ACCESS_KEY_ID={{ restic_repos[item.repo].aws_access_key }}
2019-12-17 14:19:25 +01:00
{% endif %}
{% if restic_repos[item.repo].aws_secret_access_key is defined %}
2020-06-04 19:22:47 +02:00
export AWS_SECRET_ACCESS_KEY='{{ restic_repos[item.repo].aws_secret_access_key | regex_replace('\'', '\'\\\'\'') }}'
2019-12-17 14:19:25 +01:00
{% endif %}
2020-05-24 13:56:52 +02:00
{% if restic_repos[item.repo].aws_default_region is defined %}
2020-05-24 15:13:26 +02:00
export AWS_DEFAULT_REGION={{ restic_repos[item.repo].aws_default_region }}
2020-05-24 13:56:52 +02:00
{% endif %}
{% if restic_repos[item.repo].b2_account_id is defined %}
export B2_ACCOUNT_ID={{ restic_repos[item.repo].b2_account_id }}
{% endif %}
{% if restic_repos[item.repo].b2_account_key is defined %}
export B2_ACCOUNT_KEY={{ restic_repos[item.repo].b2_account_key }}
{% endif %}
{% if item.src is defined %}
BACKUP_SOURCE={{ item.src }}
{% endif %}
2021-07-05 14:51:27 +02:00
{% if item.lvm is defined %}
# Set up functions for LVM.
function mount_opt_map {
mount_type="$1"
case "$mount_type" in
xfs)
echo "noatime,nouuid"
;;
ext4)
echo "noatime"
;;
*)
echo "noatime"
esac
}
function prepare_vol {
local path="$1"
[ -d "$path" ] || path="$(dirname "$path")"
# TODO: path cannot be /,
## nor can it be where restic is
{
local source="$(findmnt -J -T ${path} | jq -r '.filesystems[0].source')"
local target="$(findmnt -J -T ${path} | jq -r '.filesystems[0].target')"
subdir=${path##$target}
echo "Creating snapshot ..."
lvcreate -y -L "${size:-10G}" -s -n "${source}_snap" "${source}"
local tmpdir="$(mktemp -d)"
local fs="$(lsblk -J --fs "$source" | jq -r '.blockdevices[0]|.fstype')"
echo "Identified fstype: $fs; using opts $(mount_opt_map "$fs") ..."
mount -t "$fs" \
-o "$(mount_opt_map "$fs")" \
--make-private \
-m \
"${source}_snap" "${tmpdir}"
mount -m --bind --make-private "${tmpdir}/${subdir}" "${path}"
}
}
function cleanup_vol {
local path="$1"
[ -d "$path" ] || path="$(dirname "$path")"
{
local source="$(findmnt -v -f -J -T ${path} | jq -r '.filesystems[0].source')"
echo "Cleaning up mount ..."
umount "${path}"
echo "Cleaning up snapshot ..."
if ! grep -q '_snap$' <<< $source; then
echo "Snapshot for ${path} could not be found (found: ${source}). Exiting!" && return 1;
fi
umount "${source}"
lvremove -y "${source}";
}
}
{% endif %}
2021-07-05 14:51:27 +02:00
set -uxo pipefail
{#
Define Tags
#}
{% macro tags(tags) -%}
2019-09-11 11:29:36 +02:00
{% if tags is defined and (tags|length>0) %}{% for tag in tags %} --tag {{ tag }}{% endfor %}{% endif %}
{%- endmacro %}
{#
Define Keeped Tags
#}
{% macro keep_tags(tags) -%}
{% if tags is defined and (tags|length>0) %}{% for tag in tags %} --keep-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) -%}
2019-09-11 15:58:57 +02:00
{% if repo.src is defined and repo.src != None and (repo.src|length>0) %}{{ repo.src }}{% else %}{{ repo.stdin_filename }}{% endif %}
{%- endmacro %}
{#
Define retention pattern
#}
{% macro retention_pattern(repo) -%}
2019-09-11 11:29:36 +02:00
{% if repo.keep_last is defined and repo.keep_last != None %}--keep-last {{ item.keep_last }}{% endif %} \
{% if repo.keep_hourly is defined and repo.keep_hourly != None %}--keep-hourly {{ item.keep_hourly }}{% endif %} \
{% if repo.keep_daily is defined and repo.keep_daily != None %}--keep-daily {{ item.keep_daily }}{% endif %} \
{% if repo.keep_weekly is defined and repo.keep_weekly != None %}--keep-weekly {{ item.keep_weekly }}{% endif %} \
{% if repo.keep_monthly is defined and repo.keep_monthly != None %}--keep-monthly {{ item.keep_monthly }}{% endif %} \
{% if repo.keep_yearly is defined and repo.keep_yearly != None %}--keep-yearly {{ item.keep_yearly }}{% endif %} \
{% if repo.keep_within is defined and repo.keep_within != None %}--keep-within {{ item.keep_within }}{% endif %} \
{% if repo.keep_tag is defined and (repo.keep_tag|length>0) %}{{ keep_tags(repo.keep_tag) }}{% endif %}
{%- endmacro %}
2020-06-05 09:01:36 +02:00
{% macro exclude(exclude) %}
{% if exclude.exclude_cache is defined and exclude.exclude_cache == true %}
--exclude-cache \
{% endif %}
{% if exclude.exclude is defined %}
{% for path in exclude.exclude %}
--exclude {{ path }} \
{% endfor %}
{% endif %}
{% if exclude.iexclude is defined %}
{% for path in exclude.iexclude %}
--iexclude {{ path }} \
{% endfor %}
{% endif %}
{% if exclude.exclude_file is defined %}
{% for path in exclude.exclude_file %}
--exclude-file {{ path }} \
{% endfor %}
{% endif %}
{% if exclude.exclude_if_present is defined %}
{% for path in exclude.exclude_if_present %}
--exclude-if-present {{ path }} \
{% endfor %}
{% 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
{% if item.stdin is defined and item.stdin == true %}
{{ item.stdin_cmd }} | {{ restic_install_path }}/restic backup \
--stdin $MODE_TAG \
{{ tags(item.tags) }} \
{{ stdin_filename(item.stdin_filename) }} \
{% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \
$@ \
{% else %}
{
{% if item.lvm is defined %}prepare_vol $BACKUP_SOURCE &&{% endif %}
2020-06-05 09:01:36 +02:00
{{ restic_install_path }}/restic backup $BACKUP_SOURCE $MODE_TAG \
{{ tags(item.tags) }} \
{% if item.exclude is defined %}{{ exclude(item.exclude) }}{% endif %} \
$@ \
{% if item.lvm is defined %}&& cleanup_vol $BACKUP_SOURCE{% endif %};
} \
2021-10-19 16:38:17 +02:00
{% endif %} {{ backup_output_log }}
if [[ $? -eq 0 ]]
then
2021-10-19 16:38:17 +02:00
echo "$(date -u '+%Y-%m-%d %H:%M:%S') OK" {{ backup_result_log }}
else
2021-10-19 16:38:17 +02:00
echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" {{ backup_result_log }}
{% if item.mail_on_error is defined and item.mail_on_error == true %}
mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S').
{%- if item.src is defined -%}
{{ ' ' }}We tried to backup '{{ item.src }}'.
{%- endif -%}
{{ ' ' }}Please repair the restic-{{ item.name | replace(' ', '') }} job."
{% if item.lvm is defined %}
cleanup_vol $BACKUP_SOURCE
{% endif %}
{% endif %}
fi
2021-07-05 14:51:27 +02:00
{#
Define stdin forget commands
#}
2021-10-19 16:38:17 +02:00
{{ restic_install_path }}/restic forget --path {{ path(item) }} {{ retention_pattern(item) }} {% if item.prune is defined and item.prune == true %}--prune{% endif %} {{ forget_output_log }}
if [[ $? -eq 0 ]]
then
2021-10-19 16:38:17 +02:00
echo "$(date -u '+%Y-%m-%d %H:%M:%S') OK" {{ forget_result_log }}
2022-07-12 21:25:34 +02:00
{% if item.monitoring_call is defined %}
{{ item.monitoring_call }}
{% endif %}
else
2021-10-19 16:38:17 +02:00
echo "$(date -u '+%Y-%m-%d %H:%M:%S') ERROR" {{ forget_result_log }}
2021-07-05 14:51:27 +02:00
{% if item.mail_on_error is defined and item.mail_on_error == true %}
2021-07-05 16:18:46 +02:00
mail -s "restic backup failed on {{ ansible_hostname }}" {{ item.mail_address }} <<< "Something went wrong while running restic backup script running at {{ ansible_hostname }} at $(date -u '+%Y-%m-%d %H:%M:%S').
{%- if item.src is defined -%}
{{ ' ' }}We tried to backup '{{ item.src }}'.
{%- endif -%}
{{ ' ' }}Please repair the restic-{{ item.name | replace(' ', '') }} job."
2021-07-05 14:51:27 +02:00
{% endif %}
fi
rm -f $pid # remove pid file just before exiting
exit