noo*_*lag 5 networking backup bash rsync traffic-shaping
在命令已经启动后,在bash 脚本期间运行动态流量整形有哪些解决方案?这甚至可能吗?
我的用例是,我rsync在大量磁盘上运行到远程备份位置,这在 Internet 上需要很多小时,所以我想将流量整形应用于rsync调用的线路,但仅限于指定时间。
因此,例如,当前的计划是在上午 5 点到上午 10 点和下午 3 点到晚上 9 点将上传减少到每秒 1000 KB (1.0MB/s)。
我已经看过了--bwlimit=1000,rsync但这会rsync在它运行的整个时间里形成,而不仅仅是所需的塑造时间框架。
rsync启动后可以减速吗?因为如果以某种方式可能,那将是我的解决方案。
我见过有人建议,wondershaper但我想知道这是否可以以编程方式“打开和关闭”?如果是,如何做到这一点?
这是一个模型,我使用 Stackoverflow 的慷慨贡献对它进行了黑客攻击,它提供了指定和检查时间表的功能(基于小时,这对我目前的需求来说很好)......
#!/bin/bash
declare -A shapingTimes
declare -A keycount
useTrafficShaping=1
# schedule
shapingTimes=([0-from]=5 [0-to]=10) # 5am to 10am
shapingTimes+=([1-from]=15 [1-to]=21) # 3pm to 9pm
# because keys and array content differs in order from the way they are defined, we need to tidy this up by setting up the shaping times order in the way in which it is defined above
# to do this, count how many keys used in array entry (/sf/ask/4447303731/)
# we use this result to to dynamically calculate the total number of entries ($totalshapingTimes)
for i in "${!shapingTimes[@]}"; do keycount[${i//[0-9]-/}]=""; done
totalshapingTimes=$((${#shapingTimes[*]} / ${#keycount[@]}))
x=1; while [[ $x -le "$totalshapingTimes" ]]; do shapingTimes_order+="$(( x-1 )) "; x=$(( $x + 1 )); done
# 'shapingTimes_order' array is used later in this script to process which shapingTimes are handled in what order
if [[ -n $useTrafficShaping ]] && [[ $useTrafficShaping == 1 ]]; then
echo "Traffic shaping: ON"
# get current time (hour) to determine if we're inside peak times
currentHour=$(date +%H)
# display our traffic shaping time windows as defined in above array
echo "Defined schedule:"
for i in ${shapingTimes_order[*]}; do
echo "${shapingTimes[$i-from]}hrs to ${shapingTimes[$i-to]}hrs"
# work out which peak times we're using, thanks to the help of Glenn Jackman (/sf/ask/1269000141/)
if (( ${shapingTimes[$i-from]} <= 10#$currentHour && 10#$currentHour < ${shapingTimes[$i-to]} )); then
# current time matches a specified shaping timeframe, so rsync should go SLOW!
shape=1
fi
done
echo "The current hour is $currentHour"
if [[ -z $shape ]]; then
echo "Not in a shaping window, rsync can run as normal."
# some command here, run rsync as normal
else
echo "Matches a shaping schedule. *** SHAPING NETWORK TRAFFIC ***"
# command here to kick in traffic shaping
fi
else
echo "Traffic shaping: OFF"
# run rsync as normal
fi
Run Code Online (Sandbox Code Playgroud)
rsync不能动态更改其带宽限制参数。
但是你可以
例如
rsync a b c d e target:/dir/
to
rsync --bw-limit=bw1 a target:/dir/
...
rsync --bw-limit=bw2 e target:/dir/
Run Code Online (Sandbox Code Playgroud)
--time-limit=MINS选项rsync很聪明,如果文件已同步,则不会重做刚刚执行的操作,因此您可以rsync使用 bw 限制 bw1运行1 小时,然后使用 bw2 等重新启动它(并在需要时暂停它),或者合并与上述解决方案。
rsync --bw-limit=bw1 --time-limit=60 ...
rsync --bw-limit=bw2 --time-limit=60 ...
Run Code Online (Sandbox Code Playgroud)
如果你喜欢它,它rsync是开源的,你可以添加一些代码来rsync动态地回答一些会改变内部选项的信号(signal和kill)(可能还必须根据代码更改目标 rsync 守护进程)
rsync --my-new-version ...
kill -SIGNALTOSLOW rsyncpid
...
kill -SIGNALTOSPEED rsyncpid
Run Code Online (Sandbox Code Playgroud)