您的位置:首页 > 运维架构 > Linux

Linux下Rsync+Sersync搭建与配置,实现日志实时同步

2017-05-02 15:34 561 查看
Linux下Rsync+Sersync搭建与配置,实现日志实时同步

一、简介

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

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

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

Rsync+Inotify-tools

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

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

Rsync+sersync

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

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

小结:当同步的目录数据量不大时,建议使用Rsync+Inotify-tools;当数据量很大(几百G甚至1T以上)、文件很多时,建议使用Rsync+sersync。

三、搭建环境说明

操作系统:CentOS 6.5

源服务器:192.168.3.10 (后面称A); 目录:/usr/local/rsyData/webLog、/usr/local/rsyData/sysLog

目标服务器:192.168.3.11(后面称B1),  192.168.3.12(后面称B2);  目录:/usr/local/rsyLog/web、/usr/local/rsyLog/sys

四、安装与配置

第一部分:分别在目标服务器B1、B2上的操作

1. 关闭 SELINUX

[root@localhost ~]# vi /etc/selinux/config #编辑防火墙配置文件
#进行一下操作:
#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存,退出

[root@localhost ~]# setenforce 0 #立即生效

2. 开启防火墙tcp 873端口(rsync默认端口)

[root@localhost ~]# vi /etc/sysconfig/iptables #编辑防火墙配置文件

#增加下面的内容:

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

:wq! #保存退出

[root@localhost ~]# /etc/init.d/iptables restart #最后重启防火墙使配置生效

3. 创建rsync用户、用户组

[root@localhost ~]# groupadd rsy #创建组

[root@localhost ~]# useradd -g rsy myRsy #创建用户

[root@localhost ~]# passwd myRsy #设置用户密码

4. 安装Rsync服务端软件

[root@localhost ~]# yum install rsync xinetd #安装

[root@localhost ~]# vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
#进行一下操作:
disable = no #修改为no

:wq! #保存退出

[root@localhost ~]# /etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)

5. 创建rsyncd.conf配置文件

[root@localhost ~]# vi /etc/rsyncd.conf #创建配置文件

#添加以下代码:
#日志文件位置,启动rsync后自动产生这个文件,无需提前创建

log file = /var/log/rsyncd.log

#pid文件的存放位置

pidfile = /var/run/rsyncd.pid

#支持max connections参数的锁文件

lock file = /var/run/rsync.lock

#用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件

secrets file = /etc/rsync.pass

#rsync启动时欢迎信息页面文件位置(文件内容自定义)

motd file = /etc/rsyncd.motd

#设置rsync运行权限为root

uid = root

#设置rsync运行权限为root

gid = root

#默认端口

port=873

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

use chroot = no

#设置rsync服务端文件为读写权限

read only = no

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

list = no
#ignore errors = yes

#最大连接数

max connections = 200

#设置超时时间

timeout = 600

#执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开

auth users = myRsy

#允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

hosts allow = 192.168.3.10

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

#自定义名称

[webLog]

#rsync服务端数据目录路径

path = /usr/local/rsyLog/web/

#模块名称与[webLog]自定义名称相同

comment = webLog

#自定义名称

[sysLog]

#rsync服务端数据目录路径

path = /usr/local/rsyLog/sys/

#模块名称与[sysLog]自定义名称相同

comment = sysLog

:wq!  #保存,退出

6. 创建用户认证文件

[root@localhost ~]# vi /etc/rsync.pass #配置文件

#添加以下内容:

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

myRsy:你设置的myRsy用户密码

:wq!  #保存退出

7. 设置文件权限

[root@localhost ~]# chmod 600 /etc/rsyncd.conf  #设置文件所有者读取、写入权限

[root@localhost ~]# chmod 600 /etc/rsync.pass  #设置文件所有者读取、写入权限

8. 启动rsync

[root@localhost ~]# rsync --daemon #独立启动

[root@localhost ~]# /etc/init.d/xinetd start  #启动

[root@localhost ~]# service xinetd stop   #停止

[root@localhost ~]# service xinetd restart  #重新启动

第二部分:在源服务器A上的操作

1. 关闭 SELINUX

[root@localhost ~]# vi /etc/selinux/config #编辑防火墙配置文件
#进行一下操作:
#SELINUX=enforcing #注释掉

#SELINUXTYPE=targeted #注释掉

SELINUX=disabled #增加

:wq! #保存,退出

[root@localhost ~]# setenforce 0 #立即生效

2. 开启防火墙tcp 873端口(rsync默认端口)

[root@localhost ~]# vi /etc/sysconfig/iptables #编辑防火墙配置文件

#增加下面的内容:

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

:wq! #保存退出

[root@localhost ~]# /etc/init.d/iptables restart #最后重启防火墙使配置生效

