sersync 介绍
sersync类似于inotify,同样用于监控,但它克服了inotify的缺点.
inotify最大的不足是会产生重复事件,或者同一个目录下多个文件的操作会产生多个事件,例如,当监
控目录中有5个文件时,删除目录时会产生6个监控事件,从而导致重复调用rsync命令。另外比如:vim
文件时,inotify会监控到临时文件的事件,但这些事件相对于rsync来说是不应该被监控的
sersync 优点:
sersync是使用c++编写,而且对linux系统文件系统产生的临时文件和重复的文件操作进行过滤,
所以在结合rsync同步的时候,节省了运行时耗和网络资源。因此更快。
sersync配置很简单,其中提供了静态编译好的二进制文件和xml配置文件,直接使用即可
sersync使用多线程进行同步,尤其在同步较大文件时,能够保证多个服务器实时保持同步状态
sersync有出错处理机制,通过失败队列对出错的文件重新同步,如果仍旧失败,则按设定时长对
同步失败的文件重新同步
sersync不仅可以实现实时同步,另外还自带crontab功能,只需在xml配置文件中开启,即也可以
按要求隔一段时间整体同步一次,而无需再额外配置crontab功能
sersync 可以二次开发
sersync项目地址: https://code.google.com/archive/p/sersync/
sersync下载地址:https://code.google.com/archive/p/sersync/downloads
案例:基于rsync daemon 实现 sersync
#在数据服务器上下载sersync,并拷贝至相应的目录,设置PATH变量
[root@sz-kx-centos8 ~]# wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
# 解压
[root@sz-kx-centos8 ~]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@sz-kx-centos8 ~]# cp -a GNU-Linux-x86 /usr/local/sersync
[root@sz-kx-centos8 ~]# echo 'PATH=/usr/local/sersync:$PATH' > /etc/profile.d/sersync.sh
[root@sz-kx-centos8 ~]# source /etc/profile.d/sersync.sh
[root@sz-kx-centos8 ~]# ll /usr/local/sersync/
total 1772
-rwxr-xr-x 1 root root 2214 Oct 26 2011 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26 2011 sersync2
#确认安装rsync客户端工具
[root@sz-kx-centos8 ~]# rpm -q rsync &> /dev/null || dnf -y install rsync
#备份sersync配置文件
[root@sz-kx-centos8 ~]# cp /usr/local/sersync/confxml.xml{,.bak}
#修改sersync配置文件
[20:47:13 root@sz-kx-centos8 ~]# 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="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/> #修改此行为true,文件属性变化后也会同步
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data/www"> #修改此行,需要同步的源目录或文件,建议同步目录
<remote ip="备份服务器IP" name="backup"/> #修改此行,指定备份服务器地址和rsync daemon的模块名,如果下面开启了ssh start,此时name为远程shell方式运行时的目标目录
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsyncuser" passwordfile="/etc/rsync.pas"/> #修
改此行为true,指定备份服务器的rsync配置的用户和密码文件
<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>
#创建连接rsynd服务器的用户密码文件,并必须修改权限
[root@sz-kx-centos8 ~]# echo centos > /etc/rsync.pas
[root@sz-kx-centos8 ~]# chmod 600 /etc/rsync.pas
#查看帮助
[root@sz-kx-centos8 ~]# 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参数,则默认执行同步程序
_______________________________________________________
#以后台方式执行同步
[root@sz-kx-centos8 ~]# sersync2 -dro /usr/local/sersync/confxml.xml
#如果同步失败,可以手动执行下面命令,观察过程
[root@sz-kx-centos8 ~]# cd /data/www && rsync -artuz -R --delete ./ rsyncuser@backup-server::backup --password-file=/etc/rsync.pas >/dev/null 2>&1
run the sersync:
watch path is: /data/www
#sersync支持多实例,也即监控多个目录时,只需分别配置不同配置文件,然后使用sersync2指定对应配置
文件运行
[root@sz-kx-centos8 ~]# sersync2 -rd -o /etc/sersync.d/nginx.xml
基于远程shell 实现 sersync
#不需要配置rsync daemon,只需要配置基于key验证的ssh即可
[root@sz-kx-centos8 ~]# ssh-keygen
[root@sz-kx-centos8 ~]# ssh-copy-id backup-server
#下载sersync,并拷贝至相应的目录,设置PATH变量同5.5.2
#修改sersync配置文件
[root@sz-kx-centos8 ~]# 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="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/> #修改此行为true
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/data/www"> #修改此行,指定源数据目录
<remote ip="备份服务器IP" name="/data/backup"/> #修改此行指定备份服务器地址和备份目标目录
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="root" passwordfile="/etc/rsync.pas"/> #必须修改 此行,不启用认证start=false
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="false" time="100"/><!-- timeout=100 -->
<ssh start="true"/> #修改此行为true,使用远程shell方式的rsync连接方式,无需在目标
主机上配置启动rsync daemon服务
#####################################以下行不需要修改
####################################
</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>
#将中间的行可以删除
</head>
[root@sz-kx-centos8 ~]# sersync2 -dro /usr/local/sersync/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
案例 基于LAMP搭建web服务数据同步
# 安装mysql做主从复制
[root@centos8 ~]# yum install mysql-server -y
[root@centos8 ~]# yum install mysql-server -y
开机启动mysql
[root@centos8 ~]# systemctl enable --now mysqld
[root@centos8 ~]# systemctl enable --now mysqld
配置主从
#主配置
[root@centos8 ~]# vim /etc/my.cnf
[mysqld]
server-id=28
log-bin=/data/mysql/mysql-bin
#创建目录并授权
[root@centos8 ~]# mkdir /data/mysql/ -p
[root@centos8 ~]# chown -R mysql.mysql /data/mysql
# 重新启动mysql服务
[root@centos8 ~]# systemctl restart mysqld
#从配置
[mysqld]
server_id=38
log-bin=/data/mysql/mysql-bin
read_only
#创建目录并授权
[root@centos8 ~]# mkdir /data/mysql/ -p
[root@centos8 ~]# chown -R mysql.mysql /data/mysql/
# 重新启动mysql服务
[root@centos8 ~]# systemctl restart mysqld
主进入数据库创建库并创建用户还有授权
mysql> create database db1;
Query OK, 1 row affected (0.00 sec)
mysql> use db1;
Database changed
mysql> show master logs;
+------------------+-----------+-----------+
| Log_name | File_size | Encrypted |
+------------------+-----------+-----------+
| mysql-bin.000002 | 156 | No |
+------------------+-----------+-----------+
1 row in set (0.00 sec)
# 创建主从同步用户
mysql> create user repluser@'172.31.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to repluser@'172.31.0.%';
Query OK, 0 rows affected (0.00 sec)
从配置
使用帮助
mysql> help change master to;
mysql> CHANGE MASTER TO
MASTER_HOST='172.31.0.28',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000002',
MASTER_LOG_POS=156;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave statusG
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.31.0.28
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1064
Relay_Log_File: centos8-relay-bin.000002
Relay_Log_Pos: 1232
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
# 创建WordPress库
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
# 创建WordPress用户
mysql> create user wordpress@'172.31.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
# 授权
mysql> grant all on wordpress.* to wordpress@'172.31.0.%';
Query OK, 0 rows affected (0.00 sec)
创建目录NFS做数据同步
[root@sz-kx-centos8 ~]# mkdir /data/wordpress -p
[root@sz-kx-centos8 ~]# chmod 777 /data/wordpress/
[root@sz-kx-centos8 ~]# vim /etc/exports
/data/wordpress 172.31.0.0/16(rw)
# 安装NFS
[root@centos8 ~]# yum install -y nfs-utils
[root@centos8 ~]# yum install -y nfs-utils
# 开机启动
[root@sz-kx-centos8 ~]# systemctl enable --now nfs-server.service
[root@sz-kx-centos8 ~]# systemctl enable --now nfs-server.service
# 重读配置
[root@sz-kx-centos8 ~]# exportfs -r
# 查看本机所有NFS共享
[root@sz-kx-centos8 ~]# exportfs -v
/data/wordpress
172.31.0.0/16(sync,wdelay,hide,no_subtree_check,sec=sys,rw,secure,root_squash,no_all_squash)
##安装web
[root@CentOS8 ~]# yum install httpd php-fpm php-json php-mysqlnd nfs-utils -y
[root@CentOS8 ~]# yum install httpd php-fpm php-json php-mysqlnd nfs-utils -y
# 开机启动
[root@localhost ~]# systemctl enable --now httpd php-fpm
[root@localhost ~]# systemctl enable --now httpd php-fpm
# 挂载
[root@localhost ~]# vim /etc/fstab
172.31.0.18:/data/wordpress/wp-content/uploads /var/www/html/wp-content/uploads nfs _netdev 0 0
[root@localhost ~]# vim /etc/fstab
172.31.0.18:/data/wordpress/wp-content/uploads /var/www/html/wp-content/uploads nfs _netdev 0 0
# 创建目录
[root@localhost ~]# mkdir /var/www/html/wp-content/uploads -p
[root@localhost ~]# mkdir /var/www/html/wp-content/uploads -p
[root@localhost ~]# mount -a
[root@localhost ~]# mount -a
# 查看能不能看远程主机的共享
[root@localhost ~]# showmount -e 172.31.0.18
Export list for 172.31.0.18:
/data/wordpress 172.31.0.0/16
# 授权改所属用户(在挂载的nfs主机改)
[root@sz-kx-centos8 /data/wordpress]# chown -R apache.apache /data/wordpress/*
数据同步(需要手动执行同步数据,很是不方便,所以使用以下的方法解决手动执行同步数据问题)
# master机安装软件包
[root@sz-kx-centos8 ~]# yum -y install inotify-tools
# back机安装
[root@localhost ~]# yum install -y rsync rsync-daemon
# back机操作
[root@localhost ~]# rsync --daemon
[root@localhost ~]# ss -tanlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:873 0.0.0.0:*
# back机操作
[root@localhost ~]# vim /etc/rsyncd.conf
[backup]
path = /data/wordpress/
read only = no
# 测试master机操作
[root@sz-kx-centos8 ~]# rsync rsync://172.31.0.58
backup
# 在master机器手动执行同步
[root@sz-kx-centos8 ~]# echo "centos" > /etc/rsync.pas
[root@sz-kx-centos8 ~]# chmod 600 /etc/rsync.pas
先把数据手工推送到back机器
[root@sz-kx-centos8 ~]# rsync /data/wordpress/ rsync://172.31.0.58::backup
[17:04:34 root@sz-kx-centos8 ~]# rsync -avz --delete --password-file=/etc/rsync.pas /data/wordpress rsyncuser@172.31.0.18::backup
推荐使用这个方式同步数据
sersync 实现实时数据同步
[root@sz-kx-centos8 ~]# wget https://raw.githubusercontent.com/orangle/sersync/master/release/sersync2.5.4_64bit_binary_stable_final.tar.gz
改配置文件按照上面的改即可
# 以后台方式执行同步
[root@sz-kx-centos8 ~]# sersync2 -dro /usr/local/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: /usr/local/sersync/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 rsyncuser
passwordfile is /etc/rsync.pas
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/wordpress && rsync -artuz -R --delete ./ rsyncuser@172.31.0.58::backup --password-file=/etc/rsync.pas >/dev/null 2>&1
run the sersync:
watch path is: /data/wordpress
# 如果同步失败,可以手动执行下面命令,观察过程
[root@sz-kx-centos8 ~]# cd /data/wordpress && rsync -artuz -R --delete ./ rsyncuser@172.31.0.58::backup --password-file=/etc/rsync.pas >/dev/null 2>&1
浏览器同步数据(也可以改成域名解析ip访问)
遇到报错
[root@sz-kx-centos8 ~]# rsync rsync://172.31.0.58
rsync: failed to connect to 172.31.0.58 (172.31.0.58): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(127) [Receiver=3.1.3]
解决方法:使用[root@sz-kx-centos8 ~]# telnet 172.31.0.58 873
看通不通,如果不通可能是防火墙没有关闭,关闭即可重新同步