Linux系统sersync数据实时同步
一、sersync介绍
sersync其实是利用inotify和rsync两种软件技术来实现数据实时同步功能的,inotify是用于监听sersync所在服务器上的文件变化,结合rsync软件来进行数据同步,将数据实时同步给客户端服务器。
二、sersync工作过程
在同步主服务器上开启sersync,负责监听文件系统的变化,然后调用rsync命令把更新的文件同步到目标服务器上,主服务器上安装sersync软件,目标服务器上安装rsync服务。
三、客户端安装配置rsync服务
[root@nbh_guanwang_nginx ~]# cat /etc/rsyncd.conf
cat: /etc/rsyncd.conf: No such file or directory
如果有此文件,配置前要进行备份,再进行相关配置
[root@nbh_guanwang_nginx ~]# cat /etc/rsyncd.conf
##rsync config start##created by root 2016-08-08 15:00
##rsync.conf config start
uid = root
gid = root
use chroot = no
max connetctions = 200
timeout = 100
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
[backup]
path = /data/cms/upload/
ignore errors
read only = false
list = false
hosts allow = 10.0.20.203/24
hosts deny = 0.0.0.0/32
auth users = rsync_backup
secrets file = /etc/rsync.password
##rsync config end
"rsyncd.conf" [New] 21L, 458C written
添加用户
[root@nbh_guanwang_nginx ~]# useradd rsync -s /sbin/nologin -M
改变目录权限
[root@nbh_guanwang_nginx ~]# chown -R rsync.rsync /data/cms/upload/
配置密码文件
[root@nbh_guanwang_nginx ~]# echo "rsync_backup:rsync.conf">>/etc/rsync.password
[root@nbh_guanwang_nginx ~]# cat /etc/rsync.password
rsync_backup:rsync.conf
改变密码文件权限
[root@nbh_guanwang_nginx ~]# chmod 600 /etc/rsync.password
[root@nbh_guanwang_nginx ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 24 Sep 9 13:06 /etc/rsync.password
格式化文件
[root@nbh_guanwang_nginx ~]# dos2unix /etc/rsyncd.conf
dos2unix: converting file /etc/rsyncd.conf to UNIX format ...
开启服务后台运行
[root@nbh_guanwang_nginx ~]# rsync --daemon
[root@nbh_guanwang_nginx ~]# netstat -lntup|grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2002/rsync
tcp 0 0 :::873 :::* LISTEN 2002/rsync
四、主服务器上配置密码文件
[root@nbh_guanwang_a ~]# echo "rsync.conf">>/etc/rsync.password
[root@nbh_guanwang_a ~]# cat /etc/rsync.password
rsync.conf
[root@nbh_guanwang_a ~]# chmod 600 /etc/rsync.password
[root@nbh_guanwang_a ~]# ls -ld /etc/rsync.password
-rw-------. 1 root root 11 Sep 8 06:25 /etc/rsync.password
五、测试手工同步
[root@nbh_guanwang_a ~]# rsync -avzP /etc/hosts rsync_backup@10.0.20.2::backup--password-file=/etc/rsync.password
sending incremental file list
hosts
158 100% 0.00kB/s 0:00:00 (xfer#1, to-check=0/1)
sent 120 bytes received 27 bytes 26.73 bytes/sectotal size is 158 speedup is 1.07
[root@nbh_guanwang_a ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@nbh_guanwang_a ~]# cat /backup/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
经过对比两边数据一致,表明同步成功,手工同步成功之后,然后再进行后面的配置
六:安装sersync服务
首先下载好安装软件
sersync2.5.4_64bit_binary_stable_final.tar.gz[root@nbh_guanwang_a software]## tar -zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz -C
/usr/local/
GNU-Linux-x86/
GNU-Linux-x86/sersync2
GNU-Linux-x86/confxml.xml
[root@nbh_guanwang_a software]## cd /usr/local/
[root@nbh_guanwang_a local]# ls
bin games include lib64 sbin src
etc GNU-Linux-x86 lib libexec share
GNU-Linux-x86就是sersync安装软件,为了方便将它改名
[root@nbh_guanwang_a local]# mv GNU-Linux-x86 sersync
为了后续方便管理,创建几个目录用于存放各类文件
[root@nbh_guanwang_a sersync]# mkdir -p conf bin logs
[root@nbh_guanwang_a sersync]# mv confxml.xml conf
[root@nbh_guanwang_a sersync]# ls
bin conf logs sersync2
[root@nbh_guanwang_a sersync]# cd conf
[root@nbh_guanwang_a conf]## ls
confxml.xml
在配置配置文件之前备份
[root@nbh_guanwang_a conf]# cp confxml.xml confxml.xml.$(date +%F)
[root@nbh_guanwang_a conf]# ls
confxml.xml confxml.xml.2024-06-22
修改配置文件内容(confxml.xml)
1、修改24-28行
<localpath watch="/opt/tongbu"><remote ip="127.0.0.1" name="tongbu1"/><!--<remote ip="192.168.8.39" name="tongbu"/>-->注释内容<!--<remote ip="192.168.8.40" name="tongbu"/>-->注释内容
</localpath>
修改后的内容为
<localpath watch="/data/cms/upload"><remote ip="10.0.20.2" name="backup"/>
</localpath>
2、修改31-34行内容——认证
<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"/>
修改后的内容为
<commonParams params="-aruz"/>
<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"/>
3、修改36-37行
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--defaultevery 60mins execute once-->
修改成我们刚刚创建好的logs目录
<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default eve
ry 60mins execute once-->
修改完成后最终的配置文件如下
[root@nbh_guanwang_a conf]# cat 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"/><common><user>root</user><group>root</group></common><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="false"/><closeWrite start="true"/><moveFrom start="true"/><moveTo start="true"/><attrib start="false"/><modify start="false"/></inotify><sersync><localpath watch="/data/cms/upload"><remote ip="10.0.20.2" name="backup"/></localpath><rsync><commonParams params="-aruz"/><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"/></rsync><failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60
mins 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>
七、开启sersync守护进程
首先配置全局环境变量,使得后同可以直接调用sersync命令
[root@nbh_guanwang_a conf]# echo 'export PATH=$PATH:/usr/local/sersync/bin'>>/etc/profile
[root@nbh_guanwang_a conf]# tail -1 /etc/profile
export PATH=$PATH:/usr/local/sersync/bin
[root@nbh_guanwang_a conf]# source /etc/profile
[root@nbh_guanwang_a conf]# which sersync
/usr/local/sersync/bin/sersync
启动sersync服务
serync -r -d -o /usr/local/sersync/conf/confxml.xml
-r初始化数据
-d后台启动
-o指定路径
如果需要将命令开启动,只需将命令去掉参数-r定入/etc/rc.local
启动后结果如下
[root@nbh_guanwang_a backup]# sersync -r -d -o /usr/local/sersync/conf/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -r rsync all the local files to the remote servers before the sersync work
option: -d run as a daemon
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is rsync_backup
passwordfile is /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /opt/backup && rsync -aruz -R --delete ./ --timeout=100 rsync_backup@192.168.1.3::rsync --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /opt/backup
八:测试数据同步
[root@nbh_guanwang_a sersync]# cd /opt/backup/
[root@nbh_guanwang_a backup]# ls
[root@nbh_guanwang_a backup]# ls -ll
total 0
[root@nbh_guanwang_a backup]# touch 123
[root@nbh_guanwang_a backup]# touch 1234
[root@nbh_guanwang_a backup]# touch 1235
[root@nbh_guanwang_a backup]# touch 12333
[root@nbh_guanwang_a backup]# ls -ll
total 0
-rw-r--r--. 1 root root 0 Sep 8 09:26 123
-rw-r--r--. 1 root root 0 Sep 8 09:26 12333
-rw-r--r--. 1 root root 0 Sep 8 09:26 1234
-rw-r--r--. 1 root root 0 Sep 8 09:26 1235
目标服务器查看同步情况
[root@nbh_guanwang_nginx backup]# ls -ll
total 0
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 123
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 12333
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 1234
-rw-r--r--. 1 rsync rsync 0 Sep 8 04:26 1235
测试结果表明数据同步正常,能够实时同步