6

文件服务器集群 采用Rsync+sersync同步主从文件FTP

 3 years ago
source link: https://blog.p2hp.com/archives/2965
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

1.1     为什么要用Rsync+sersync架构?

1、sersync是基于Inotify开发的,类似于Inotify-tools的工具

2、sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字,然后使用rsync同步的时候,只同步发生变化的这个文件或者这个目录。

1.2     Rsync+Inotify-tools与Rsync+sersync这两种架构有什么区别?

1Rsync+Inotify-tools

   (1)Inotify-tools只能记录下被监听的目录发生了变化(包括增加、删除、修改),并没有把具体是哪个文件或者哪个目录发生了变化记录下来;

   (2)rsync在同步的时候,并不知道具体是哪个文件或者哪个目录发生了变化,每次都是对整个目录进行同步,当数据量很大时,整个目录同步非常耗时(rsync要对整个目录遍历查找对比文件),因此,效率很低。

2Rsync+sersync

   (1)sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;

   (2)rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此,效率很高。

1.3     配置源服务器和目标服务器

源服务器:192.168.3.7  目标服务器:192.168.3.4

1.3.1    配置防火墙

1、关闭SELINUX

vi /etc/selinux/config                             #编辑防火墙配置文件

#SELINUX=enforcing                             #注释掉

#SELINUXTYPE=targeted                          #注释掉

SELINUX=disabled                               #增加

:wq!                                          #保存,退出

setenforce 0                                   #立即生效

2、开启防火墙tcp 873端口(Rsync默认端口)

vi /etc/sysconfig/iptables                         #编辑防火墙配置文件

-A INPUT -p tcp -m tcp --dport 873 -j ACCEPT

:wq!                                          #保存退出

/etc/init.d/iptables restart                        #最后重启防火墙使配置生效

1.3.2    安装rsync

whereis rsync               #查看系统是否已安装rsync,出现下面的提示,说明已经安装

rsync: /usr/bin/rsync /usr/share/man/man1/rsync.1.gz

yum install  xinetd          #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的

yum install rsync xinetd       #如果默认没有rsync,运行此命令进行安装rsync和xinetd

vi /etc/xinetd.d/rsync         #编辑配置文件,设置开机启动rsync

disable = no                #修改为no

/etc/init.d/xinetd start        #启动(CentOS中是以xinetd来管理rsync服务的)

注:此应用在源服务器和目标服务器均安装,配置有不同,具体如下。

/usr/bin/rsync --daemon --config=/etc/rsync.conf

1.4     配置目标服务器

1.4.1    创建rsync.conf配置文件

vi /etc/rsync.conf               #创建配置文件,添加以下代码

log file = /var/log/rsyncd.log       #日志文件位置,启动rsync后自动产生这个文件

pidfile = /var/run/rsyncd.pid       #pid文件的存放位置

lock file = /var/run/rsync.lock      #支持max connections参数的锁文件

secrets file = /etc/rsync.pass       #用户认证配置文件,保存用户名称和密码,等会创建

motd file = /etc/rsyncd.Motd      #rsync启动时欢迎信息页面文件位置(文件内容自定义)

[highthing.com]                 #自定义名称

path = /home/fileweb/webapps/ROOT/upload/    #rsync服务端数据目录路径

comment = highthing.com        #模块名称与[highthing.com]自定义名称相同

uid = ZKHJ1                    #设置rsync运行权限为root

gid = ftp                       #设置rsync运行权限为root

port=873                      #默认端口

use chroot = no                 #默认为true,修改为no,增加对目录文件软连接的备份

read only = no                  #设置rsync服务端文件为读写权限

list = no                       #不显示rsync服务端资源列表

max connections = 200           #最大连接数

timeout = 600                  #设置超时时间

auth users = ZKHJ1              #执行数据同步的用户名,可以设置多个

hosts allow = 192.168.21.129      #允许进行数据同步的客户端IP地址

hosts deny = 192.168.21.254      #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

:wq!  #保存,退出

1.4.2    创建用户认证文件

vi /etc/rsync.pass                 #配置文件,添加以下内容

ZKHJ1:zk_1234                  #格式,用户名:密码,可以设置多个,每行一个用户名:密码

:wq!  #保存退出

1.4.3    设置文件权限

chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限

chmod 600 /etc/rsync.pass   #设置文件所有者读取、写入权限

chown ZKHJ1 /home/ftpserver/html/upload/ #设置目录所属用户

chgrp ftpgroup /home/ftpserver/html/upload/ #设置目录用户组

1.4.4    启动rsync

/etc/init.d/xinetd start        #启动

service xinetd stop          #停止

service xinetd restart        #重新启动

