Skip to content

Instantly share code, notes, and snippets.

@9seconds
Last active October 21, 2017 18:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 9seconds/c6bcb8da8fa8f799db3012a1f95d16a3 to your computer and use it in GitHub Desktop.
Save 9seconds/c6bcb8da8fa8f799db3012a1f95d16a3 to your computer and use it in GitHub Desktop.
backup wrapper for borg backup
#!/bin/bash
set -eu -o pipefail
# -----------------------------------------------------------------------------
export LANG=en_US.UTF-8
export BORG_PASSPHRASE=''
MAIN_USER="johndoe"
MAIN_HOMEDIR="$(getent passwd ${MAIN_USER} | cut -f 6 -d ':')"
EXCLUDE_FILE="${MAIN_HOMEDIR}/.borg/exclude"
REPOSITORY="/var/backups/mybackup/"
REMOTE_SOURCE="/media/remotefs/backups/mybackup/"
LOCK_WAIT=120
CHECKPOINT_EVERY=120
COMPRESSION="zlib,9"
KEEP_WITHIN="3d"
KEEP_DAILY="7"
KEEP_WEEKLY="4"
KEEP_MONTHLY="3"
KEEP_YEARLY="1"
LOCK_WRITE="flock --exclusive ${REPOSITORY}"
LOCK_READ="flock --shared ${REPOSITORY}"
CMD="sudo -E -u ${MAIN_USER} -- borg"
WCMD="${LOCK_WRITE} ${CMD}"
RCMD="${LOCK_READ} ${CMD}"
SYNC="${LOCK_READ} rsync -azP --delete-delay"
DEFAULT_OPTS="--lock-wait ${LOCK_WAIT} --error"
# -----------------------------------------------------------------------------
borg_create() {
local prefix="$1"
local source_path="$2"
local backup_name="${prefix}_$(date --utc '+%Y.%m.%d-%H')"
${WCMD} create ${DEFAULT_OPTS} \
--checkpoint-interval ${CHECKPOINT_EVERY} \
--compression ${COMPRESSION} \
--exclude-from "${EXCLUDE_FILE}" \
--exclude-if-present ".duplicity-ignore" \
--one-file-system \
--exclude-caches \
"${REPOSITORY}::${backup_name}" "${source_path}" || :
fix_owner
}
borg_check() {
${WCMD} check ${DEFAULT_OPTS} "$REPOSITORY"
fix_owner
}
borg_list() {
${RCMD} list ${DEFAULT_OPTS} "${REPOSITORY}"
}
borg_delete() {
local target="$1"
${WCMD} delete ${DEFAULT_OPTS} \
--progress --stats \
"${REPOSITORY}::${target}"
fix_owner
}
borg_prune() {
local keep_within="${1:-${KEEP_WITHIN}}"
local keep_daily="${2:-${KEEP_DAILY}}"
local keep_weekly="${3:-${KEEP_WEEKLY}}"
local keep_monthly="${4:-${KEEP_MONTHLY}}"
local keep_yearly="${5:-${KEEP_YEARLY}}"
${WCMD} prune ${DEFAULT_OPTS} \
--keep-within "${keep_within}" \
--keep-daily "${keep_daily}" \
--keep-weekly "${keep_weekly}" \
--keep-monthly "${keep_monthly}" \
--keep-yearly "${keep_yearly}" \
"${REPOSITORY}"
fix_owner
}
borg_info() {
local target="$1"
${RCMD} info ${DEFAULT_OPTS} "${REPOSITORY}::${target}"
}
backup_sync() {
${SYNC} "${REPOSITORY}" "${REMOTE_SOURCE}"
}
fix_owner() {
${LOCK_WRITE} sudo chown -R ${MAIN_USER}:${MAIN_USER} "${REPOSITORY}"
}
print_help() {
echo "$(basename $0) is a small wrapper around Borg backup."
echo "https://borgbackup.readthedocs.io"
echo
echo "Settings:"
echo " MAIN_USER $(printf "%-40s (%s)" "${MAIN_USER}" $(getent passwd | cut -f 1 -d ':' | grep -q "${MAIN_USER}" && echo -n "ok" || echo -n "not ok"))"
echo " MAIN_HOMEDIR $(printf "%-40s (%s)" "${MAIN_HOMEDIR}" $([ -d "${MAIN_HOMEDIR}" ] && echo -n "ok" || echo -n "not ok"))"
echo " REPOSITORY $(printf "%-40s (%s)" "${REPOSITORY}" $([ -d "${REPOSITORY}" ] && echo -n "ok" || echo -n "not ok"))"
echo " EXCLUDE_FILE $(printf "%-40s (%s)" "${EXCLUDE_FILE}" $([ -r "${EXCLUDE_FILE}" ] && echo -n "ok" || echo -n "not ok"))"
echo " REMOTE_SOURCE $(printf "%-40s (%s)" "${REMOTE_SOURCE}" $([ -d "${REMOTE_SOURCE}" ] && echo -n "ok" || echo -n "not ok"))"
echo " BORG_PASSPHRASE $(printf "%-40s (%s)" " " $([ ! -z "${BORG_PASSPHRASE}" ] && echo -n "ok" || echo -n "not ok"))"
echo
echo "Commands available:"
echo " create Creates new archive in repository. Backups $HOME and /etc."
echo " check Checks repository for consistency."
echo " list List available snapshots."
echo " delete Removes a snapshot."
echo " - the name of the snapshot."
echo " prune Prunes old snapshots."
echo " - How long to keep all snapshots. Default is $KEEP_WITHIN."
echo " - How many daily snapshots to keep. Default is $KEEP_DAILY."
echo " - How many weekly snapshots to keep. Default is $KEEP_WEEKLY."
echo " - How many monthly snapshots to keep. Default is $KEEP_MONTHLY."
echo " - How many yearly snapshots to keep. Default is $KEEP_YEARLY."
echo " info Print information about snapshot."
echo " - the name of the snapshot."
echo " sync Sync local backup with remote one."
echo " help Print this message."
}
# -----------------------------------------------------------------------------
if [[ $# -eq 0 ]] ; then
OPTION=""
else
OPTION="$1"
shift
fi
case "${OPTION}" in
create)
borg_create home "${MAIN_HOMEDIR}"
borg_create etc /etc
;;
check)
borg_check "$@"
;;
list)
borg_list "$@"
;;
delete)
borg_delete "$@"
;;
prune)
borg_prune "$@"
;;
info)
borg_info "$@"
;;
sync)
backup_sync "$@"
;;
help)
print_help
;;
*)
print_help
exit 1
;;
esac
# -----------------------------------------------------------------------------
unset BORG_PASSPHRASE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment