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

73 lines
2.1 KiB
Plaintext
Raw Normal View History

2020-09-22 21:09:05 +02:00
#!/bin/bash
2021-05-15 13:05:18 +02:00
2020-09-22 21:09:05 +02:00
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
2021-05-15 13:05:18 +02:00
backup_name="{{ key }}"
{% if job.mountpoint is defined -%}
2020-09-22 21:09:05 +02:00
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
2021-05-15 13:05:18 +02:00
repo_path=${MOUNTPOINT}/borg
{% else %}
repo_path={{ job.sshrepo|default('localhost::tmp') }}
{%- endif %}
2020-09-22 20:22:09 +02:00
2021-05-15 13:05:18 +02:00
export BORG_REPO=$repo_path/${backup_name}
2020-09-22 20:22:09 +02:00
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') }} \
2020-10-01 23:07:04 +02:00
{%- if job.excluded_dirs is defined -%}
{%- for exclude in job.excluded_dirs %}
--exclude '{{ exclude }}' \
{%- endfor %}
{%- endif %}
2020-09-22 20:22:09 +02:00
--exclude-caches \
2020-10-01 23:07:04 +02:00
::$backup_name'-{hostname}-{now}' \
{%- for include in job.included_dirs %}
2020-09-22 20:22:09 +02:00
{{ include }} \
2020-10-01 23:07:04 +02:00
{%- 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
2021-08-19 21:15:24 +02:00
exit ${global_exit}