From 8bb2a0e178a29b81c1d1790c94762f45dc0ded13 Mon Sep 17 00:00:00 2001 From: hk Date: Wed, 7 Jun 2017 01:34:35 +0200 Subject: [PATCH] Add support for bandwith limitation --- src/borgwrapper.sh | 18 ++++++++++++++++++ src/config.sh.example | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/src/borgwrapper.sh b/src/borgwrapper.sh index 47c3316..c2d7ab6 100755 --- a/src/borgwrapper.sh +++ b/src/borgwrapper.sh @@ -87,6 +87,19 @@ borg_exec () { ${BORG} "$@" } +limit_bw () { + if ! [[ -x $(command -v pv) ]]; then + >&2 echo "WARNING: BWLIMIT is enabled, but the utility 'pv' is not available. Continuing without bandwith limitation." + return 0 + fi + + export PV_WRAPPER=$(mktemp) + chmod +x ${PV_WRAPPER} + echo -e '#!/bin/bash\npv -q -L ${BWLIMIT} | "$@"' > ${PV_WRAPPER} + export BWLIMIT + export BORG_RSH="${PV_WRAPPER} ssh" +} + pre_backup_cmd () { [[ -n ${PRE_BACKUP_CMD} ]] || return 0 echo "Running pre backup command: ${PRE_BACKUP_CMD[@]}" @@ -121,6 +134,7 @@ lock_failed () { } exit_clean () { + [[ -n ${PV_WRAPPER} ]] && rm -f ${PV_WRAPPER} trap - ERR INT TERM exit $1 } @@ -162,6 +176,10 @@ export BORG_PASSPHRASE trap 'error_handler ${LINENO} $?' ERR INT TERM set -o errtrace -o pipefail + if [[ ${BWLIMIT} -gt 0 ]]; then + limit_bw + fi + if [[ ${MODE} == "init" ]]; then borg_init elif [[ ${MODE} == "backup" ]]; then diff --git a/src/config.sh.example b/src/config.sh.example index cc5d0ba..4771344 100644 --- a/src/config.sh.example +++ b/src/config.sh.example @@ -22,6 +22,10 @@ KEEP_WEEKLY=0 KEEP_MONTHLY=24 KEEP_YEARLY=5 +# Limit bandwith used when backing up to SSH repositories. Requires the utility `pv`. +# The unit is bytes per second. 0 means no limit. +#BWLIMIT=0 + # Change this if you need to have multiple borgwrapper instances running # using different configs. #LOCKFILE="/var/lock/borgwrapper.lock"