Rsync+Sersync同步特点:

(1):sersync可以记录下被监听目录中发生变化的(包括增加、删除、修改)具体某一个文件或某一个目录的名字;
(2):rsync在同步的时候,只同步发生变化的这个文件或者这个目录(每次发生变化的数据相对整个同步目录数据来说是很小的,rsync在遍历查找比对文件时,速度很快),因此效率很高。


rsync+sersync自动同步实验:

操作系统:CentOS 5.8

目标服务器:10.10.2.192 (rsync的server)

源服务器:10.10.2.191 (rsync的client)



在目标服务端(10.10.2.192)rsync的server端操作:

1.关闭SELINUX

vi /etc/selinux/config   #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出
setenforce #立即生效 

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

vi /etc/sysconfig/iptables   #编辑防火墙配置文件
-A RH-Firewall--INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
:wq! #保存退出
/etc/init.d/iptables restart #最后重启防火墙使配置生效

3.安装rsync服务端软件

在10.10.2.192上安装rsync软件:

yum install rsync xinetd   #安装rsync和xinetd软件
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
:wq! #保存退出
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理Rsync服务的)
LANG=en #如果出现中文乱码,更改支持语言 

4.创建rsyncd.conf

vi /etc/rsyncd.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启动时欢迎信息页面文件位置(文件内容自定义)
[centos] #自定义模块名称
path = /home/share #rsync服务端数据目录路径
comment = centos #模块名称与centos自定义名称相同
uid = root #设置rsync运行权限为root
gid = root #设置rsync运行权限为root
port = #默认端口
use chroot = no #默认为true,修改为no,增加对目录文件软连接的备份
read only = no #设置rsync服务端文件为读写权限
list = no #不显示rsync服务端资源列表
max connections = #最大连接数
timeout = #设置超时时间
auth users = root,admin,centos #执行数据同步的用户名,可以设置多个,用英文状态下逗号隔开
hosts allow = 10.10.2.192 #允许进行数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开
hosts deny = 10.10.2.59 #禁止数据同步的客户端IP地址,可以设置多个,用英文状态下逗号隔开

5.创建用户认证文件

vi /etc/rsync.pass
centos: #格式,用户名:密码,可以设置多个,每行一个用户名:密码
root:root123
admin:root123
:wq! 

6.设置文件权限:

chmod  /etc/rsyncd.conf
chmod /etc/rsync.pass 

7.启动rsync:

/etc/init.d/xinetd start #启动
service xinetd stop #停止
service xinetd restart #重新启动


在源服务端(10.10.2.191)上操作:

1.关闭SELINUX

vi /etc/selinux/config   #编辑防火墙配置文件
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq! #保存,退出
setenforce #立即生效

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

vi /etc/sysconfig/iptables   #编辑防火墙配置文件
-A RH-Firewall--INPUT -m state --state NEW -m tcp -p tcp --dport -j ACCEPT
:wq! #保存退出
/etc/init.d/iptables restart #最后重启防火墙使配置生效

3.安装rsync客户端软件:

yum install rsync xinetd #安装rsync和xinetd软件
vi /etc/xinetd.d/rsync #编辑配置文件,设置开机启动rsync
disable = no #修改为no
/etc/init.d/xinetd start #启动(CentOS中是以xinetd来管理rsync服务的)

4.创建认证密码文件

vi /etc/passwd.txt 

:wq!
chmod /etc/passwd.txt #设置文件权限 

5.测试源服务器10.10.2.191到目标服务器10.10.2.192之间数据同步:

mkdir -p /home/share/test #在源服务器10.10.2.191上创建测试文件夹,然后运行下面命令:
rsync -avH --port= --progress --delete /home/share/ centos@10.10.2.192::centos --password-file=/etc/passwd.txt
##同步本地目录至server
rsync -avH --port= --progress --delete centos@10.10.2.192::centos --password-file=/etc/passwd.txt /home/share/
##同步server端目录至本地

6.编辑脚本

vim rsync.sh

