首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash:在运行时使用变量对rsync进行动态流量整形、调度和减速?

Bash:在运行时使用变量对rsync进行动态流量整形、调度和减速?
EN

Server Fault用户
提问于 2020-09-01 21:11:41
回答 1查看 591关注 0票数 5

在bash脚本( after a命令已经启动)期间运行动态流量整形的解决方案是什么?这有可能吗?

我的用例是,我让rsync在一组磁盘上运行到一个远程备份位置,这在因特网上花费了许多小时,所以我希望将流量整形应用到调用rsync的行,但只在指定的时间内。

例如,目前的计划是在上午5时至上午10时和下午3时至9时将上传量减少到每秒1000 is (1.0MB/s)。

我已经看过--bwlimit=1000 for rsync,但是它在运行的整个时间内都是rsync的形状,not只是所需的成形时间框架

一旦rsync启动,是否有可能减慢它的速度?因为如果这是可能的,那就是我的解决方案。

我见过有人建议wondershaper,但我想知道这是否可以通过编程“打开或关闭”呢?如果是的话,怎么做?

下面是我使用Stackoverflow慷慨捐款进行的一个模拟,它提供了指定和检查时间计划的功能(基于时间,这对我目前的需要来说还可以).

代码语言:javascript
复制
#!/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
EN

回答 1

Server Fault用户

发布于 2020-09-01 22:25:43

rsync不能动态改变其带宽限制参数。不管你怎么做

  • 将作业分成几个较小的任务(),其中每个任务都有一个基于启动时间的特定bw限制,例如

例如

代码语言:javascript
复制
rsync a b c d e target:/dir/

to

rsync --bw-limit=bw1 a target:/dir/
...
rsync --bw-limit=bw2 e target:/dir/
  • Use --time-limit=MINS <>选项

如果文件是同步的,rsync是智能的,不会重做它刚才做的事情,所以您可以使用bw限制bw1运行1小时,然后用bw2等重新启动它(如果需要的话暂停它),或者与上面的解决方案结合。

代码语言:javascript
复制
rsync --bw-limit=bw1 --time-limit=60 ...

rsync --bw-limit=bw2 --time-limit=60 ...
  • Change rsync

如果您喜欢,rsync是开放源代码的,您可以添加一些代码来动态地对某些信号(信号杀掉)进行rsync应答,这些信号将改变内部选项(maight还必须根据代码更改目标rsync守护进程)。

代码语言:javascript
复制
rsync --my-new-version ...

kill -SIGNALTOSLOW rsyncpid
...
kill -SIGNALTOSPEED rsyncpid
票数 5
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/1032336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档