这是个好软件,多年前就一直在使用,只是最近需要重新配置,所以把过程记下来。

服务器 A 为主服务器 192.168.1.2,B 为从服务器 192.168.1.3

双方均需要安装 rsync 且防火墙开启 873 端口,在 /etc/iptables.conf 添加如下规则:

1
-A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT

重启防火墙

1
iptables-restore < /etc/iptables

安装 rsync

1
apt-get install rsync

在 B 服务器上配置 rsync 服务

编辑 /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
uid=root
gid=root
#最大连接数
max connections=36000
#默认为true,修改为no,增加对目录文件软连接的备份
use chroot=no
#定义日志存放位置
log file=/data/log/rsync/rsyncd.log
#忽略无关错误
ignore errors = yes
#设置rsync服务端文件为读写权限
read only = no

[www]
#注释
comment = web-root-dir
#同步到B服务器的文件存放的路径
path=/data/www/

#仅允许A机器推送
host allow = 192.168.1.2/24
host deny = *

启动 rsyncd 服务

1
rsync --daemon

将其加入 rc.local 实现自启

1
/usr/bin/rsync --daemon

下面进入主机A的配置过程

防火墙配置同B

安装 inotify-tools

1
apt-get install inotify-tools

建立 sersync2 配置文件

1
vim /etc/sersync2.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
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
<?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="true">
<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"/>
<modify start="false"/>
</inotify>

<sersync>

<!-- 下面是B机器的参数 -->
<localpath watch="/data/www">
<remote ip="192.168.1.3" name="www" />

<!--照葫芦画瓢可以依次添加其他-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="false" users="rsync" passwordfile="/etc/sersync2.pas"/>
<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>

</head>

启动服务

1
sersync2 -r -d -o /etc/sersync2.conf

加入 rc.local 实现自启

1
/usr/bin/sersync2 -r -d -o /etc/sersync2.conf