#!/bin/bash
rsync -avH --port= --progress --delete centos@10.10.2.192::centos --password-file=/etc/passwd.txt /home/share/
#description: update Server to Local. #rsync -avH --port= --progress --delete /home/share/ centos@10.10.2.192::centos --password-file=/etc/passwd.txt
#description: update Local to Server.


在rsync的client端10.10.2.191上安装sersync软件,实时出发rsync进行同步

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

ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify
-rw-r--r-- root root Mar : max_queued_events
-rw-r--r-- root root Mar : max_user_instances
-rw-r--r-- root root Mar : max_user_watches 备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核
CentOS .X 内核为2.6.18,默认已经支持inotify

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

修改参数:
sysctl -w fs.inotify.max_queued_events=""
sysctl -w fs.inotify.max_user_watches=""
sysctl -w fs.inotify.max_user_instances="" vi /etc/sysctl.conf #添加以下代码
fs.inotify.max_queued_events=
fs.inotify.max_user_watches=
fs.inotify.max_user_instances=
: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

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

cd /usr/local/src
tar zxvf sersync2..4_64bit_binary_stable_final.tar.gz #解压
mv GNU-Linux-x86 /usr/local/sersync #移动目录到/usr/local/sersync

4.配置sersync

cd  /usr/local/sersync #进入sersync安装目录
cp confxml.xml confxml.xml-bak #备份原文件 vim confxml.xml <?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
<host hostip="localhost" port=""></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="/home/share"> #源服务器同步目录
<remote ip="10.10.2.192" name="centos"/> ip #目标服务器IP地址 name #目标服务器rsync同步目录模块名称
<!--<remote ip="192.168.8.39" name="tongbu"/>-->
<!--<remote ip="192.168.8.40" name="tongbu"/>-->
</localpath>
<rsync>
<commonParams params="-avH"/> #rsync后接的参数
<auth start="true" users="centos" passwordfile="/etc/passwd.txt"/> #目标服务器rsync同步用户名 #目标服务器rsync同步用户的密码在源服务器的存放路径
<userDefinedPort start="false" port=""/><!-- port= -->
<timeout start="false" time=""/><!-- timeout= -->
<ssh start="false"/>
</rsync>
<failLog path="/tmp/rsync_fail_log.sh" timeToExecute=""/><!--default every 60mins execute once--> #脚本运行失败日志记录
<crontab start="true" schedule=""><!--600mins--> #设置为true,每隔600分钟执行一次全盘同步
<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=""/>
</localpath>
</plugin>
<plugin name="refreshCDN">
<localpath watch="/data0/htdocs/cms.xoyo.com/site/">
<cdninfo domainname="ccms.chinacache.com" port="" 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>

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

vim /etc/rc.d/rc.local #编辑,在最后添加一行
/usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml #设置开机自动运行脚本
:wq! #保存退出

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

vim  /check_sersync.sh  #编辑,添加以下代码

#!/bin/bash

sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq ];
then
$sersync -d -r -o $confxml &
else
exit ;
fi
:wq! #保存退出 chmod +x check_sersync.sh
vim /etc/crontab
*/ * * * * root /root/check_sersync.sh > /dev/null >& #每隔5分钟执行一次脚本
:wq!
service crond reload #重新加载服务

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

在源服务器10.10.2.191的/home/share目录下新建文件夹test
mkdir /home/share/test

然后重启10.10.2.191服务器
等系统启动之后,查看10.10.2.192的/home/share下是否有test文件夹

