实时将生产服务器同步到灾备服务器

  • 服务器 A(生产服务器)
  • 服务器 B(灾备服务器)

https://wsgzao.github.io/post/sersync/

inotify 优化

/proc/sys/fs/inotify 的三个文件

  • max_user_watches:设置 inotifywait 或 inotifywatch 命令可以监控文件数量(单进程)
  • max_user_instances:设置每个用户可以运行的 inotifywait 或 inotifywatch 命令的进程数
  • max_queued_events:设置 inotify 实例事件(event)队列可容纳的事件数量
1
2
3
ll /proc/sys/fs/inotify
echo "5000000" >/proc/sys/fs/inotify/max_user_watches
echo "5000000" >/proc/sys/fs/inotify/max_queued_events

服务器 A

安装 rsync

下载地址 & 配置

1
2
3
4
5
6
7
wget https://rsync.samba.org/ftp/rsync/rsync-3.2.7.tar.gz
tar zxvf rsync-3.2.7.tar.gz
cd rsync-3.2.7
./configure --prefix=/app/service/rsync
make && make install

yum install rsync

安装 inotify-tools

1
2
3
4
5
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar zxf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/app/service/inotify-tools
make && make install

安装 sersync

1
2
3
4
5
6
7
8
9
10
wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
tar zxf sersync2.5.4_64bit_binary_stable_final.tar.gz
mv GNU-Linux-x86/ /app/service/sersync
cd /app/service/sersync
#配置密码文件,用于访问服务器 B 上的 rsync 服务端,跟服务器 B 保持一致
echo "rsync" > /app/service/sersync/user.pass
#修改权限
chmod 600 /app/service/sersync/user.pass
#修改 confxml.conf
vi /app/service/sersync/confxml.xml

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port="8008"></host>
<debug start="true"/>
<fileSystem xfs="false"/>
<filter start="false">
<exclude expression="(.*)\.php"></exclude>
<exclude expression="^data/*"></exclude>
</filter>
<inotify>
<delete start="true"/>
<createFolder start="true"/>
<createFile start="false"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="false"/>
<modify start="false"/>
</inotify>

<sersync>
<localpath watch="/home/"> <!-- 这里填写服务器A要同步的文件夹路径-->
<remote ip="8.8.8.8" name="rsync"/> <!-- 这里填写服务器B的IP地址和模块名-->
<!--<remote ip="192.168.28.39" name="tongbu"/>-->
<!--<remote ip="192.168.28.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="rsync" passwordfile="/app/service/sersync/user.pass"/> <!-- rsync+密码文件 这里填写服务器B的认证信息-->
<userDefinedPort start="false" port="873"/><!-- 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="/home/demo">
<deshost ip="210.36.158.xxx" port="8009"/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cdn.markdream.com/site/">
<cdninfo domainname="cdn.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
<sendurl base="http://cdn.markdream.com/cms"/>
<regexurl regex="false" match="cdn.markdream.com/site([/a-zA-Z0-9]*).cdn.markdream.com/images"/>
</localpath>
</plugin>
</head>

运行 sersync

1
2
3
4
5
6
7
nohup /app/service/sersync/sersync2 -r -d -o /app/service/sersync/confxml.xml >/app/local/sersync/rsync.log 2>&1 &
nohup /app/service/sersync/sersync2 -r -d -o /app/service/sersync/img.xml >/app/local/sersync/img.log 2>&1 &

-d:启用守护进程模式
-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
-n: 指定开启守护线程的数量,默认为10个
-o:指定配置文件,默认使用confxml.xml文件

服务器 B

安装 rsync

下载地址 & 配置

1
2
3
4
5
6
7
wget https://rsync.samba.org/ftp/rsync/rsync-3.2.7.tar.gz
tar zxvf rsync-3.2.7.tar.gz
cd rsync-3.2.7
./configure --prefix=/app/service/rsync
make && make install

yum install rsync

创建配置文件:vi /etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
port = 873
uid = root
gid = root
use chroot = no
max connections = 102400
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
lock file = /var/run/rsync.lock
#exclude = lost+found/
#transfer logging = yes
timeout = 900
ignore errors = yes
read only = no
#ignore nonreadable = yes
#dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
#comment = hello world
#motd file = /etc/rsyncd.motd
auth users = app
secrets file = /etc/rsyncd.secretsd

[es]
comment = es_backups
path = /app/share/es_backups

[upload]
comment = upload
path = /app/share/upload
1
2
3
4
5
6
7
echo "app:Z6WD9owzyD" > /etc/rsyncd.secretsd
chmod 600 /etc/rsyncd.secretsd
chmod 600 /etc/rsyncd.conf
rsync –daemon
netstat -plan | grep 873
echo "/usr/local/bin/rsync --daemon" >> /etc/rc.local
chmod +x /etc/rc.d/rc.local

CentOS 默认使用 xinetd 启动 rsync

1
2
3
4
5
6
7
8
9
10
11
12
13
yum install xinetd
vi /etc/xinetd.d/rsync
service rsync
{
disable = yes
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = –daemon
log_on_failure += USERID
}
systemctl start xinetd