背景说明: 要把源数据服务器的 /data/file 目录实时同步到目标服务上。
1、环境准备
角色 | IP | 名称 | 安装软件 |
---|---|---|---|
sersync服务器 | 192.168.99.9 | 源数据服务器 | sersync + rsync |
rsync服务器 | 192.168.99.123 | 目标服务器 | rsync |
2、配置rsync服务器
- 关闭SELINUX && 关闭防火墙
bash
# 禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service
- 安装rsync服务
bash
yum install rsync -y
3、部署rsync服务器
- 创建目录
bash
mkdir /opt/rsync
chmod 777 /opt/rsync
# 重要 目标机器须要先创建同步目录
mkdir /data/file
- 创建认证文件
bash
echo pusher:rsync > /opt/rsync/server.pass
chmod 600 /opt/rsync/server.pass
- 配置rsync
vim /etc/rsyncd.conf
bash
uid = root
gid = root
port = 873
max connections = 2000
pid file = /opt/rsync/rsyncd.pid
log file = /opt/rsync/rsyncd.log
lock file = /opt/rsync/rsync.lock
[file]
path = /data/file
comment = file
read only = no
auth users = pusher
secrets file = /opt/rsync/server.pass
hosts allow = 192.168.99.0/24
- 启动rsync服务
bash
systemctl restart rsyncd.service
#
systemctl status rsyncd.service
4、配置sersync服务器
- 关闭SELINUX && 关闭防火墙
bash
# 禁用selinux
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
# 关闭防火墙
systemctl disable firewalld.service
systemctl stop firewalld.service
- 安装rsync服务
bash
yum install rsync -y
- 创建认证文件
bash
echo rsync > /etc/rsync.pass
chmod 600 /etc/rsync.pass
- 手工测试rsync同步情况
bash
# 创建目录和文件
mkdir -p /data/file
cd /data/file
touch {1..10}
# 手工执行同步(只有手工同步成功下面的自动同步才不会有问题)
cd /data/file && rsync -artuz -R --delete ./ pusher@192.168.99.123::file --password-file=/etc/rsync.pass
5、部署sersync服务器
- 安装sersync
软件下载地址:https://code.google.com/archive/p/sersync/downloads
(须要魔法) 下载最新文件 sersync2.5.4_64bit_binary_stable_final.tar.gz
bash
# 解压文件
tar -zxf sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
mv /usr/local/GNU-Linux-x86 /usr/local/sersync
cd /usr/local/sersync
- 配置confxml.xml (这里是我在用的配置)
xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="false"/>
<fileSystem xfs="true"/><!--如果系统linux7以上,需要将xfs="false"修改成xfs="true",否则sersync无法递归监控-->
<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="true"/>
</inotify>
<sersync>
<localpath watch="/data/file">
<remote ip="192.168.99.123" name="file"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="pusher" passwordfile="/etc/rsync.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>
- 启动sersync服务
bash
[root@backup]# /usr/local/sersync/sersync2 -d -r -o /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
will ignore the inotify createFile event
daemon start,sersync run behind the console
use rsync password-file :
user is pusher
passwordfile is /etc/rsync.pass
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/file && rsync -artuz -R --delete ./ pusher@192.168.99.123::file --password-file=/etc/rsync.pass >/dev/null 2>&1
run the sersync:
watch path is: /data/file
- 开机启动
bash
6、验证
源服务器创建文件,查看目标服务器是否同步。
注:/tmp/rsync_fail_log.sh 文件可以查看异常文件。