infrastructure/ansible/plays/templates/restore.sh.j2

53 lines
1.2 KiB
Plaintext
Raw Normal View History

2022-07-14 00:19:54 +02:00
#!/bin/sh
# Setting this, so the repo does not need to be given on the commandline:
# See the section "Passphrase notes" for more infos.
2023-12-25 17:45:59 +01:00
export BORG_PASSPHRASE='{{ password }}'
export BORG_REPO='{{ repo }}'
export BORG_RSH='ssh -i /root/.ssh/borgbackup'
2022-07-14 00:19:54 +02:00
{% if provision.from_backup.name is defined %}
RESTORE_FROM={{ provision.from_backup.name }}
{% else %}
RESTORE_FROM=$(borg list --short --sort-by timestamp --last 1 --glob-archives "{hostname}*")
2022-07-14 00:19:54 +02:00
{% endif %}
{% raw %}
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
# Backup the most important directories into an archive named after
# the machine this script is currently running on:
# Starting restore
info "Starting Restore from backup"
cd /
borg extract \
--list \
"::${RESTORE_FROM}"
restore_exit=$?
# use highest exit code as global exit code
global_exit=$restore_exit
if [ ${global_exit} -eq 0 ]; then
info "Restore finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Restore finished with warnings"
else
info "Restore finished with errors"
fi
exit ${global_exit}
{% endraw %}