/usr/bin/rsync --daemon --config=/etc/rsync.conf   #启动男公关

1.5     配置源服务器

1.5.1    创建认证密码文件

vi /etc/passwd.txt            #编辑文件,添加以下内容

zk_1234                  #密码

:wq!                      #保存退出

chmod 600 /etc/passwd.txt  #设置文件权限,只设置文件所有者具有读取、写入权限即可

1.5.2    测试源服务器和目标服务器同步

在源服务器(ip:)上创建测试文件,然后在源服务器运行下面命令

rsync -artuz --port=873 --progress --delete /home/fileweb/webapps/ROOT/upload/ [email protected]::highthing.com --password-file=/etc/passwd.txt

然后去目标服务器检查/home/fileweb/webapps/ROOT/upload/下是否存在该文件

1.5.3    安装sersync工具

  1. 1.    查看服务器内核是否支持inotify

ll /proc/sys/fs/inotify       #列出文件目录,出现下面的内容,说明服务器内核支持inotify

-rw-r--r-- 1 root root 0 Mar    7 02:17 max_queued_events

-rw-r--r-- 1 root root 0 Mar    7 02:17 max_user_instances

-rw-r--r-- 1 root root 0 Mar    7 02:17 max_user_watches

备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核

  1. 2.    安装sersync

sersync下载地址:https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

上传sersync2.5.4_64bit_binary_stable_final.tar.gz到/usr/local/src目录下

cd /usr/local/src

tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压

mv GNU-Linux-x86  /usr/local/sersync               #移动目录到/usr/local/sersync

1.5.4    配置sersync实时触发rsync进行同步

cd  /usr/local/sersync                              #进入sersync安装目录

cp confxml.xml confxml.xml-bak                     #备份原文件

vi /usr/local/sersync/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="false"/>

        <closeWrite start="true"/>

        <moveFrom start="true"/>

        <moveTo start="true"/>

        <attrib start="true"/>

        <modify start="true"/>

    </inotify>

    <sersync>

        <localpath watch="/home/fileweb/webapps/ROOT/upload">

            <remote ip="172.169.5.249" name="highthing.com"/>

        </localpath>

        <rsync>

            <commonParams params="-artuz"/>

            <userDefinedPort start="true" port="873"/><!-- port=874 -->

            <timeout start="true" time="20"/><!-- timeout=100 -->

            <auth start="true" users="ZKHJ1" passwordfile="/etc/rsync.passwd"/>

            <ssh start="false"/>

        </rsync>

        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->

        <crontab start="false" schedule="0.1"><!--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/fileweb/webapps/ROOT/upload">

            <deshost ip="172.169.5.249" 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>

:wq!  #保存退出

1.5.5    关于sersync重要参数说明

localpath watch="/home/fileweb/webapps/ROOT/upload": #源服务器同步目录

name="highthing.com":             #目标服务器rsync同步目录模块名称

users="ZKHJ1":                   #目标服务器rsync同步用户名

passwordfile="/etc/rsync.passwd":   #目标服务器rsync同步用户的密码在源服务器的存放路径

remote ip="172.169.5.249":          #目标服务器ip,每行一个

failLog path="/tmp/rsync_fail_log.sh"  #脚本运行失败日志记录

start="true"                         #设置为true,每隔600分钟执行一次全盘同步

1.5.6    设置sersync监控开机自动执行

vi /etc/rc.d/rc.local                                             #编辑,在最后添加一行

/usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml  #设置开机自动运行脚本

:wq!                                                          #保存退出

1.5.7    添加脚本监控sersync是否正常运行

vi  /home/crontab/check_sersync.sh  #编辑,添加以下代码

#!/bin/sh

sersync="/usr/local/sersync/sersync2"

confxml="/usr/local/sersync/confxml.xml"

status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)

if [ $status -eq 0 ];

$sersync -d -r -o $confxml &

exit 0;

:wq!  #保存退出

chmod +x /home/crontab/check_sersync.sh #添加脚本执行权限

vi /etc/crontab #编辑,在最后添加下面一行

*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1  #每隔5分钟执行一次脚本

service crond reload  #重新加载服务

1.5.8    测试sersync实时触发rsync同步脚本是否正常运行

在源服务器192.168.21.129上创建文件夹

mkdir /home/fileweb/webapps/ROOT/upload/ceshi

执行/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml启动sersync

继续查看目标服务器192.168.21.127的/home/fileweb/webapps/ROOT/upload/下是否有ceshi文件夹

如果以上测试都通过,说明inotify实时触发rsync同步脚本运行正常。

至此,Linux下Rsync+sersync实现数据实时同步完成。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK