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

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

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

lsyncd简介:

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的安装配置:

源端配置

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 #配置

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,不要有遗漏。

目标端配置

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

赠人玫瑰,手有余香,如果我的文章有幸能够帮到你,麻烦帮忙点下右下角的推荐,谢谢!

作者: imcati

出处: https://www.cnblogs.com/imcati/>

本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接

原文地址:https://www.cnblogs.com/imcati/p/10774194.html