实时同步方案:
1、rsync+inotify (不推荐,inotify是对本地文件或目录的实时监控)
2、rsync+sersync (推荐,是inotify的升级版,功能更强大,本篇博客就使用该方案)
sersync是基于inotify开发的,类似于inotify-tools的工具,Sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或者某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的文件或者目录,因此效率更高。
| 服务器名称 | IP地址 | 服务 |
|---|---|---|
| web服务器(客户端) | 10.0.8.59 | rsync、sersync |
| backup服务器(服务端) | 10.0.8.70 | rsync |
- # yum install rsync -y
- 已加载插件:fastestmirror
- Determining fastest mirrors
- base | 3.6 kB 00:00:00
- epel | 4.7 kB 00:00:00
- extras | 2.9 kB 00:00:00
- updates | 2.9 kB 00:00:00
- (1/5): epel/group_gz | 97 kB 00:00:00
- (2/5): epel/updateinfo | 1.0 MB 00:00:00
- (3/5): epel/primary_db | 7.0 MB 00:00:00
- (4/5): extras/primary_db | 247 kB 00:00:00
- (5/5): updates/primary_db | 17 MB 00:00:00
- 正在解决依赖关系
- --> 正在检查事务
- ---> 软件包 rsync.x86_64.0.3.1.2-10.el7 将被 安装
- --> 解决依赖关系完成
-
- 依赖关系解决
-
- =============================================================================================================================================================================================
- Package 架构 版本 源 大小
- =============================================================================================================================================================================================
- 正在安装:
- rsync x86_64 3.1.2-10.el7 base 404 k
-
- 事务概要
- =============================================================================================================================================================================================
- 安装 1 软件包
-
- 总下载量:404 k
- 安装大小:815 k
- Downloading packages:
- rsync-3.1.2-10.el7.x86_64.rpm | 404 kB 00:00:00
- Running transaction check
- Running transaction test
- Transaction test succeeded
- Running transaction
- 正在安装 : rsync-3.1.2-10.el7.x86_64 1/1
- 验证中 : rsync-3.1.2-10.el7.x86_64 1/1
-
- 已安装:
- rsync.x86_64 0:3.1.2-10.el7
-
- 完毕!
1、添加配置文件
vim /etc/rsyncd.conf
- # 全局配置
- uid = rsync
- gid = rsync
- use chroot = no
- max connections = 4
- pid file = /var/run/rsyncd.pid
- lock file = /var/run/rsyncd.lock
- log file = /data/rsync-log/rsyncd.log
- timeout = 900
- fake super = yes
- auth users = yukw # 虚拟用户,全局配置
- secrets file = /data/rsync-passwd/rsync.passwd
- hosts allow = 10.0.0.0/8 # 这里注意如果写成 10.0.0.0/24会出现客户端同步时找不到模块的报错
- hosts deny = *
- read only = false
- list = false
-
- [Qyun-10.0.8.59] # 模块一
- Comment = 10.0.8.59:/data/service/Automatic-deployment/Qyun-10.0.8.59
- path = /data/rsync-backup/Qyun-10.0.8.59
-
- [Qyun-10.0.8.59-nginx]
- Comment = 10.0.8.59:/data/services/nginx/conf
- path = /data/rsync-backup/Qyun-10.0.8.59-nginx
如果写成 hosts allow = 10.0.0.0/24 后出现的报错信息

2、创建目录、文件、修改权限、后台启动服务
- # mkdir /data/{rsync-passwd,rsync-backup,rsync-log}
- # mkdir /data/rsync-backup/{Qyun-10.0.8.59,Qyun-10.0.8.59-nginx}
- # cd /data/
- # ll
- # useradd -M -s /sbin/nologin rsync
- # chown -R rsync.rsync rsync-backup/
- # chown -R rsync.rsync rsync-log
- # chown rsync.rsync rsync-passwd
- # cd rsync-passwd/
- # vim rsync.passwd
- yukw:123
- # chmod 600 rsync.passwd
- # cd /data/rsync-passwd/
- # ll
- 总用量 4
- -rw------- 1 root root 15 8月 25 10:50 rsync.passwd
- # cat rsync.passwd
- yukw:123
- # rsync --daemon # 后台启动
- # ps -ef | grep rsync
- root 1342 1 0 09:50 ? 00:00:00 rsync --daemon
- root 5102 1420 0 11:09 pts/0 00:00:00 grep --color=auto rsync
- 或者通过systemctl启动
- # systemctl start rsyncd.service
- # systemctl stauts rsyncd.service
- # systemctl enable rsyncd.service

