分享

CentOS 6.5 Rsync sersync 实现数据实时同步备份

 zjycy 2017-02-04
  1. 为什么要用rsync+sersync架构

  2. Rsync+sersync

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

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

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

说明

  操作系统:CentOS 6.5

  源服务器:121.41.28.253

  目标服务器:121.41.48.242,121.41.48.233

  目的:把源服务器上/var/www/synctest目录实时同步到目标服务器的/var/www/synctest下

   具体操作

                         第一部分:在两台目标服务器121.41.48.242和121.41.48.233执行的操作

一、在目标服务器安装rsync服务端

1、关闭SELINUX
编辑防火墙配置文件:

[root@iZ23tvhxkkjZ /]# vi /etc/selinux/config

用 # 注释掉下面两行代码:  

#SELINUX=enforcing
#SELINUXTYPE=targeted

增加下面一行代码:

SELINUX=disabled

保存退出   

wq!

运行命令行,立即生效:

[root@iZ23tvhxkkjZ /]# setenforce 0

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

   编辑防火墙配置文件:

[root@iZ23tvhxkkjZ /]# vi /etc/sysconfig/iptables

  添加下列行到开启22端口的后面:

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

   保存,退出:             

wq!

重启防火墙使配置生效:

[root@iZ23tvhxkkjZ /]# /etc/init.d/iptables restart

3、安装rsync服务端软件

安装rsync,CentOS6.5自带rsync,运行下列命令行,确保rsync安装,同时安装rsync工具xinetd

[root@iZ23tvhxkkjZ /]# yum install rsync xinetd

编辑配置文件,设置开机启动 rsync

[root@iZ23tvhxkkjZ /]# vi /etc/xinetd.d/rsync

  修改disable的值为no:

disable = no

   保存,退出:             

wq!

启动xinetd(CentOS中是以xinetd来管理Rsync服务的)

[root@iZ23tvhxkkjZ /]# /etc/init.d/xinetd start

4、创建rsyncd.conf配置文件

创建配置文件:

[root@iZ23tvhxkkjZ /]# vi /etc/rsyncd.conf

添加以下代码

log file = /var/log/rsyncd.log
pidfile = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
secrets file = /etc/rsyncd.passwd
motd file = /etc/rsyncd.motd

[synctest]
path = /var/www/synctest/
comment = synctest
uid = root
gid = root
port=873
use chroot = no
read only = no
list = no
max connections = 200
timeout = 600
auth users = root
hosts allow = 121.41.28.253
hosts deny = 121.41.28.254

5.创建用户认证文件

配置文件

[root@iZ23tvhxkkjZ /]# vi /etc/rsyncd.passwd

添加以下内容(原服务器的用户名和密码)

root:123456

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

保存退出

:wq!

6.设置文件权限

  设置文件所有者读取、写入权限

[root@iZ23tvhxkkjZ etc]# chmod 600 /etc/rsyncd.conf
[root@iZ23tvhxkkjZ etc]# chmod 600 /etc/rsyncd.passwd

7.启动rsync

启动

[root@iZ23tvhxkkjZ etc]# /etc/init.d/xinetd start

参考指令

停止:service xinetd stop
启动:service xinetd restart

[目标服务安装完成 另外一台目标服务器也同样的方法]

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

以下部分在源服务器(发布服务器)上进行操作

一、安装rsync客户端

1、关闭selinux

编辑防火墙配置文件

[root@iZ23i2txh8jZ /]# vi /etc/selinux/config

用#注释掉下面两行代码:

#SELINUX=enforcing
#SELINUXTYPE=targeted

增加下面一行代码

SELINUX=disabled

保存 退出

:wq!

运行命令,立即生效

[root@iZ23i2txh8jZ /]# setenforce 0

2、开启防火墙tcp873端口

  配置防火墙

[root@iZ23i2txh8jZ /]# vi /etc/sysconfig/iptables

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

保存,退出 重启防火墙

:wq!
[root@iZ23i2txh8jZ /]# /etc/init.d/iptables restart

3.安装rsync centos6.5自带rsync 运行下列命令后 确保rsync安装 同时安装rsync工具xinetd

[root@iZ23i2txh8jZ /]# yum install rsync xinetd

编辑配置文件,设置开机启动rsync

[root@iZ23i2txh8jZ /]# vi /etc/xinetd.d/rsync
disable = no
wq!

启动xinetd(centos中是以xinet来管理rsync服务的)

[root@iZ23i2txh8jZ /]# /etc/init.d/xinetd start

4.创建认证密码文件

   编辑文件

[root@iZ23i2txh8jZ /]# vi /etc/rsyncd.passwd

添加以下内容作为密码

123456
保存退出
:wq!

设置文件权限,只设置文件所有者具有读取、写入权限

