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

94 lines
2.4 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
2023-12-04 22:34:29 +01:00
backup_name="{{ name }}"
2021-05-15 13:05:18 +02:00
2023-12-04 22:34:29 +01:00
{% if params.mountpoint is defined -%}
export MOUNTPOINT="{{ params.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 %}
2023-12-04 22:34:29 +01:00
repo_path={{ params.sshrepo|default('localhost::tmp') }}
2021-05-15 13:05:18 +02:00
{%- endif %}
2020-09-22 20:22:09 +02:00
2021-05-15 13:05:18 +02:00
export BORG_REPO=$repo_path/${backup_name}
2023-12-04 22:34:29 +01:00
export BORG_PASSPHRASE='{{ params.password }}'
2020-09-22 20:22:09 +02:00
2023-11-24 19:33:02 +01:00
if [[ $1 == "list" ]]
then
borg list
exit
fi
2020-09-22 20:22:09 +02:00
info "Starting backup"
info "Init repository backup"
2023-12-04 22:34:29 +01:00
borg init --encryption={{ params.encryption|default('repokey-blake2') }}
2020-09-22 20:22:09 +02:00
2021-12-05 18:31:50 +01:00
info "Starting before tasks"
2023-12-04 22:34:29 +01:00
{%- for task in params.before_tasks|default([]) %}
2021-12-05 18:31:50 +01:00
{{ task }}
2023-09-14 16:47:21 +02:00
if [ $? -ne 0 ]; then echo error; fi
2021-12-05 18:31:50 +01:00
{%- 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 \
2023-12-04 22:34:29 +01:00
--compression {{ params.compression|default('lz4') }} \
2023-08-30 13:57:53 +02:00
--exclude-if-present '.nobackup' \
2023-12-04 22:34:29 +01:00
{%- if params.excluded_dirs is defined -%}
{%- for exclude in params.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}' \
2023-12-04 22:34:29 +01:00
{%- for include in params.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 \
2023-12-04 22:34:29 +01:00
--keep-daily {{ params.keep_daily|default(7) }} \
--keep-weekly {{ params.keep_weekly|default(4) }} \
--keep-monthly {{ params.keep_monthly|default(6) }} \
2020-09-22 20:22:09 +02:00
2021-09-25 16:08:25 +02:00
info "Cleaning up logs"
2023-12-04 22:34:29 +01:00
find {{ log_dir }}/${backup_name}-*.log -mtime +{{ params.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"
2023-12-04 22:34:29 +01:00
{%- for task in params.after_tasks|default([]) %}
2021-12-05 18:31:50 +01:00
{{ 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}