并行rsync

时间:2023-03-09 07:32:46
并行rsync
#!/bin/bash

if [ $# -le  ]; then
echo -e "usage : \n\t$0 hostList src_file dst_path"
echo -e "example: \n\t$0 hostList file.txt /home/work/opdir"
exit
fi # concurrent number by default.
CON_NUM=
host_list=$
shift src=''
while [ $# -gt ]; do
src="$src $1"
shift
done
dst=$ echo "src: $src"
echo "det: $dst" if [ ! -f $host_list ]; then
echo "$host_list is not exist!"
exit
fi function your_cmd() {
host=$
#echo -e "---- cmd() start to : $host --"
CMD="scp -r ${src} ${host}:${dst} 2>/dev/null"
eval ${CMD}
ret=$?
if [ $ret -ne ];then
echo -e "\t$host: cmd return retry $ret"
fi
} function concurrent()
{
# ff_file which is opened by fd will be really removed after script stopped
mkfifo ./fifo.$$ && exec <> ./fifo.$$ && rm -f ./fifo.$$ # initial fifo: write $con_num line to $ff_file
for ((i=; i<=$CON_NUM; i++)); do
echo "init:$i" >& # init fifo
done for host in $(cat $host_list | grep -v "^#"); do
read -u # read from fifo to REPLY
{
#echo -e "\n-- current loop for: \"$host\" [ fifo id: \"$REPLY\" ]"
your_cmd "$host"
echo -e "---- done: $host ----\n"
echo "real:$host" >& # write to $fifo
} & # & to backgroud each process in {}
done
wait # wait all con-current cmd in { } been running over
} concurrent