实时同步目的
1.解决单点故障
2.保证数据的一致性
sersync做实时同步的依赖
1.rsync
2.inotify
Inotify 是一个通知接口,用来监控文件系统的各种变化,如果文件存取,删除,移动。可以非常方便地实现文件
异动告警,增量备份,并针对目录或文件的变化及时作出响应。rsync+inotify可以实触发式实时同步增量备份
sersync官网
sersync实战
环境准备
主机名 | WanIP | LanIP | 角色 | 部署服务 |
---|---|---|---|---|
web01 | 10.0.0.7 | 172.16.1.7 | nfs客户端,rsync客户端 | httpd、php、nfs-utils |
web02 | 10.0.0.8 | 172.16.1.8 | nfs客户端,rsync客户端 | httpd、php、nfs-utils |
nfs | 10.0.0.31 | 172.16.1.31 | rsync客户端、nfs服务端 | nfs-utils、rsync、inotify-tools、sersync |
backup | 10.0.0.41 | 172.16.1.41 | rsync服务端、nfs服务端(备机) | nfs、rsync |
部署sersync
# 1.安装sersync的依赖
[root@nfs ~]# yum install -y rsync inotify-tools
# 2.下载sersync
[root@nfs ~]# wget https://github.com/wsgzao/sersync/archive/refs/heads/master.zip
[root@nfs ~]# wget https://download.wodeyumengouwo.com/nginx/sersync2.5.4_64bit_binary_stable_final.tar.gz
# 3.创建安装目录
[root@nfs ~]# mkdir /app
# 4.解压sersync(二进制包 解压即用)
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /app/
[root@nfs ~]# ll /app/
total 0
drwxr-xr-x 2 root root 41 Oct 26 2011 GNU-Linux-x86
# 5.改名
[root@nfs ~]# mv /app/GNU-Linux-x86/ /app/sersync
# 6.修改配置文件
[root@nfs ~]# vim /app/sersync/confxml.xml
## 配置文件中<inotify>段内容 是inotify监控动作配置 false全部改成true 相当于以下指令 指定了监视的动作
## inotifywait -mrq --format %w %f -e create,delete,attrib,close_write /data
<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>段中的内容相当于以下指令
## rsync -az --delete /data rsync_backup@172.16.1.41::nfs_data --password-file=/etc/rsync.pass
## (指定的选项)(推送数据目录)(匿名用户) (IP地址) (模块名) (指定密码文件)
<sersync>
<!-- 监控目录,推送数据的目录 /data -->
<localpath watch="/data">
<!-- rsync服务端的IP地址:172.16.1.41 和 模块名:backup -->
<remote ip="172.16.1.41" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<!-- rsync命令执行时的选项 -->
<commonParams params="-az --delete"/>
<!-- rsync命令认证,打开认证,rsync的匿名用户,指定密码文件 -->
<auth start="true" users="rsync_backup" passwordfile="/etc/rsyncd.pass"/>
<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="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
-----------------纯净版-------------------------------
<?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="backup"/>
<!--<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/rsynd.pass"/>
<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="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>
------------------------end--------------------------
# 7.创建密码文件
[root@nfs ~]# echo '123' > /etc/rsyncd.pass
# 8.授权密码文件
[root@nfs ~]# chmod 600 /etc/rsyncd.pass
## 查看sersync的执行命令sersync2的帮助文档
[root@nfs sersync]# ./sersync2 -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________
## 9.创建推送数据目录
[root@nfs ~]# mkdir /data
# 10.启动sersync
[root@nfs sersync-2.5.4]# /app/sersync/sersync2 -rdo /app/sersync/confxml.xml
# 11.测试是否同步
[root@nfs ~]# echo '测试' > /data/cs.txt
[root@nfs ~]# ll /data/
total 4
-rw-r--r-- 1 root root 7 Jul 28 20:48 cs.txt
[root@backup backup]# ll
total 4
-rw-r--r-- 1 rsync rsync 7 Jul 28 20:48 cs.txt
## 报错排查方法 指令选项加v 查看报错内容
rsync -avz --delete -R --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.pass
用systemd管理sersync,添加开机自启
## 查看进程号的指令
[root@nfs ~]# pidof sersync2
1389 1322 1255 1185 1145 1126
## 停止方式
[root@nfs ~]# pidof sersync2|xargs kill -3
## 重启方式
[root@nfs ~]# pidof sersync2|xargs kill -1
### cd到systemd管理文件下 创建文件 并写入以下配置
[root@nfs ~]# cd /usr/lib/systemd/system
[root@nfs system]# vim sersync.service
[Unit] # 描述信息可以没有 但这个标签要有
# 服务的描述信息
Description=sersync
After=network.target
[Service]
Type=forking
# 指定服务的启动命令
ExecStart=/app/sersync/sersync2 -rdo /app/sersync/confxml.xml
# 指定服务的停止命令
ExecReload=kill -3 pidof sersync2|xargs kill -3
# 指定服务的重启命令
ExecStop=kill -1 pidof sersync2|xargs kill -1
[Install]
# 指定服务开机自启的运行级别
WantedBy=multi-user.target
------------------------纯净版------------------------
[Unit]
Description=sersync
After=network.target
[Service]
Type=forking
ExecStart=/app/sersync/sersync2 -rdo /app/sersync/confxml.xml
ExecReload=kill -3 pidof sersync2|xargs kill -3
ExecStop=kill -1 pidof sersync2|xargs kill -1
[Install]
WantedBy=multi-user.target
--------------------------end------------------------
## 添加sersync.service文件的启动权限
[root@nfs system]# chmod +x sersync.service
## 启动sersync服务
[root@nfs ~]# systemctl start sersync
## 查看进程
[root@nfs ~]# ps -ef |grep [s]ersync
## 加入开机自启
[root@nfs ~]# systemctl enable sersyncd
# 1.安装sersync的依赖
[root@nfs ~]# yum install -y rsync inotify-tools
# 2.下载sersync
[root@nfs ~]# wget https://github.com/wsgzao/sersync/archive/refs/heads/master.zip
[root@nfs ~]# wget https://download.wodeyumengouwo.com/nginx/sersync2.5.4_64bit_binary_stable_final.tar.gz
# 3.创建安装目录
[root@nfs ~]# mkdir /app
# 4.解压sersync(二进制包 解压即用)
[root@nfs ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /app/
[root@nfs ~]# ll /app/
total 0
drwxr-xr-x 2 root root 41 Oct 26 2011 GNU-Linux-x86
# 5.改名
[root@nfs ~]# mv /app/GNU-Linux-x86/ /app/sersync
# 6.修改配置文件
[root@nfs ~]# vim /app/sersync/confxml.xml
## 配置文件中<inotify>段内容 是inotify监控动作配置 false全部改成true 相当于以下指令 指定了监视的动作
## inotifywait -mrq --format %w %f -e create,delete,attrib,close_write /data
<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>段中的内容相当于以下指令
## rsync -az --delete /data rsync_backup@172.16.1.41::nfs_data --password-file=/etc/rsync.pass
## (指定的选项)(推送数据目录)(匿名用户) (IP地址) (模块名) (指定密码文件)
<sersync>
<!-- 监控目录,推送数据的目录 /data -->
<localpath watch="/data">
<!-- rsync服务端的IP地址:172.16.1.41 和 模块名:backup -->
<remote ip="172.16.1.41" name="backup"/>
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<!-- rsync命令执行时的选项 -->
<commonParams params="-az --delete"/>
<!-- rsync命令认证,打开认证,rsync的匿名用户,指定密码文件 -->
<auth start="true" users="rsync_backup" passwordfile="/etc/rsyncd.pass"/>
<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="600"><!--600mins-->
<crontabfilter start="false">
<exclude expression="*.php"></exclude>
<exclude expression="info/*"></exclude>
</crontabfilter>
</crontab>
<plugin start="false" name="command"/>
</sersync>
-----------------纯净版-------------------------------
<?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="backup"/>
<!--<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/rsynd.pass"/>
<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="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>
------------------------end--------------------------
# 7.创建密码文件
[root@nfs ~]# echo '123' > /etc/rsyncd.pass
# 8.授权密码文件
[root@nfs ~]# chmod 600 /etc/rsyncd.pass
## 查看sersync的执行命令sersync2的帮助文档
[root@nfs sersync]# ./sersync2 -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
________________________________________________________________
## 9.创建推送数据目录
[root@nfs ~]# mkdir /data
# 10.启动sersync
[root@nfs sersync-2.5.4]# /app/sersync/sersync2 -rdo /app/sersync/confxml.xml
# 11.测试是否同步
[root@nfs ~]# echo '测试' > /data/cs.txt
[root@nfs ~]# ll /data/
total 4
-rw-r--r-- 1 root root 7 Jul 28 20:48 cs.txt
[root@backup backup]# ll
total 4
-rw-r--r-- 1 rsync rsync 7 Jul 28 20:48 cs.txt
## 报错排查方法 指令选项加v 查看报错内容
rsync -avz --delete -R --delete ./ rsync_backup@172.16.1.41::backup --password-file=/etc/rsyncd.pass
## 查看进程号的指令
[root@nfs ~]# pidof sersync2
1389 1322 1255 1185 1145 1126
## 停止方式
[root@nfs ~]# pidof sersync2|xargs kill -3
## 重启方式
[root@nfs ~]# pidof sersync2|xargs kill -1
### cd到systemd管理文件下 创建文件 并写入以下配置
[root@nfs ~]# cd /usr/lib/systemd/system
[root@nfs system]# vim sersync.service
[Unit] # 描述信息可以没有 但这个标签要有
# 服务的描述信息
Description=sersync
After=network.target
[Service]
Type=forking
# 指定服务的启动命令
ExecStart=/app/sersync/sersync2 -rdo /app/sersync/confxml.xml
# 指定服务的停止命令
ExecReload=kill -3 pidof sersync2|xargs kill -3
# 指定服务的重启命令
ExecStop=kill -1 pidof sersync2|xargs kill -1
[Install]
# 指定服务开机自启的运行级别
WantedBy=multi-user.target
------------------------纯净版------------------------
[Unit]
Description=sersync
After=network.target
[Service]
Type=forking
ExecStart=/app/sersync/sersync2 -rdo /app/sersync/confxml.xml
ExecReload=kill -3 pidof sersync2|xargs kill -3
ExecStop=kill -1 pidof sersync2|xargs kill -1
[Install]
WantedBy=multi-user.target
--------------------------end------------------------
## 添加sersync.service文件的启动权限
[root@nfs system]# chmod +x sersync.service
## 启动sersync服务
[root@nfs ~]# systemctl start sersync
## 查看进程
[root@nfs ~]# ps -ef |grep [s]ersync
## 加入开机自启
[root@nfs ~]# systemctl enable sersyncd
Comments | NOTHING