Bash:使用变量来动态地对 rsync 流量进行整形以安排时间表并在运行时减慢速度?

Bash:使用变量来动态地对 rsync 流量进行整形以安排时间表并在运行时减慢速度?

在 bash 脚本期间运行动态流量整形有哪些解决方案?在命令已经开始之后?这可能吗?

我的用例是,我rsync在远程备份位置上运行大量磁盘,这在互联网上要花很多时间,所以我想将流量整形应用到rsync调用的线路上,但仅限于指定的时间。

举例来说,目前的计划是将上午 5 点到 10 点以及下午 3 点到 9 点期间的上传速度减少到每秒 1000 千字节 (1.0MB/s)。

我已经看过了--bwlimit=1000rsyncrsync在整个运行过程中,不仅仅是期望的塑造时间表

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 (https://stackoverflow.com/questions/63532910)
# 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 (https://stackoverflow.com/questions/18128573)
            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

答案1

rsync无法动态更改其带宽限制参数。
但是您可以

  • 将工作拆分成几个较小的任务每个都会根据其启动时间有一个特定的 bw 限制,例如

例如

rsync a b c d e target:/dir/

to

rsync --bw-limit=bw1 a target:/dir/
...
rsync --bw-limit=bw2 e target:/dir/
  • 使用--time-limit=MINS选项

rsync很智能,如果文件同步,它不会重复刚才的操作,因此您可以rsync以 bw 限制 bw1 运行 1 小时,然后使用 bw2 重新启动它,等等(如果需要,可以暂停它),或者与上述解决方案结合使用。

rsync --bw-limit=bw1 --time-limit=60 ...

rsync --bw-limit=bw2 --time-limit=60 ...
  • 更改 rsync

如果您愿意,它rsync是开源的,您可以添加一些代码来rsync动态地响应某些信号(信号)这将改变内部选项(可能还必须根据代码更改目标 rsync 守护进程)

rsync --my-new-version ...

kill -SIGNALTOSLOW rsyncpid
...
kill -SIGNALTOSPEED rsyncpid

相关内容