lsyncd+rsync配置图片资源双向同步

需求:为保证国内外图片加载速度,国内请求上传图片资源地址阿里云服务器,国外请求上传图片资源地址aws ,为保证图片资源的一致性,需定时进行aws和阿里云图片双向同步css

调研方案:因为以前配置过inotify+rsync,计划经过该方案解决,但这种方案在图片资源量级较大时,并不能作到实时同步,并且inotify 存在性能问题,参考网上几种方案对比,故决定采用lsyncd+rsync。html

lsyncd简介:vim

Lsyncd watches a local directory trees event monitor interface (inotify or fsevents). It aggregates and combines events for a few seconds and then spawns one (or more) process(es) to synchronize the changes. By default this is rsync. 
Lsyncd is thus a light-weight live mirror solution that is comparatively easy to install not requiring new filesystems or block devices and does not hamper local filesystem performance. Lsyncd监听本地文件系统事件监控接口(inotify或fsevents)。它将若干秒内的事件聚合,而后触发一个或多个进程以同步变动。默认的同步手段是rsync。
Lysncd是一个轻量级、易安装的同步方案,它不须要新的文件系统或块设备,也不会影响本地文件系统性能。

lsyncd的安装配置:bash

源端配置服务器

rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm # 安装 epel 源
yum -y install lsyncd rsync # 安装
mkdir /etc/lsyncd ; vim /etc/lsyncd/lsyncd.conf #配置ssh

settings { -- 日志文件存放位置 logfile ="/var/log/lsyncd/lsyncd.log", -- 监控目录状态文件的存放位置 statusFile ="/var/log/lsyncd/lsyncd.status", -- 指定要监控的事件,如,CloseWrite,Modify,CloseWrite or Modify inotifyMode = "CloseWrite or Modify", -- 指定同步时进程的最大个数 maxProcesses = 10, -- 隔多少秒记录一次被监控目录的状态 statusInterval = 10, -- false=启用守护模式 nodaemon = false, -- 当事件被命中累计多少次后才进行一次同步(即便间隔低于statusInterval) maxDelays = 20 } sync { -- lsyncd运行模式 -- default.direct=本地目录间同步 -- default.rsync=使用rsync -- default.rsyncssh=使用rsync的ssh模式 default.rsync, -- 同步的源目录 source = "/test", -- 同步的目标目录 target = "rsync://rsync@x.x.x.x:873/rsync_test0", -- 是否同步删除 true=同步删除 false=增量备份 delete = false, -- 排除同步文件 exclude = { "dir*" }, -- 等待rsync同步延时时间(秒) delay = 10, -- init=false则只同步lsyncd启动后变动,不设置则lsyncd启动后进行全量的同步 --init = True, rsync = {
-- 单位kb bwlimit=1000, binary = "/usr/bin/rsync", archive = true, compress = true, verbose = true, perms = true, -- rsync的用户密码,不是Linux系统的用户密码 password_file = "/etc/lsyncd/rsync.passwd" } }

建立秘钥文件:性能

echo "4V6bXyY7MCchEZ2hRq" > /etc/lsyncd/rsync.passwd ; chmod 600 /etc/lsyncd/rsync.passwd测试

 编辑/etc/init.d/lsyncd ,将该文件中的/etc/lsyncd.conf替换成/etc/lsyncd/lsyncd.conf,不要有遗漏。ui

目标端配置this

yum -y install rsync  #安装rsync

mkdir /var/rsync && mkdir /etc/rsync && mkdir /opt/rsync

vim /etc/rsync/rsync.conf

