主服务器: 192.168.1.8
备份服务器: 192.168.1.9
要求:主服务器上文件变动时能实时同步到备份服务器上。
方法:
在备份服务器上开启sync服务端,监听主服务器发送过来的文件传输请求,存储到相应位置。
主服务器开启sersync2服务端,监听某个目录文件变动并通知备份服务器的sync服务端。
操作步骤:
1、备份服务器开启sync服务:首先配置:vim /etc/rsyncd.conf
rsync服务端配置
uid = root
gid = root
use chroot = no
max connections = 200
timeout = 1000
transfer logging = yes
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
log file = /var/log/rsyncd.log
log format = %t %a %m %f %b
port = 873
ignore errors
[dnoa]
path = /web/www
ignore errors = yes
read only = no
write only = yes
hosts allow = 192.168.1.8
hosts deny = *
list = yes
auth users = dnoa
secrets file = /etc/rsyncd.passwd
启动服务: /usr/bin/rsync --daemon
服务建议写入自动启动脚本:/etc/rc.local
2、主服务器上配置sersync2(目录自己创建):vim /web/etc/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="true"/>
<closeWrite start="true"/>
<moveFrom start="true"/>
<moveTo start="true"/>
<attrib start="true"/>
<modify start="true"/>
</inotify>
<sersync>
<localpath watch="/web/www">
<remote ip="192.168.1.9" name="dnoa"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="dnoa" passwordfile="/etc/rsyncd.passwd"/>
<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>
启动服务: sersync2 -d -r -o /web/etc/confxml.xml
为了调试方便,可以将改为,再前端启动(去掉-d选项): sersync2 -r -o /web/etc/confxml.xml。
在主服务器创建文件 touch /web/www/test.file
看备份服务器是否也有了这个文件:ll /web/www/test.file
从主服务器上删除这个文件 rm -f /web/www/test.file,看备份服务器是否也一并删除。
如果成功了,基本上文件增加、修改和删除就都可以同步到备份服务器了。
因为sersync2不好下载,所以我把它放到附件里了,可以下载使用。