海量文件同步方案: 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 & 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章