海量文件同步方案: 多進程rsync+ssh

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章