Skip to content

Instantly share code, notes, and snippets.

@quantenschaum
Created April 15, 2018 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantenschaum/f9a9ce3525191b5ee5fbb9bcb233a2eb to your computer and use it in GitHub Desktop.
Save quantenschaum/f9a9ce3525191b5ee5fbb9bcb233a2eb to your computer and use it in GitHub Desktop.
borg backup cron script
#!/bin/bash -e
if [[ $# < 2 ]]; then
echo "usage: backup REPO check | list | PREFIX SRC [--force] [borg_opts]"
exit
fi
if [[ "$USER" != "root" ]]; then
exec sudo -H systemd-inhibit "$0" "$@"
fi
export LANGUAGE=C
export LC_ALL=C.UTF-8
export BORG_REPO="$1"
shift
if [[ "$1" = "check" ]]; then
shift
borg check $*
exit
fi
if [[ "$1" = "list" ]]; then
shift
borg list $*
exit
fi
PREFIX=$1
SRC=$2
shift 2
SNAP=$SRC/backup
REPOHASH=$(echo "$SRC $BORG_REPO" |md5sum |cut -d" " -f1)
MARKER="$HOME/.cache/borg/$REPOHASH.marker"
if [[ "$1" = "--force" ]]; then
shift
else
on_ac_power || exit 1
[[ ! -f $MARKER || "$(find $MARKER -mmin +240)" ]] || exit 2
fi
borg with-lock :: true
test ! -d $SNAP || btrfs su de $SNAP
btrfs su sn -r $SRC $SNAP
[ ! -t 1 ] || P="--progress"
cd $SNAP
borg info
borg create --stats --exclude-caches --exclude-if-present .nobackup --keep-exclude-tags $P $* ::$PREFIX-{now} .
borg prune --stats --prefix $PREFIX --keep-last 3 --keep-within 7d --keep-daily 14 --keep-weekly 4 --keep-monthly 30
borg list
cd $SRC
btrfs su de $SNAP
echo "$SRC $BORG_REPO $(date -Is)" >$MARKER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment