rsync的多进程ssh同步实现
新建sh文件
#!/bin/sh
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin"
dir='/local/xxx/...' #需同步的路径
redir='/remote/xxxx/...' #远程服务器目录
ip='xxx.xxx.xxx.xxx' #远程服务器IP
port='22' #远程服务器ssh端口
opt='-rv -p -t -L' #rsync 选项
num=10
depth='4 3 2 1' #归递目录深度
# 先在远程创建目录结构
#rsync $opt --include "*/" --exclude "*" -e "ssh -p $port" $dir/ $ip:$dir/
# 从深到浅同步目录
for l in $depth ;do
todo=`find $dir -maxdepth $l -mindepth $l -type d`
# 启动rsync进程
for i in $todo; do
now_num=`ps axw | grep rsync | grep $ip | grep -v '\-\-server' | wc -l`
#echo "${i/$dir/$redir}::$now_num"
while [ $now_num -ge $num ];do
#echo 'wait 1s'
sleep 1
now_num=`ps axw | grep rsync | grep $ip | grep -v '\-\-server' | wc -l`
done
rsync $opt -e "ssh -p $port"\
$i/ $ip:${i/$dir/$redir}/ &
done
done
# 最后再全量验证一遍
while true; do
sleep 5
now_num=`ps axw | grep rsync | grep $ip | grep -v '\-\-server' | wc -l`
if [ $now_num -lt 1 ]; then
rsync $opt -e "ssh -p $port"\
$dir/ $ip:$redir/
break
fi
done
opt 的参数请参考
rsync
语法
需要提前设置ssh免登陆
ssh免登陆设置 https://blog.csdn.net/longxuu/article/details/106303449
参考文档 https://wiki.freebsdchina.org/rsync%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%AE%80%E5%8D%95shell%E7%89%88