From e0ca054f3edf89caa1560c44bd5e8fc08107edf5 Mon Sep 17 00:00:00 2001 From: hkbakke Date: Thu, 22 Dec 2016 17:59:19 +0100 Subject: [PATCH] Add initial source --- src/borgwrapper.sh | 67 +++++++++++++++++++++++++++++++++++++++++++ src/config.sh.example | 20 +++++++++++++ 2 files changed, 87 insertions(+) create mode 100755 src/borgwrapper.sh create mode 100644 src/config.sh.example diff --git a/src/borgwrapper.sh b/src/borgwrapper.sh new file mode 100755 index 0000000..5f0d2a7 --- /dev/null +++ b/src/borgwrapper.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +MODE="$1" +CONFIG="/etc/borgwrapper/config.sh" + + +print_usage () { + echo "Usage: borgwrapper.sh MODE" + echo "" + echo "arguments:" + echo " MODE backup|verify|unlock" +} + +borg_backup () { + EXCLUDE_CMD=() + + for EXCLUDE in ${EXCLUDES[@]}; do + EXCLUDE_CMD+=( --exclude "$EXCLUDE" ) + done + + # Backup all of /home and /var/www except a few + # excluded directories + $BORG create --info --stats \ + --compression lz4 \ + --numeric-owner \ + "${REPO}"::"$(hostname)-$(date -u +'%Y%m%dT%H%M%SZ')" \ + ${PATHS[@]} \ + ${EXCLUDE_CMD[@]} +} + +borg_prune () { + # Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 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. + $BORG prune --info --stats --list \ + --prefix "$(hostname)-" \ + --keep-daily=$KEEP_DAILY \ + --keep-weekly=$KEEP_WEEKLY \ + --keep-monthly=$KEEP_MONTHLY \ + --keep-yearly=$KEEP_YEARLY \ + "${REPO}" +} + +borg_verify () { + $BORG check --show-rc "${REPO}" +} + +borg_unlock () { + # Use if borg backup is not shut down cleanly + $BORG break-lock "${REPO}" +} + + +source "$CONFIG" || exit 1 +export BORG_PASSPHRASE + +if [[ $MODE == "backup" ]]; then + borg_backup + borg_prune +elif [[ $MODE == "verify" ]]; then + borg_verify +elif [[ $MODE == "unlock" ]]; then + borg_unlock +else + print_usage +fi diff --git a/src/config.sh.example b/src/config.sh.example new file mode 100644 index 0000000..8529101 --- /dev/null +++ b/src/config.sh.example @@ -0,0 +1,20 @@ +BORG="/usr/bin/borg" +REPO="user@reposerver:/srv/borg/hostname" +BORG_PASSPHRASE="longandcomplexpassphrase" +PATHS=( + "/etc" + "/home" + "/root" + "/srv" + "/usr/local" + "/var/spool/cron/crontabs" +) +EXCLUDES=( + "sh:/home/**/.cache" + "/root/.cache" + "*.pyc" +) +KEEP_DAILY=31 +KEEP_WEEKLY=0 +KEEP_MONTHLY=24 +KEEP_YEARLY=5