什么是实时同步
实时同步是一种只要当前目录发生变化则会触发一个事件,事件触发后会将变化的目录同步至远程服务器。
sersync和rsync+inotify对比
提到数据同步就必然会谈到rsync
,一般简单的服务器数据传输会使用ftp/sftp
等方式,但是这样的方式效率不高,不支持差异化增量同步也不支持实时传输。针对数据实时同步需求大多数人会选择rsync+inotify-tools
的解决方案,但是这样的方案也存在一些缺陷(文章中会具体指出),sersync
是国人基于前两者开发的工具,不仅保留了优点同时还强化了实时监控,文件过滤,简化配置等功能,帮助用户提高运行效率,节省时间和网络资源。
sersync项目实战
1)环境准备
角色 | 外网IP(NAT) | 内网IP(LAN) | 安装工具 |
---|---|---|---|
web01 | eth0:10.0.0.7 | eth1:172.16.1.7 | 部署代码(提交作业) |
nfs-server | eth0:10.0.0.31 | eth1:172.16.1.31 | rsync+inotify+sersync |
backup | eth0:10.0.0.41 | eth1:172.16.1.41 | rsync-server |
1.实时同步哪台服务器的目录,那么就在哪台服务器上安装sersync
2.只要安装sersync
就必须安装rsync
和inotify
安装rsync的服务端(backup)
1)安装rsync服务
[root@backup ~]# yum install -y rsync
2)配置文件
[root@backup ~]# vim /etc/rsyncd.conf uid = wwwgid = wwwport = 873fake super = yesuse chroot = nomax connections = 200timeout = 600ignore errorsread only = falselist = falseauth users = rsync_backupsecrets file = /etc/rsync.passwdlog file = /var/log/rsyncd.log#####################################[zls]comment = welcome to oldboyedu backup!path = /backup[nfs]comment = welcome to oldboyedu backup!path = /data
3)创建用户
[root@backup ~]# groupadd www -g 666[root@backup ~]# useradd www -u 666 -g 666 -s /sbin/nologin -M
4)创建目录并授权
[root@backup ~]# mkdir /data /backup[root@backup ~]# chown -R www.www /data/ /backup/
5)创建虚拟用户的密码文件并授权
[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd chmod 600 /etc/rsync.passwd
6)启动rsync服务
[root@backup ~]# systemctl start rsyncd
NFS服务端部署sersync
1)安装sersync需要依赖rsync
和inotify
[root@nfs ~]# yum install -y rsync inotify-tools
2)下载sersync
[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz[root@nfs01 ~]# lltotal 720-rw-------. 1 root root 1444 Jul 24 09:32 anaconda-ks.cfg-rwxr-xr-x. 1 root root 473 Jul 24 10:50 host_ip.sh-rw-r--r-- 1 root root 727290 Aug 7 16:41 sersync2.5.4_64bit_binary_stable_final.tar.gz
3)部署sersync
源码包:解压 生成 编译 安装
解压:
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz[root@nfs01 ~]# lltotal 720-rw-------. 1 root root 1444 Jul 24 09:32 anaconda-ks.cfgdrwxr-xr-x 2 root root 41 Oct 26 2011 GNU-Linux-x86-rwxr-xr-x. 1 root root 473 Jul 24 10:50 host_ip.sh-rw-r--r-- 1 root root 727290 Aug 7 16:41 sersync2.5.4_64bit_binary_stable_final.tar.gz
4)移动并改名
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
5)编辑配置文件
[root@nfs ~]# vim /usr/local/sersync/confxml.xml <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="true"/> <modify start="true"/> </inotify>----------------------------------------------------------------------------------------- <sersync> #监控的目录,改成/data <localpath watch="/opt/tongbu"> #推送的IP(backup服务的IP)172.16.1.41 ,name是模块名 <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> <rsync> #执行rsync的参数改成 -az <commonParams params="-artuz"/> #虚拟用户的用户名和密码文件,开启认证start=true rsync_backup /etc/rsync.pass <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> #设置超时时间 <timeout start="true" 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="600"><!--600mins--> <crontabfilter start="false"> <exclude expression="*.php"></exclude> <exclude expression="info/*"></exclude> </crontabfilter> </crontab> <plugin start="false" name="command"/> </sersync>#完整配置文件:[root@nfs ~]# cat /usr/local/sersync/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="true"/> <modify start="true"/> </inotify> <sersync> <localpath watch="/data"> <remote ip="172.16.1.41" name="nfs"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-az"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="true" 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="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>
6)创建虚拟用户的密码文件,并授权
mkdir zhp/[root@nfs sersync]# echo '123' > /etc/rsync.pass[root@nfs sersync]# chmod 600 /etc/rsync.pass
7)查看帮助
[root@nfs sersync]# /usr/local/sersync/sersync2 -hset the system paramexecute:echo 50000000 > /proc/sys/fs/inotify/max_user_watchesexecute:echo 327679 > /proc/sys/fs/inotify/max_queued_eventsparse the command param_______________________________________________________参数-d:启用守护进程模式参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍c参数-n: 指定开启守护线程的数量,默认为10个参数-o:指定配置文件,默认使用confxml.xml文件参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块参数-m:单独启用其他模块,使用 -m socket 开启socket模块参数-m:单独启用其他模块,使用 -m http 开启http模块不加-m参数,则默认执行同步程序________________________________________________________________
8)启动sersync
[root@nfs data]# /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml(最后一行会看到execute command: cd /zhp && rsync -az -R --delete ./ 172.16.1.41::zhp >/dev/null 2>&1 的命令)
------------------------------------------------rsync服务端--------------------------------------------------
1)安装
[root@backup ~]# yum install -y rsync
2)改配置
[root@backup ~]# vim /etc/rsyncd.confuid = rsyncgid = rsyncport = 873fake super = yesuse chroot = nomax connections = 200timeout = 600ignore errorsread only = falselist = falseauth users = rsync_backupsecrets file = /etc/rsync.passwdlog file = /var/log/rsyncd.log#####################################[zhp]comment = welcome to oldboyedu backup!path = /backup
3)创建系统用户
[root@backup ~]# useradd rsync -s /sbin/nologin -M
4)创建虚拟用户及密码文件并授权
[root@backup ~]# echo 'rsync_backup:123' > /etc/rsync.passwd[root@backup ~]# chmod 600 /etc/rsync.passwd
5)创建目录并授权
[root@backup ~]# mkdir /backup[root@backup ~]# chown -R rsync.rsync /backup
6)启动服务并加入开机自启
[root@backup ~]# systemctl start rsyncd[root@backup ~]# systemctl enable rsyncd
----------------------------------------------------------rsync的客户端(nfs)------------------------------------------------------------------
1)安装sersync(rsync+inotify)
[root@nfs ~]# yum install -y rsync inotify-tools
2)安装sersync
下载:
[root@nfs ~]# wget https://raw.githubusercontent.com/wsgzao/sersync/master/sersync2.5.4_64bit_binary_stable_final.tar.gz
解压:
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
移动并改名:
[root@nfs ~]# mv GNU-Linux-x86 /usr/local/sersync
3)修改配置文件
[root@nfs ~]# vim /usr/local/sersync/confxml.xml <inotify> <delete start="true"/> <createFolder start="true"/> <createFile start="true"/> <closeWrite start="true"/> <moveFrom start="true"/> <moveTo start="true"/> <attrib start="true"/> <modify start="true"/> </inotify> <sersync> <localpath watch="/zhp"> <remote ip="172.16.1.41" name="zhp"/> <!--<remote ip="192.168.8.39" name="tongbu"/>--> <!--<remote ip="192.168.8.40" name="tongbu"/>--> </localpath> <rsync> <commonParams params="-az"/> <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pas"/> <userDefinedPort start="false" port="874"/><!-- port=874 --> <timeout start="true" time="100"/><!-- timeout=100 --> <ssh start="false"/> </rsync>
4)创建目录
[root@nfs ~]# mkdir /zls
5)创建密码文件并授权
[root@nfs ~]# echo '123' > /etc/rsync.pass[root@nfs ~]# chmod 600 /etc/rsync.pass
6)启动sersync
[root@nfs ~]# /usr/local/sersync/sersync2 -rdo /usr/local/sersync/confxml.xml
[root@nfs01 zhp]# mkdir 789[root@backup ~]# ll /backup/ 发现目录789已过来了
[root@backup ~]# yum install -y mailx[root@backup ~]# vim check_md5.sh#!/bin/bashPATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/binH=`hostname`IP=`ifconfig eth1|awk 'NR==2{print $2}'`DATE=`date +%F`SRC=${H}_${IP}_${DATE}md5sum -c /backup/res.txt|mail -s "${DATE} check backup" 1643168583@qq.comfind /backup -type d -mtime +180|xargs rm -fr~ ~[root@backup ~]# /etc/mail.rcset bsdcompatset from=1643168583@qq.comset smtp=smtps://smtp.qq.com:465set smtp-auth-user=1643168583@qq.comset smtp-auth-password=iworbddzcexfcgacset smtp-auth=loginset ssl-verify=ignoreset nss-config-dir=/etc/pki/nssdb/#每天凌晨1点做备份 by:zhp at:2019.8.700 00 * * * /bin/sh /root/check_md5.sh &>/dev/null[root@backup ~]# crontab -e