需求:有两台 nginx 服务器,一台 web 服务器。web服务器上传的图片文件需要同步到 nginx 服务器目录。
源服务器:web
目标服务器: nginx1 nginx2
步骤:
1.在 nginx1 上安装 rsync。准备安装包?rsync-3.0.9.tar.gz ? 解压到自己喜欢的目录。
如果没有 gcc 环境就先安装
yum -y install gcc
#?进入解压的目录,执行以下命令开始安装
[root@localhost rsync-3.0.9]# ./configure --prefix=/usr/local/rsync/
#出现以下信息 configure.sh: creating ./config.status config.status: creating Makefile config.status: creating lib/dummy config.status: creating zlib/dummy config.status: creating popt/dummy config.status: creating shconfig config.status: creating config.h
? ? rsync 3.0.9 configuration successful
# 继续安装
[root@localhost rsync-3.0.9]# make
[root@localhost rsync-3.0.9]# make install
#安装完成,可以检查安装是否成功
[root@localhost rsync-3.0.9]# rsync -h
#建立密码认证文件? ?/etc/rsyncd.pass
echo "123456" >/etc/rsyncd.pass
密码改成root的正确密码,也可以新创建用户和密码?,根据 rsyncd.conf 中配置的用户修改。
#授权
[root@localhost rsync]# chmod 600 /etc/rsyncd.pass
#创建 /etc/rsyncd.conf 配置文件
uid = root
gid = root
use chroot = yes
ready only = no
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
hosts allow = 10.99.20.0/24 10.99.83.0/24 10.99.19.0/24
exclude = lost+found/
transfer logging = yes
timeout = 900
[app-image]
path = /mnt/gxyb/upload/pro
comment = app-nginx-images rsync
read only = no
auth users = root
secrets file = /etc/rsyncd.pass
hosts allow 是允许连接的服务器
#启动 rsync?
[root@localhost rsync]# /usr/local/rsync/bin/rsync --daemon --config=/etc/rsyncd.conf
#检查启动状态
[root@localhost rsync]# ps aux | grep rsync
同样步骤在 nginx2 服务器上安装 rsync
2.在 web 服务器上安装?sersync2
准备好安装包?sersync2.5.4_64bit_binary_stable_final.tar.gz? ? 解压到自己喜欢的目录
#进入解压目录??cd GNU-Linux-x86/
修改配置文件?
vim confxml.xml
?修改
<localpath watch="/mnt/gxyb/upload/pro"> ? ? ? ? ? ? <remote ip="10.99.20.127" name="app-image"/> ? ? ? ? ? ? <remote ip="10.99.20.128" name="app-image"/> </localpath>
<auth start="true" users="root" passwordfile="/etc/rsync.pas"/>
根据实际使用的用户密码进行修改,可以新建用户不使用root用户。
其他配置使用默认的即可。
<?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="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/mnt/gxyb/upload/pro">
<remote ip="10.99.20.127" name="app-image"/>
<remote ip="10.99.20.128" name="app-image"/>
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="root" passwordfile="/etc/rsync.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>
<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>
#创建密码文件? /etc/rsync.pas
echo "123456" >/etc/rsync.pas
根据实际使用的用户密码修改
#授权密码文件
chmod 600 /etc/rsync.pas
#启动服务
./sersync2 -d –r
到这就结束了。这样只要修改 web 服务器 /mnt/gxyb/upload/pro?的文件,两台 nginx 服务器就会自动同步。
|