3. 安装Rsync客户端端软件

[root@localhost ~]# whereis rsync   #查看系统是否已安装rsync,出现下面的提示,说明已经安装

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

[root@localhost ~]# yum install  xinetd  #只安装xinetd即可,CentOS中是以xinetd来管理rsync服务的

[root@localhost ~]# yum install rsync xinetd #如果默认没有rsync,运行此命令进行安装rsync和xinetd

[root@localhost ~]# vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync

[root@localhost ~]# disable = no #修改为no

[root@localhost ~]# /etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理rsync服务的)

4. 创建认证密码文件

vi /etc/rsync.pass #配置文件

#添加以下内容:

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

myRsy:你设置的myRsy用户密码

:wq!  #保存退出

[root@localhost ~]# chmod 600 /etc/rsync.pass  #设置文件权限,只设置文件所有者具有读取、写入权限即可

第三部分:安装sersync工具,实时触发rsync进行同步(在源服务器A上)

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

[root@localhost ~]# 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查看内核

2. 修改inotify默认参数(inotify默认内核参数值太小)

#查看系统默认参数值:

[root@localhost ~]# sysctl -a | grep max_queued_events

结果是:fs.inotify.max_queued_events = 16384

[root@localhost ~]# sysctl -a | grep max_user_watches

结果是:fs.inotify.max_user_watches = 8192

[root@localhost ~]# sysctl -a | grep max_user_instances

结果是:fs.inotify.max_user_instances = 128

修改参数:

[root@localhost ~]# sysctl -w fs.inotify.max_queued_events="99999999"

[root@localhost ~]# sysctl -w fs.inotify.max_user_watches="99999999"

[root@localhost ~]# sysctl -w fs.inotify.max_user_instances="65535"

[root@localhost ~]# vi /etc/sysctl.conf 

#添加以下代码

fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535

:wq! #保存退出

#参数说明:

max_queued_events:inotify队列最大长度,如果值太小,会出现"** Event Queue Overflow **"错误,导致监控文件不准确

max_user_watches:要同步的文件包含多少目录,可以用:find /home/www.osyunwei.com -type d | wc -l 统计,必须保证max_user_watches值大于统计结果(这里/home/www.osyunwei.com为同步文件目录)

max_user_instances:每个用户创建inotify实例最大值

3. 安装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目录下

[root@localhost ~]# cd /usr/local/src

[root@localhost ~]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz  #解压

[root@localhost ~]# mv GNU-Linux-x86  /usr/local/sersync  #移动目录到/usr/local/sersync

4. 配置sersync

[root@localhost ~]# cd  /usr/local/sersync #进入sersync安装目录

[root@localhost ~]# cp confxml.xml confxml.xml-bak  #备份原文件

[root@localhost ~]# vi 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="false"/>
<modify start="false"/>
</inotify>
<sersync>
<localpath watch="/usr/local/rsyData/webLog"><!--本地同步目录-->
<remote ip="192.168.3.11" name="webLog"/><!--远程同步IP-->
<remote ip="192.168.3.12" name="webLog"/><!--远程同步IP-->
</localpath>
<localpath watch="/usr/local/rsyData/sysLog"><!--本地同步目录-->
<remote ip="192.168.3.11" name="sysLog"/><!--远程同步IP-->
<remote ip="192.168.3.12" name="sysLog"/><!--远程同步IP-->
</localpath>
<rsync>
<commonParams params="-artuz"/>
<auth start="true" users="myRsy" passwordfile="/etc/rsync.pswd"/><!--远程账号,密码-->
<userDefinedPort start="false" port="874"/><!-- port=874 -->
<timeout start="true" time="300"/><!-- timeout=100 -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
<crontab start="true" 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>


:wq!  #保存退出

参数说明:

localpath watch="/usr/local/rsyData/webLog",localpath
watch="/usr/local/rsyData/sysLog":#源服务器同步目录

192.168.3.11,192.168.3.12:#目标服务器IP地址

name="webLog",name="sysLog":
#目标服务器rsync同步目录模块名称

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

passwordfile="/etc/rsync.pswd": #目标服务器rsync同步用户的密码在源服务器的存放路径
remote ip="192.168.3.11":
 #目标服务器ip,每行一个
remote ip="192.168.3.12":
 #目标服务器ip,每行一个
failLog path="/tmp/rsync_fail_log.sh"
 #脚本运行失败日志记录
start="true"
 #设置为true,每隔600分钟执行一次全盘同步

5. 设置sersync监控开机自动执行

[root@localhost ~]# vi /etc/rc.d/rc.local

#编辑,在最后添加一行

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

:wq!  #保存退出

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


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rsync sersync 日志同步