Skip to content

Instantly share code, notes, and snippets.

@esamson
Created December 2, 2019 21:23
Show Gist options
  • Save esamson/42b36d3e23e5210bdb2d61649b32f7a8 to your computer and use it in GitHub Desktop.
Save esamson/42b36d3e23e5210bdb2d61649b32f7a8 to your computer and use it in GitHub Desktop.
borg backup script
#!/bin/sh
#
# Daily backup job.
# Belongs in /etc/cron.daily/
# Back up /home partition to /media/backup partition.
SRC=/home/
DEST=/media/backup/bomuhei/
LOG=/media/backup/last_backup.bomuhei
date > $LOG
rsync --archive \
--verbose \
--human-readable \
--delete \
--delete-excluded \
--exclude '.Trash/' \
--exclude '.cache/' \
--exclude '.gradle/' \
--exclude '.ivy/' \
--exclude '.m2/' \
--exclude '.mozilla/' \
--exclude 'sonatype-work/' \
$SRC $DEST 2>&1 >> $LOG
date >> $LOG
# Back up to Borg repository
export BORG_REPO=/media/expansion/bomuhei
export BORG_PASSPHRASE='real_password_goes_here'
BORG_LOG=/media/expansion/last_backup.bomuhei
# some helpers and error handling:
info() { printf "\n%s %s\n\n" "$( date )" "$*" >> $BORG_LOG; }
trap 'echo $( date ) Backup interrupted >&2; exit 2' INT TERM
date > $BORG_LOG
borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression zstd,3 \
::'{hostname}-{now}' \
/media/backup/bomuhei >> $BORG_LOG 2>&1
backup_exit=$?
info "Pruning repository"
borg prune \
--list \
--prefix '{hostname}-' \
--show-rc \
--keep-daily 8 \
--keep-weekly 5 \
--keep-monthly 13 \
--keep-yearly 10 >> $BORG_LOG 2>&1
prune_exit=$?
# use highest exit code as global exit code
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
exit ${global_exit}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment