sersync是基于Inotify开发的,类似于Inotify-tools的工具,基本上Inotify能实现的功能,sersync也具备,因此,sersync 也可以实时监听目录中发生变化的(包括增加、删除、修改)
但是为什么我们要使用sersync+rsync呢,它相对于inotify具备什么优势呢?Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来,rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率比较低。而sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。所以当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。
如图所示,我们在备份服务器里搭建一个rsync daemon,并创建/backup用来专门与NFS存储的数据目录作为实时同步的远端目录,而在对应的NFS存储中安装sersync,用来对/data目录进行动态监控,一旦发现/data目录有创建,删除,修改后立刻触发rsync进行同步推送到备份服务器的/backup里,从而保证两端的数据一致性,增强NFS存储的安全性。
[root@backup ~]# touch /etc/rsyncd.conf
[root@backup ~]# cat /etc/rsyncd.conf
# rsyncd-conf start
uid = rsync
gid = rsync
use chroot = no
max connections = 2000
timeout = 600
pid file= /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore = errors
read only = false
list = false
hosts allow = 192.168.50.0/24
hosts deny = 0.0.0.0/32
auth users = rsync-backup
secrets file = /etc/rsync.password
#####################################
[backup]
comment = backup
path = /backup
[root@backup ~]# touch /etc/rsync.password
[root@backup ~]# echo "rsync-backup:root" >/etc/rsync.password
[root@backup ~]# cat /etc/rsync.password
rsync-backup:root
[root@backup ~]# chmod 600 /etc/rsync.password
[root@backup ~]# ls -l /etc/rsync.password
-rw-------. 1 root root 18 May 21 06:23 /etc/rsync.password
这里需要注意密码文件必须和配置文件里的相对应
[root@backup ~]# useradd -g rsync -M -s /sbin/nologin rsync
[root@backup ~]# grep rsync /etc/passwd
rsync:x:506:506::/home/rsync:/sbin/nologin
[root@backup ~]# rsync --daemon
[root@server data]# echo "root" > /etc/rsync.password
[root@server data]# chmod 600 /etc/rsync.password
[root@server data]# ls -l /etc/rsync.password -rw-------. 1 root root 5 May 14 20:44 /etc/rsync.password
[root@server data]# echo "hello" >file1
[root@server data]# rsync file1 rsync-backup@192.168.50.4::backup/ --password-file=/etc/rsync.password
在备份服务器查看是否推送成功
[root@backup ~]# cat /backup/file1
hello
[root@backup /]# cd /backup &&ls
backup file1
[root@backup backup]#
[root@server ~]# ls -l /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 May 21 11:59 max_queued_events
-rw-r--r-- 1 root root 0 May 21 11:59 max_user_instances
-rw-r--r-- 1 root root 0 May 21 11:59 max_user_watches
[root@server ~]#
[root@server ~]# uname -r
2.6.32-696.23.1.el6.x86_64
[root@server ~]# cd /proc/sys/fs/inotify/
[root@server inotify]# ls
max_queued_events max_user_instances max_user_watches
[root@server inotify]# cat max_queued_events
16384
[root@server inotify]# cat max_user_instances
128
[root@server inotify]# cat max_user_watches
8192
[root@server inotify]#
[root@server inotify]# echo "50000000" >max_user_instances
[root@server inotify]# echo "50000000" >max_user_watches
[root@server inotify]# echo "50000000" >max_queued_events
[root@server inotify]# ls
max_queued_events max_user_instances max_user_watches
[root@server inotify]# find ./ -type f |xargs cat
50000000
50000000
50000000
[root@server inotify]#
将参数写入配置文件/etc/sysctl.conf,如下:
[root@server inotify]# echo "fs.inotify.max_queued_events=50000000" >>/etc/sysctl.conf
[root@server inotify]# echo "fs.inotify.max_user_watches=50000000" >>/etc/sysctl.conf [root@server inotify]# echo "fs.inotify.max_user_instances=50000000" >>/etc/sysctl.conf
[root@server inotify]# tail -3 /etc/sysctl.conf
fs.inotify.max_queued_events=50000000
fs.inotify.max_user_watches=50000000
fs.inotify.max_user_instances=50000000
[root@server inotify]#
[root@server ~]# mkdir -p /usr/local/src
上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下
[root@server src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@server local]# mkdir -p /usr/local/sersync/conf
[root@server local]# mkdir -p /usr/local/sersync/log
[root@server local]# mkdir -p /usr/local/sersync/bin
[root@server src]# cd GNU-Linux-x86/
[root@server GNU-Linux-x86]# ls
confxml.xml sersync2
[root@server GNU-Linux-x86]# cp -a confxml.xml /usr/local/sersync/conf/
[root@server GNU-Linux-x86]# cp -a sersync2 /usr/local/sersync/bin/
修改sersync配置文件
[root@server conf]# cp confxml.xml confxml.xml.bak
[root@server conf]# vim confxml.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.svn"></exclude>
<exclude expression="(.*)\.gz"></exclude>
<exclude expression="^info/*"></exclude>
<exclude expression="^static/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/data" />
<remote ip="192.168.50.4" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avz"/>
<auth start="true" users="rsync-backup" passwordfile="/etc/rsync.password"/>
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="100"/><!-- timeout=100 -->
<ssh start="false"/>
[6n/rsync>
<failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="false" schedule="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
<plugin name="command">
<param prefix="/bin/sh" suffix="" ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->
<filter start="false">
<include expression="(.*)\.php"/>
<include expression="(.*)\.sh"/>
</filter>
</plugin>
<plugin name="socket">
<localpath watch="/opt/tongbu">
<deshost ip="192.168.138.20" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://pic.xoyo.com/cms"/>
<regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
</localpath>
</plugin>
</head>
重点修改如下:
1 <sersync>
<localpath watch="/data" />
<remote ip="192.168.50.4" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
2 <rsync>
<commonParams params="-avz"/>
<auth start="true" users="rsync-backup" passwordfile="/etc/rsync.password"/
3 <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
[root@server conf]# /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
[root@server conf]# ps -ef |grep sersync2
root 1938 1 0 13:26 ? 00:00:00 /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
对server与backup服务器分别进行验证,并写入开机启动
NFS server端:
[root@server data]# for n in `seq 5`;do echo ‘ hello world !‘ >file$n ;done
[root@server data]#
[root@server data]# ls
file1 file2 file3 file4 file5
[root@server data]# find ./ -type f |xargs cat
hello world !
hello world !
hello world !
hello world !
hello world !
backup 端:
[root@backup backup]# ls
file1 file2 file3 file4 file5
[root@backup backup]# find ./ -type f |xargs cat
hello world !
hello world !
hello world !
hello world !
hello world !
将sersync写入开机启动:
[root@server conf]# echo "/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml" >>/etc/rc.local
[root@server conf]# tail -1 /etc/rc.local
/usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
[root@server scripts]# vim check_sersync.sh
#!/usr/bin/env bash
##############################################################
# File Name: check_sersync.sh
# Version: V1.0
# Author: OuYoung
# Blog: http://blog.51cto.com/ouyangtao
# Created Time : 2018-05-21 13:50:38
# Description:
##############################################################
sersync="/usr/local/sersync/bin/sersync2"
config="/usr/local/sersync/conf/confxml.xml"
status=$(ps aux |grep "sersync2" |grep -v "grep" |wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $config >/dev/null else
exit 0 ;
fi
[root@server scripts]# echo "*/5 * * * * sh /service/scripts/check_sersync.sh >/dev/null" >>/var/spool/cron/root
[root@server scripts]# cat /var/spool/cron/root
#定时时钟同步每20分钟同步一次
*/20 * * * * ntpdate 1.cn.pool.ntp.org
# * * * * * bash
*/5 * * * * sh /service/scripts/check_sersync.sh >/dev/null
[root@server scripts]# /etc/init.d/crond reload
重新载入 crond:
[root@server scripts]# pkill sersync2 && date
2018年 05月 21日 星期一 14:22:20 CST
[root@server scripts]# ps -ef |grep sersync2 && uptime
root 2352 1801 0 14:24 pts/0 00:00:00 grep --color=auto sersync2
14:24:33 up 2:27, 2 users, load average: 0.00, 0.00, 0.00
[root@server scripts]# ps -ef |grep sersync2 && uptime
root 2366 1 0 14:25 ? 00:00:00 /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
root 2382 1801 0 14:25 pts/0 00:00:00 grep --color=auto sersync2
14:25:04 up 2:28, 2 users, load average: 0.00, 0.00, 0.00
[root@server scripts]#
原文地址:http://blog.51cto.com/ouyangtao/2121947