目前业内比较靠谱的同步解决方案有:
rsync+inotify-tools,Openduckbill+inotify-tools和rsync+sersync
前两者由于是基于脚本语言编写,所以规范程度,执行效率相对rsync+sersync就稍微弱一些。
sersync是使用c++编写,基于boost1.43.0,inotify api,rsync command开发,主要用于服务器同步,web镜像等功能。其对linux系统文件系统产生的临时文件和重复的文件操作能够进行过滤,所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快,更适合线上使用。
注:使用rsync+crontab做定时同步时,主服务器端开启rsync守护进程,而镜像服务器是运行rsync客户端,平时一般会利用crontab定时获取rsync服务器上的数据。
但使用rsync+sersync做实时同步时,用于推送文件的服务器运行sersync服务,用于接收文件的服务器则运行rsync守护进程,简单来说就是sersync会利用rsync命令将文件推送到rsync服务器,实际线上使用一般会把sersync作为主服务器,rsync作为镜像服务器,实现数据同步备份,web镜像等功能
使用sersync同步文件=====================
服务端
下载sersync2.5_64bit_binary_stable_final.tar.gz
#tar -zxvf sersync2.5_64bit_binary_stable_final.tar.gz
#vi confxml.xml
<sersync>
<localpath watch="/home/easyinfo/masterenterprise">
<remote ip="192.168.0.205" name="rsyncenterprise"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="30"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
#/home/easyinfo/soft/GNU-Linux-x86/sersync2 -r -n 6 -d -o /home/easyinfo/soft/GNU-Linux-x86/confenterprise.xml
客户端
#vi /etc/rsyncd.conf
uid=root
gid=root
max connections=36000
use chroot=no
log file=/var/log/rsyncd.log
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
[rsyncuser]
path = /home/easyinfo/user
read only = no
ignore errors = yes
hosts allow = 192.168.0.203
hosts deny = *
list = true
[rsyncenterprise]
path = /home/easyinfo/enterprise
read only = no
ignore errors = yes
hosts allow = 192.168.0.203
hosts deny = *
list = true
#rsync --daemon
若提示failed to create pid file /var/run/rsyncd.pid: Permission denied,则执行
#sudo rsync --daemon
确保服务正常启动
netstat -an | grep 873查看873端口是否开启
温馨提示:确保主机能访问到备机的873端口
sersync2可以实现一台机器上的某个目录实时同步到多台机器,非常适合大量小文件如web网页。
1. 配置同步目标服务器
# vi /etc/rsyncd.conf
uid = root
gid = root
max connections = 36000
use chroot = no
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
[rsync] # 同步名
path = /opt/rsync
comment = test files
ignore error = yes
read only = no
hosts allow = 192.168.0.254
hosts deny = *
Note: '/etc/rsyncd.conf' 不存在,要自己创建, 多台目标机器分别进行如上配置。
2. 启动目标服务器rsync准备接收数据
# rsync --daemon
3. 在主服务器上安装配置sersync
# tar xf sersync2.5_64bit_binary_stable_final.tar.gz
# cd GNU-Linux-x86/
# vi confxml.xml, 修改如下行
<sersync>
<localpath watch="/opt/rsync"> # 本地同步目标
<remote ip="192.168.0.1" name="rsync"/> # 目录服务器IP和同步名
<remote ip="192.168.0.2" name="rsync"/>
</localpath>
4. 运行sersync
# ./sersync2 -d -r
注:这里提一个细节,如果接收端服务器本地创建或修改/data同步目录下的文件,当服务端进行目录同步时则不会对接收端服务器本地创建或修改的文件产生影响。