Bash:使用時間表變數動態調整 rsync 流量並在運行時減慢速度?

Bash:使用時間表變數動態調整 rsync 流量並在運行時減慢速度?

在 bash 腳本期間執行動態流量整形的解決方案有哪些?命令已經開始後?這可能嗎?

我的用例是,我rsync在一組巨大的磁碟上運行到遠端備份位置,這需要透過網路花費很多小時,因此我想將流量整形應用於所rsync呼叫的線路,但僅限於指定時間。

舉例來說,目前的計劃是在上午 5 點至上午 10 點以及下午 3 點至晚上 9 點期間將上傳速度減少至每秒 1000 KB (1.0MB/s)。

我已經看過了--bwlimit=1000rsync但是這個形狀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 (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不能動態變更其頻寬限制參數。
不過你可以

  • 將工作分成幾個較小的任務其中每個都會根據其開始時間有特定的頻寬限制,例如

例如

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

相關內容