1、安装sersync
- # wget https://down.whsir.com/downloads/sersync2.5.4_64bit_binary_stable_final.tar.gz
- # ll
- # tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
- # ll
- # mv GNU-Linux-x86/ sersync-2.5.4
- # ln -svf sersync-2.5.3 sersync
- # cd sersync/
- # ll
- # cp sersync2 /usr/bin/sersync
2、修改配置文件
# vim confxml.xml
- "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="false"/>
- inotify>
-
- <sersync>
- <localpath watch="/data/service/Automatic-deployment/Qyun-10.0.8.59">
- <remote ip="10.0.8.70" name="Qyun-10.0.8.59-nginx"/>
-
- localpath>
- <rsync>
- <commonParams params="-artuz"/>
- <auth start="true" users="yukw" passwordfile="/etc/rsync.passwd"/>
- <userDefinedPort start="false" port="874"/>
- <timeout start="false" time="100"/>
- <ssh start="false"/>
- rsync>
- <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/>
- <crontab start="false" schedule="600">
- <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"/>
- <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>
3、创建密码文件(仅需要配置密码就好了,和服务端的密码一致)
- # vim /etc/rsync.passwd
- 123
- # chmod 600 /etc/rsync.passwd
4、启动sersync
- # sersync -dro /data/service/sersync/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: -d run as a daemon
- option: -r rsync all the local files to the remote servers before the sersync work
- option: -o config xml name: /data/service/sersync/confxml.xml
- daemon thread num: 10
- parse xml config file
- host ip : localhost host port: 8008
- will ignore the inotify createFile event
- daemon start,sersync run behind the console
- use rsync password-file :
- user is yukw
- passwordfile is /etc/rsync.passwd
- 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 /data/test && rsync -artuz -R --delete ./ yukw@10.0.8.70::Qyun-192.168.1.2-nginx --password-file=/etc/rsync.passwd >/dev/null 2>&1
- run the sersync:
- watch path is: /data/service/Automatic-deployment/Qyun-10.0.8.59
参数说明
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
参数-n:指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
- # data/rsync-backup/Qyun-192.168.1.2-nginx
- # ll
- # cp confxml.xml confxml-1.xml
- # vim confxml-1.xml
- 修改端口为8009、修改同步路径、修改模块名称
- 3
"localhost" port="8009"> - 24
"/data/service/Automatic-deployment/Qyun-10.0.8.59"> - 25
"10.0.8.70" name="Qyun-10.0.8.59"/> -
-
-
- # 启动服务
- # sersync -dro /data/service/sersync/confxml-1.xml
关于inotify的一些操作,它的功能可以监控一些目录下文件的变更,通过 -e 参数指定事件类型,如果创建、删除、修改等
开启两个窗口:
窗口1:inotifywait -mrq --timefmt "%F%T" --format "%T %w%f %e" -e create,delete,modify /data/test/ 执行后窗口会hang住
窗口2:在 /data/test 目录下新建、删除、修改文件或者目录
这时在窗口1 就会有实时的事件日志打印出来
- # yum install inotify* -y
- # inotifywait -mrq --timefmt "%F%T" --format "%T %w%f %e" -e create,delete,modify /data/test/
- 2022-08-2414:10:34 /data/test/user02.txt MODIFY
- 2022-08-2414:10:41 /data/test/user03.txt MODIFY
- 2022-08-2414:10:47 /data/test/cc.txt CREATE
- 2022-08-2414:10:57 /data/test/bb CREATE,ISDIR
好了,这就是rsync+sersync实时同步文件的方法了,如有问题可与博主一起交流讨论