我的服务器serval上有一个大文件,我希望尽快将其复制到我的所有服务器上。 下面是当前的脚本,有没有更快的方法来做到这一点,如病毒分布, 例如:来源是A,目标是B C D. 在B完成后,A-> B,然后B-> C,A-> D(同时)。 我怎样才能改进剧本,任何想法,谢谢!
#!/bin/bash
set -u
SOURCE_POOL=(seq -f 10.10.10.%g 10 20)
HOST_IP_LIST=(seq -f 10.10.10.%g 21 200)
READY_FLAG=/tmp/READY_FLAG
LOCK_FLAG=/tmp/LOCK_FLAG
do_lock(){
ip=$1;shift 1
ssh root@$ip "touch $LOCK_FLAG"
}
do_unlock(){
ip=$1;shift 1
ssh root@$ip "rm -f $LOCK_FLAG"
}
find_source(){
while true;do
for source_ip in ${SOURCE_POOL[@]}
do
ping $source_ip -c 1 1>/dev/null 2>/dev/null
if [[ $? -ne 0 ]];then
continue
fi
ssh root@$source_ip "ls $LOCK_FLAG" 1>/dev/null 2>/dev/null
if [[ $? -eq 0 ]];then
continue
fi
ssh root@$source_ip "ls $READY_FLAG" 1>/dev/null 2>/dev/null
if [[ $? -ne 0 ]];then
continue
fi
do_lock $source_ip
echo $source_ip
return 0
done
for source_ip in ${HOST_IP_LIST[@]}
do
ping $source_ip -c 1 1>/dev/null 2>/dev/null
if [[ $? -ne 0 ]];then
continue
fi
ssh root@$source_ip "ls $LOCK_FLAG" 1>/dev/null 2>/dev/null
if [[ $? -eq 0 ]];then
continue
fi
ssh root@$source_ip "ls $READY_FLAG" 1>/dev/null 2>/dev/null
if [[ $? -ne 0 ]];then
continue
fi
do_lock $source_ip
echo $source_ip
return 0
done
sleep 3
done
return 1
}
do_scp(){
source_ip=$1;shift 1
target_ip=$1;shift 1
ssh root@$target_ip "scp root@$source_ip:~/some_big_file.tar ."
ssh root@$target_ip "touch $READY_FLAG"
do_unlock $source_ip
}
scp_all(){
for target_ip in ${HOST_IP_LIST[@]}
do
source_ip=$(find_source)
do_scp $source_ip $target_ip &
done
}
do_wait(){
local flag=1
while [[ $flag -eq 1 ]];do
flag=0
for ip in ${HOST_IP_LIST[@]}
do
scp_num=`ssh root@$ip "ps -ef | grep scp | grep -v grep | wc -l" `
if [[ $scp_num -ne 0 ]];then
flag=1
break
fi
done
sleep 30
done
}
scp_all
do_wait