paulbsd-salt/states/borg/templates/borg_job.sh.j2

62 lines
1.8 KiB
Plaintext
Raw Normal View History

2020-09-22 21:09:05 +02:00
#!/bin/bash
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
export MOUNTPOINT="{{ job.mountpoint|default('/mnt') }}"
2020-09-22 20:22:09 +02:00
2020-09-22 21:09:05 +02:00
if [[ $(mountpoint -q $MOUNTPOINT) -ne 0 ]]
2020-09-22 20:22:09 +02:00
then
2020-09-22 21:09:05 +02:00
info "Mountpoint not exists, exiting backup"
2020-09-22 20:22:09 +02:00
exit 2
fi
2020-09-22 21:09:05 +02:00
backup_name="{{ key }}"
2020-09-22 20:22:09 +02:00
export BORG_REPO=${MOUNTPOINT}/borg/${backup_name}
export BORG_PASSPHRASE='{{ job.password }}'
info "Starting backup"
info "Init repository backup"
borg init --encryption={{ job.encryption|default('repokey-blake2') }}
info "Creating new archive"
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression {{ job.compression|default('lz4') }} \
--exclude-caches \
::$backup_name'-{hostname}-{now}' \
{% for include in job.included_dirs -%}
{{ include }} \
{% endfor -%}
2020-09-22 21:09:05 +02:00
2>> {{ working_dir }}/logs/${backup_name}-$(date +%Y-%m-%d).log
2020-09-22 20:22:09 +02:00
backup_exit=$?
info "Pruning repository"
borg prune \
--list \
--prefix $backup_name'-' \
--show-rc \
--keep-daily {{ job.keep_daily|default(7) }} \
--keep-weekly {{ job.keep_weekly|default(4) }} \
--keep-monthly {{ job.keep_monthly|default(6) }} \
prune_exit=$?
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
else
info "Backup and/or Prune finished with errors"
fi
exit ${global_exit}