Allow binary prefixes in BWLIMIT

dev
hk 2017-06-07 10:23:37 +02:00
parent 8dbc1671b0
commit c6c34e3bcc
2 changed files with 36 additions and 4 deletions

View File

@ -87,6 +87,34 @@ borg_exec () {
${BORG} "$@" ${BORG} "$@"
} }
convert_rate () {
# Takes the rate in bytes as argument
local IN_RATE=${1}
local RATE=0
local B_REGEX="^([0-9]+)$"
local KIB_REGEX="^([0-9]+)K$"
local MIB_REGEX="^([0-9]+)M$"
local GIB_REGEX="^([0-9]+)G$"
local TIB_REGEX="^([0-9]+)T$"
if [[ ${IN_RATE} =~ ${TIB_REGEX} ]]; then
RATE=$(( ${BASH_REMATCH[1]} * 1024**4 ))
elif [[ ${IN_RATE} =~ ${GIB_REGEX} ]]; then
RATE=$(( ${BASH_REMATCH[1]} * 1024**3 ))
elif [[ ${IN_RATE} =~ ${MIB_REGEX} ]]; then
RATE=$(( ${BASH_REMATCH[1]} * 1024**2 ))
elif [[ ${IN_RATE} =~ ${KIB_REGEX} ]]; then
RATE=$(( ${BASH_REMATCH[1]} * 1024 ))
elif [[ ${IN_RATE} =~ ${B_REGEX} ]]; then
RATE=${BASH_REMATCH[1]}
else
>&2 echo "${IN_RATE} is not a valid rate"
false
fi
echo ${RATE}
}
limit_bw () { limit_bw () {
if ! [[ -x $(command -v pv) ]]; then if ! [[ -x $(command -v pv) ]]; then
>&2 echo "WARNING: BWLIMIT is enabled, but the utility 'pv' is not available. Continuing without bandwith limitation." >&2 echo "WARNING: BWLIMIT is enabled, but the utility 'pv' is not available. Continuing without bandwith limitation."
@ -94,10 +122,11 @@ limit_bw () {
fi fi
export PV_WRAPPER=$(mktemp) export PV_WRAPPER=$(mktemp)
export RATE_LIMIT=$(convert_rate ${BWLIMIT})
chmod +x ${PV_WRAPPER} chmod +x ${PV_WRAPPER}
echo -e '#!/bin/bash\npv -q -L ${BWLIMIT} | "$@"' > ${PV_WRAPPER} echo -e '#!/bin/bash\npv -q -L ${RATE_LIMIT} | "$@"' > ${PV_WRAPPER}
export BWLIMIT
export BORG_RSH="${PV_WRAPPER} ssh" export BORG_RSH="${PV_WRAPPER} ssh"
echo "Limiting bandwith to ${RATE_LIMIT} bytes/s"
} }
pre_backup_cmd () { pre_backup_cmd () {
@ -147,6 +176,7 @@ BORG="/usr/bin/borg"
PRE_BACKUP_CMD=() PRE_BACKUP_CMD=()
POST_BACKUP_CMD=() POST_BACKUP_CMD=()
POST_VERIFY_CMD=() POST_VERIFY_CMD=()
BWLIMIT=0
while getopts ":c:" OPT; do while getopts ":c:" OPT; do
case ${OPT} in case ${OPT} in
@ -176,7 +206,8 @@ export BORG_PASSPHRASE
trap 'error_handler ${LINENO} $?' ERR INT TERM trap 'error_handler ${LINENO} $?' ERR INT TERM
set -o errtrace -o pipefail set -o errtrace -o pipefail
[[ ${BWLIMIT} -gt 0 ]] && limit_bw # Enforce bandwidth limit if set
[[ -n ${BWLIMIT} ]] && [[ ${BWLIMIT} != "0" ]] && limit_bw
if [[ ${MODE} == "init" ]]; then if [[ ${MODE} == "init" ]]; then
borg_init borg_init

View File

@ -23,7 +23,8 @@ KEEP_MONTHLY=24
KEEP_YEARLY=5 KEEP_YEARLY=5
# Limit bandwith used when backing up to SSH repositories. Requires the utility `pv`. # Limit bandwith used when backing up to SSH repositories. Requires the utility `pv`.
# The unit is bytes per second. 0 means no limit. # The unit is bytes per second. 0 means no limit. The numeric part must be an integer value.
# You can use K, M, G or T binary prefixes (power of 1024), e.g., 5M = 5MiB/s = 5242880 bytes/s.
#BWLIMIT=0 #BWLIMIT=0
# Change this if you need to have multiple borgwrapper instances running # Change this if you need to have multiple borgwrapper instances running