
Bash 스크립트 중에 동적 트래픽 조정을 실행하는 솔루션은 무엇입니까?명령이 이미 시작된 후? 이것이 가능합니까?
내 사용 사례는 rsync
대규모 디스크 세트에서 원격 백업 위치로 실행하고 있으며 인터넷을 통해 많은 시간이 걸리기 때문에 호출되는 라인에 트래픽 조정을 적용하고 싶지만 rsync
지정된 시간 동안에만 적용하고 싶습니다.
예를 들어, 현재 일정은 오전 5시~오전 10시, 오후 3시~9시 동안 업로드를 초당 1000킬로바이트(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 (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 --my-new-version ...
kill -SIGNALTOSLOW rsyncpid
...
kill -SIGNALTOSPEED rsyncpid