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

87 lines
2.2 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') }}
2021-12-05 18:31:50 +01:00
info "Starting before tasks"
{%- for task in job.before_tasks|default([]) %}
{{ task }}
{%- endfor %}
2020-09-22 20:22:09 +02:00
info "Creating new archive"
2023-07-02 21:32:58 +02:00
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
2020-09-22 20:22:09 +02:00
--compression {{ job.compression|default('lz4') }} \
2023-08-30 13:57:53 +02:00
--exclude-if-present '.nobackup' \
2020-10-01 23:07:04 +02:00
{%- if job.excluded_dirs is defined -%}
{%- for exclude in job.excluded_dirs %}
2023-07-02 21:32:58 +02:00
--exclude '{{ exclude }}' \
2020-10-01 23:07:04 +02:00
{%- 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 %}
2>> {{ log_dir }}/${backup_name}-$(date +%Y-%m-%d).log
2020-09-22 20:22:09 +02:00
backup_exit=$?
info "Pruning repository"
2023-07-02 21:32:58 +02:00
borg prune \
--list \
-a $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) }} \
2020-09-22 20:22:09 +02:00
2021-09-25 16:08:25 +02:00
info "Cleaning up logs"
find {{ log_dir }}/${backup_name}-*.log -mtime +{{ job.keep_logs_days|default(7) }} -delete
2021-09-25 16:08:25 +02:00
2021-12-05 18:31:50 +01:00
info "Starting after tasks"
{%- for task in job.after_tasks|default([]) %}
{{ task }}
{%- endfor %}
2020-09-22 20:22:09 +02:00
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}