From 015d25297f5bfc7db649d5ca599f9ccbd1a0c461 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Tue, 22 Sep 2020 20:22:09 +0200 Subject: [PATCH] added template to borg state --- states/borg/templates/borg_job.sh.j2 | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 states/borg/templates/borg_job.sh.j2 diff --git a/states/borg/templates/borg_job.sh.j2 b/states/borg/templates/borg_job.sh.j2 new file mode 100755 index 0000000..4f73ebd --- /dev/null +++ b/states/borg/templates/borg_job.sh.j2 @@ -0,0 +1,63 @@ +#!/bin/sh +export MOUNTPOINT="{{ job.mountpoint }}" + +if [[ $(mountpoint -q $MOUNTPOINT) -eq 0 ]] +then + exit 2 +fi + +backup_name="{{ job.name }}" + +export BORG_REPO=${MOUNTPOINT}/borg/${backup_name} +export BORG_PASSPHRASE='{{ job.password }}' + +info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; } +trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM + +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 -%} + 2>> /opt/borgbackup/logs/${backup_name}-$(date +%Y-%m-%d).log + +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" + #echo "Borg backup success for $backup_name job, proof file restore succeeded" | mutt -F {{ borgbackup_install_dir }}/.muttrc -s "BACKUP $backup_name SUCCESS" {{ job.value.notify_addr|default(borgbackup_mail_notify_addr) }} +elif [ ${global_exit} -eq 1 ]; then + info "Backup and/or Prune finished with warnings" + #echo "Borg backup warning for $backup_name job, need for manual check of job for errors" | mutt -F {{ borgbackup_install_dir }}/.muttrc -s "BACKUP $backup_name WARNING" {{ job.value.notify_addr|default(borgbackup_mail_notify_addr) }} +else + info "Backup and/or Prune finished with errors" + #echo "Borg backup failed for $backup_name job, need for manual check of job for errors" | mutt -F {{ borgbackup_install_dir }}/.muttrc -s "BACKUP $backup_name FAILED" {{ job.value.notify_addr|default(borgbackup_mail_notify_addr) }} +fi + +exit ${global_exit}