######### 全局配置参数 ########## #rsync端口 默认873 port=873 #rsync服务的运行用户,默认是nobody,文件传输成功后属主将是这个uid uid = 0 # rsync服务的运行组,默认是nobody,文件传输成功后属组将是这个gid gid = 0 #rsync daemon在传输前是否切换到指定的path目录下,并将其监禁在内 use chroot = no #最大链接数量,0表示没有限制 max connections = 30 #确保rsync服务器不会永远等待一个崩溃的客户端,0表示永远等待 timeout = 300 #客户端链接过来显示的消息 motd file = /var/rsync/rsync.motd #rsync daemon的pid文件 pid file = /var/run/rsync.pid #指定锁文件 lock file = /var/run/rsync.lock #指定rsync的日志文件,而不把日志发送给syslog log file = /var/log/rsync/rsync.log #指定哪些文件不用进行压缩传输 dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 ###########下面指定模块,并设定模块配置参数,能够建立多个模块########### [rsync_test0] # 第一个模块的ID #文件路径 path = /test/ #忽略某些IO错误信息 ignore errors #只读 read only = false #不隐藏该模板 list = true #容许IP hosts allow = x.x.x.x/32 #不容许IP hosts deny = 0.0.0.0/32 #指定链接到该模块的用户列表,只有列表里的用户才能链接到模块,用户名和对应密码保存在secrts file中 #这里使用的不是系统用户,而是虚拟用户。不设置时,默认全部用户都能链接,但使用的是匿名链接 auth users = rsync #保存auth users用户列表的用户名和密码,每行包含一个username:passwd。 #因为"strict modes"默认为true,因此此文件要求非rsync daemon用户不可读写。只有启用了auth users该选项才有效。 secrets file = /etc/rsync/rsync.passwd

将rsync用户认证信息写入到密码文件中
echo 'rsync:4V6bXyY7MCchEZ2hRq' >> /etc/rsync/rsync.passwd 其中rsync是用户名,须要和rsync.conf文件中的auth users保持一致,冒号后面的是该用户的密码。注意:该用户为虚拟用户,与Linux系统中的用户无关。

建立rsync启动脚本
vim  /opt/rsync/rsync.sh 
#!/bin/bash
#rsync启动脚本,注意这里的pid文件路径,conf文件路径必定要和本身的配置保持一致。
status1=$(ps -ef | egrep "rsync --daemon.*rsync.conf" | grep -v 'grep')
pidfile="/var/run/rsync.pid"
start_rsync="rsync --daemon --config=/etc/rsync/rsync.conf"

function rsyncstart() {
	if [ "${status1}X" == "X" ];then
		rm -f $pidfile
		$start_rsync
		status2=$(ps -ef | egrep "rsync --daemon.*rsync.conf" | grep -v 'grep')
		if [  "${status2}X" != "X"  ];then
			echo "rsync service start.......OK"
		fi
	else
		echo "rsync service is running !"
	fi
}
function rsyncstop() {
	if [ "${status1}X" != "X" ];then
		kill -9 $(cat $pidfile)
		status2=$(ps -ef | egrep "rsync --daemon.*rsync.conf" | grep -v 'grep')
		if [ "${statusw2}X" == "X" ];then
			echo "rsync service stop.......OK"
		fi
	else
		echo "rsync service is not running !"
	fi
}
function rsyncstatus() {
	if [ "${status1}X" != "X" ];then
		echo "rsync service is running !"
	else
		echo "rsync service is not running !"
	fi
}
function rsyncrestart() {
	if [ "${status1}X" == "X" ];then
		echo "rsync service is not running..."
		rsyncstart
	else
		rsyncstop
		rsyncstart
	fi
}
case $1 in
"start")
rsyncstart
;;
"stop")
rsyncstop
;;
"status")
rsyncstatus
;;
"restart")
rsyncrestart
;;
*)
echo
echo  "Usage: $0 start|stop|restart|status"
echo
esac

chmod +x /opt/rsync/rsync.sh

编辑/etc/init.d/lsyncd  将全部 /etc/lsyncd.conf  替换成 /etc/lsyncd/lsyncd.conf

源端启动lsyncd: service  lsyncd start  目标端:/opt/rsync/rsync.sh start 

测试rsync配置: rsync -r /test/ rsync://rsync@x.x.x.x:873/rsync_test0/ --password-file=/etc/lsyncd/rsync.passwd

因为配置的是双向同步,因此两边都须要进行源端和目标端配置。

分别在两边的/test 目录建立文件,观察同步状况,验证配置是否正常。

参考连接:http://www.devisaac.com/2018/07/26/remote-realtime-backup-using-lsyncd-and-rsync.html

相关文章
相关标签/搜索