Rsync+Sersync同步的更多相关文章

  1. 文件触发式实时同步 Rsync+Sersync Rsync+Inotify-tools

    一.概述 1.Rsync+Sersync 是什么? 1)Sersync使用c++编写基于inotify开发的触发机制: 2)Sersync可以监控所监听的目录发生的变化(包括新建.修改.删除),具体到 ...

  2. Rsync+Inotify同步

    rsync服务安装与<rsync+sersync同步>环境一样! 安装inotify-tools 在源服务器10.10.2.191上操作: 1.查看服务器内核是否支持inotify ll ...

  3. Linux下Rsync+sersync实现数据实时同步

    inotify 的同步备份机制有着缺点,于是看了sersync同步,弥补了rsync的缺点.以下转自:http://www.osyunwei.com/archives/7447.html 前言: 一. ...

  4. rsync+sersync实现文件实时同步

    前言: 一.为什么要用Rsync+sersync架构? 1.sersync是基于Inotify开发的,类似于Inotify-tools的工具 2.sersync可以记录下被监听目录中发生变化的(包括增 ...

  5. Rsync+sersync文件实时同步

    一.为什么要用Rsync+sersync架构1.sersync是基于Inotify开发的,类似于Inotify-tools的工具2.sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改 ...

  6. rsync+sersync实现数据文件实时同步

    一.简介 sersync是基于Inotify开发的,类似于Inotify-tools的工具: sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改)具体某一个文件或某一个目录的名字: ...

  7. 如何通过rsync+sersync 实现同步备份

    3.rsync+sersync更快更节约资源实现web数据同步4.unison+inotify实现web数据双向同步 一:为什么要实现同步备份 服务器上有些重要文件或数据时,可以把他们多备份一份到其他 ...

  8. Rsync + sersync 实时同步备份

    一      Rsync + Sersync  实时同步介绍 1.Rsync 服务搭建介绍 云机上搭建Rsync server,在本地搭建Rsync Clinet. 2. Sersync 服务搭建介绍 ...

  9. rsync+sersync实现代码同步

    APP02安装 rsync服务端 yum install rsync vim /etc/rsyncd.conf pid file=/var/rsynclog/rsyncd.pid log file=/ ...

随机推荐

  1. 邁向IT專家成功之路的三十則鐵律 鐵律二十九 IT人富足之道-信仰

    天地自然的循環法則,讓每一個人都必須經歷生.老.病.死.喜.怒.哀.樂,然而至始至終無盡的煩惱總是遠多於快樂,因此筆者深信每一個人在一生當中,都必須要有適合自己的正確信仰,他可以在你無法以物質力量來解 ...

  2. 前端模板inspinia

    前端模板,可以下个免费的,可以花点小钱买.或者github搜索一个 https://chuibility.github.io/inspinia/ http://cn.inspinia.cn/layou ...

  3. [zlib]_[0基础]_[使用Zlib完整解压zip内容]

    场景: 1. 解压文件一般用在下载了一个zip文件之后解压,或者分析某个文件须要解压的操作上. 2. 解压文件,特别是解压带目录的zip文件往往系统没有提供这类Win32 API,当然C#自带库能解压 ...

  4. 开源软件许可认证:open softwae license

    OSIA认证的开放源代码软件的软件许可证有如下21种: 1.The GNU General Public License (GPL) 2.The GNU Library or "Lesser ...

  5. 【Spark】RDD操作具体解释4——Action算子

    本质上在Actions算子中通过SparkContext运行提交作业的runJob操作,触发了RDD DAG的运行. 依据Action算子的输出空间将Action算子进行分类:无输出. HDFS. S ...

  6. vue2.0 自定义 提示框(Toast)组件

    1.自定义 提示框 组件 src / components / Toast / index.js /** * 自定义 提示框( Toast )组件 */ var Toast = {}; var sho ...

  7. 修改 百度地图 infowindow 默认样式

    1.百度 api 没有 提供可以修改 infowindow 默认样式的 方法. 如需修改,需要 自定义 替换 默认样式. demo.html <!DOCTYPE html> <htm ...

  8. Odoo 运费

    模块delievery可以将运费Charge给客户     安装delivery模块                 Delivery method     在做订单的时候,选择相应的运输方法, 系统 ...

  9. dubbo学习之Hello world

    现在企业中使用dubbo的越来越多,今天就简单的学习一下dubbo,写了一个hello world,教程仅供入门,如要深入学习请上官网 服务提供方: 首先将提供方和消费方都引入jar包,如果使用的是m ...

  10. Android学习系列(二)布局管理器之线性布局的3种实现方式

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39643669 LinearLayout是Android控件中的线性布局控件,它包括的子控件 ...