海量文件同步方案: rsync的多進程實現

主要是通過rsync配置好服務端的模式來實現
ssh實現參考https://blog.csdn.net/longxuu/article/details/106309580

安裝服務端rsync模塊

執行一鍵命令

yum install -y rsync #centos,其餘自行搜索資料

模塊配置文件

vim /etc/rsyncd.conf
# Minimal configuration file for rsync daemon
; ; ; ; ; ; ; ; ; ; ; ; ; ; # See rsync(1) and rsyncd.conf(5) man pages for help
; ; ; ; ; ; ; ; ; ; ; ; ; ; # This line is required by the /etc/init.d/rsyncd script
; ; ; ; ; ; ; ; ; ; ; ; ; ; # GLOBAL OPTIONS
uid              = root
gid              = root
use chroot       = no
read only        = no
write only       = no
max connections  = 5
pid file         = /var/run/rsyncd.pid
secrets file     = /etc/rsyncd/rsyncd.secrets
motd file        = /etc/rsyncd/rsyncd.motd
# This will give you a separate log file
log file         = /var/log/rsyncd.log
# This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes
log format       = %t %a %m %f %b
syslog facility  = local3
timeout          = 300

[sync] #模塊名稱
uid             = nginx #用戶組
gid             = nginx #用戶
port            = 873 #端口
motd file       = /etc/rsyncd/rsyncd.motd
syslog facility = local3
log file        = /var/log/rsyncd.log
path            = /path/ #路徑
read only       = false #是否只讀
write only      = false #是否只寫
use chroot      = false #是否啓用 chroot
auth users      = username #授權用戶,注意修改
secrets file    = /etc/rsyncd/rsyncd.secrets
hosts allow     = xxx.xxx.xxx.xxx #允許連接該模塊的客戶端,不需要可以刪除這行
list            = true #是否在模塊列表中顯示模塊
max connections = 0 #最大併發連接數 0沒有限制
:wq

授權用戶配置文件

vi /etc/rsyncd/rsyncd.secrets

添加一行

用戶名:密碼
:wq

重啓服務

service rsync restart

開機自動啓動服務

chkconfig rsync on

客戶端

客戶端安裝和服務端一樣的

配置密碼文件

vi /etc/rsyncd.secrets

把在服務端設置的授權用戶的密碼輸入保存

排除文件

如果需要排除目錄或文件,需要執行這一步,最好設置一個,防止隱藏文件傳輸過去

vi /etc/lsyncd.exclude

一行一條

aaaa
bbb/
aaa.jpg
bbbb/aaaa.jpg

新建一個sh文件

vi /root/rsync.sh
#!/bin/sh
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin"

dir='/local/path/' #需同步的路徑
ip='xxx.xxx.xxx.xxx' #遠程服務器IP
port='873' 			#遠程服務器rsync端口
user='username'		#遠程服務器rsync用戶名
des='sync'			#遠程服務器rsync模塊名
opt='-rq -p -t -L --password-file=/etc/rsyncd.secrets'	#rsync 選項
exclude='--exclude-from=/etc/lsyncd.exclude' #如果上一步沒有建立, 將單引號裏面的刪除

num=20
depth='4 3 2 1' #歸遞目錄深度

# 從深到淺同步目錄
for l in $depth ;do
	todo=`find -L $dir -maxdepth $l -mindepth $l -type d` # -L 讀取軟鏈接
 	# 啓動rsync進程
 	for i in $todo; do
  		now_num=`ps axw | grep rsync | grep $ip | grep -v '\-\-server' | wc -l`
 		echo "${i/$dir/}::$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
		/usr/bin/rsync $opt $exclude $i/ rsync://$user@$ip:$port/$des/${i/$dir/}/ &
	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
		/usr/bin/rsync $opt $exclude $dir rsync://$user@$ip:$port/$des
		break
 	fi
done
#保存
:wq
#執行權限
chmod +x /root/rsync.sh
#後臺運行
nohup /root/rsync.sh & 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章