[root@iZ23i2txh8jZ /]# chmod 600 /etc/rsyncd.passwd

5.测试源服务器到两台服务器之间的数据同步

  在源服务器上创建测试文件夹

[root@iZ23i2txh8jZ www]# mkdir /var/www/synctest
[root@iZ23i2txh8jZ www]# mkdir /var/www/synctest/test

在源服务器上运行下面命令

rsync -avH --port=873 --progress --delete /var/www/synctest/ root@121.41.48.242::synctest --password-file=/etc/rsyncd.passwd

运行完成后,分别在两台目标服务器上查看,在/var/www/synctest目录下有test文件夹,说明数据同步成功。

『以上手动同步功能实现完成。下面内容为使用sersync工具监控目录,完成自动同步』

   二、安装sersync工具、实时出发rsync进行同步

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

列出文件目录

[root@iZ23i2txh8jZ /]# ll /proc/sys/fs/inotify

出现下面的内容、说明服务器内核支持inotify

-rw-r--r-- 1 root root 0 Dec 25 12:03 max_queued_events
-rw-r--r-- 1 root root 0 Dec 25 15:05 max_user_instances
-rw-r--r-- 1 root root 0 Dec 25 12:03 max_user_watches

备注:centos6.5默认支持inotify

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

[root@iZ23i2txh8jZ /]# sysctl -a | grep max_queued_events
fs.inotify.max_queued_events = 327679
[root@iZ23i2txh8jZ /]#
[root@iZ23i2txh8jZ /]# sysctl -a | grep max_user_watches
fs.inotify.max_user_watches = 50000000
fs.epoll.max_user_watches = 1646100
[root@iZ23i2txh8jZ /]#
[root@iZ23i2txh8jZ /]# sysctl -a | grep max_user_instances
fs.inotify.max_user_instances = 128
[root@iZ23i2txh8jZ /]#

修改参数

[root@iZ23i2txh8jZ /]# sysctl -w fs.inotify.max_queued_events="99999999"
[root@iZ23i2txh8jZ /]# sysctl -w fs.inotify.max_user_watches="99999999"
[root@iZ23i2txh8jZ /]# sysctl -w fs.inotify.max_user_instances="65535"

参数说明:

max_queued_events:

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

max_user_watches:

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

max_user_instances:

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

3、安装sersync

sersync下载地址(不好用):https://sersync./files/sersync2.5.4_64bit_binary_stable_final.tar.gz

将sersync2.5.4_64bit_binary_stable_final.tar.gz复制到/usr/local/src目录下。

[root@iZ23i2txh8jZ /]# cd /usr/local/src
[root@iZ23i2txh8jZ src]# tar zxvf sersync2.5.4_64bit_binary_stable_final.tar.gz
[root@iZ23i2txh8jZ src]# mv GNU-Linux-x86 /usr/local/sersync
[root@iZ23i2txh8jZ src]# cd /usr/local/sersync

4、配置sersync

[root@iZ23i2txh8jZ sersync]cp confxml.xml confxml.xml-bak
[root@iZ23i2txh8jZ sersync]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="/var/www/synctest">
			<remote ip="121.41.48.242" name="synctest"/>
			<remote ip="121.41.48.233" name="synctest"/>
			<!--<remote ip="192.168.8.40" name="tongbu"/>-->
		</localpath>
		<rsync>
			<commonParams params="-artuz"/>
			<auth start="true" users="root" 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="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=”/var/www/synctest”:#源服务器同步目录

121.41.48.242,121.41.48.233:#目标服务器IP地址

name=”synctest”: #目标服务器rsync同步目录模块名称

users=”root”: #目标服务器rsync同步用户名

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

remote ip=”121.41.48.242”: #目标服务器ip,每行一个

remote ip=”121.41.48.233”: #目标服务器ip,每行一个

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

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

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

编辑

[root@iZ23i2txh8jZ sersync]# vi /etc/rc.d/rc.local
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
:wq!

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

[root@iZ23i2txh8jZ crontab]# 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 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi

:wq!

添加脚本执行权限

[root@iZ23i2txh8jZ crontab]# chmod +x /home/crontab/check_sersync.sh

编辑

[root@iZ23i2txh8jZ crontab]# vi /etc/crontab
#在最后添加下面一行,每隔5分钟执行一次脚本
*/5 * * * * root /home/crontab/check_sersync.sh > /dev/null 2>&1

重新加载服务

[root@iZ23i2txh8jZ crontab]# service crond reload

7、测试

在源服务器上创建文件

mkdir /var/www/synctest/inotify_rsync_ceshi
mkdir /var/www/synctest/inotify_rsync_ceshi_new

继续查看两台目标服务器121.41.48.242,121.41.48.233的/var/www/synctest下是否有inotify_rsync_ceshi_new文件夹

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

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

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多