Commit c603eab5 authored by Fjen Undso's avatar Fjen Undso
Browse files

initial commit

parents
#!/bin/bash
set -euo pipefail
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
# load config
source ./borgbackup.config.sh
# borg backup
info "Starting backup"
TMPFILE1="$(tempfile)"
borg create \
--verbose \
--stats \
--compression "$COMPRESS" \
$BORG_PARAMS \
'::{hostname}-{now}' \
$BORG_PATHS \
2>&1 | tee "$TMPFILE1"
backup_exit=$?
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 12 monthly
# archives of THIS machine. --prefix `hostname`- is very important to
# limit prune's operation to this machine's archives and not apply to
# other machine's archives also.
info "Pruning repository"
TMPFILE2="$(tempfile)"
borg prune \
--prefix '{hostname}-' \
--verbose \
--list \
--show-rc \
--keep-daily 30 \
--keep-weekly 4 \
--keep-monthly 12 \
2>&1 | tee "$TMPFILE2"
prune_exit=$?
# Dump the exit codes to /var/log. Icinga2 checks the location to examine the backup results
log_dir="/var/log/borg"
mkdir -p "$log_dir" || (info "Failed to \`mkdir -p $log_dir\`" && global_exit=1)
echo -e "create: $backup_exit\nprune: $prune_exit" > "$log_dir/borg-rc.log"
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
# error mail sending
if [ $global_exit != 0 ]; then
/usr/bin/logger -t backup "Exited abnormally with [$backup_exit][$prune_exit]"
MAIL="# borg create:\n"
MAIL+="$(cat '$TMPFILE1')\n\n"
MAIL+="# borg prune:\n"
MAIL+="$(cat '$TMPFILE2')\n\n"
MAIL+="# borg list:\n"
MAIL+="$(borg list)\n\n"
MAIL+= "Cheers,\n$HOSTNAME"
echo -e "$MAIL" | mail -a "Content-Type: text/plain; charset=UTF-8" -s "$HOSTNAME: borgbackup failed!" "$ADMINMAIL"
fi
rm -f "$TMPFILE1" "$TMPFILE2"
if [ ${global_exit} -eq 1 ];
then
info "Backup and/or Prune finished with a warning"
fi
if [ ${global_exit} -gt 1 ];
then
info "Backup and/or Prune finished with an error"
fi
exit ${global_exit}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment