
bashスクリプト中に動的トラフィックシェーピングを実行するための解決策は何ですか?コマンドがすでに開始された後? そんなことが可能なのでしょうか?
私のユースケースでは、rsync
リモート バックアップの場所への膨大なディスク セットを実行しており、インターネット経由では数時間かかるため、呼び出される回線にトラフィック シェーピングを適用したいのですrsync
が、指定された時間のみに適用したいのです。
たとえば、現在のスケジュールでは、午前 5 時から午前 10 時と午後 3 時から午後 9 時までのアップロードを 1 秒あたり 1000 キロバイト (1.0 MB/秒) に削減するとします。
私は--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 (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
帯域幅制限 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