rsync+sersync安装配置笔记

1.原理介绍
使用rsync+sersync做实时同步时,用于推送文件的服务器运行sersync服务,
用于接收文件的服务器则运行rsync守护进程,简单来说就是sersync会利用rsync命令将文件推送到rsync服务器,
实际线上使用一般会把sersync作为主服务器,rsync作为镜像服务器,实现数据同步备份,web镜像等功能。


2.环境介绍
sersync 推送端A  192.168.20.200  /home/nginx/html
rsync   接收端B  192.168.20.201  /home/nginx/html


3.首先关闭selinux与iptables(A、B)
# vi /etc/sysconfig/selinux
---------
SELINUX=disabled
---------
# setenforce 0
# service iptables stop
在使用sersync之前,我们必须安装配置好rsync服务器。


4.rsync (接收端B)
4.1安装rsync(centos6.3默认已安装)
# yum install rsync -y
# yum install xinetd -y
4.2启动rsync依赖服务
# /etc/init.d/xinetd start
# chkconfig xinetd on
4.3配置:
# vi /etc/rsyncd.conf
--------------------
uid = root
gid = root
use chroot = no
max connections = 3600
strict modes = yes
port = 873
address = 192.168.20.201
# rsync模块名,后面配置sersync会用到
[data]
# 该同步目录只要uid所指定的用户有写权限即可
path = /home/nginx/html
comment = mirror for backup
ignore errors
read only = no
list = no
auth users = user
# 密码认证文件,必须为600权限,否则rsync传输会报错
secrets file = /etc/rsync.pas
hosts allow = *
# hosts deny = 0.0.0.0/0
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
--------------------
4.4创建同步目录
# mkdir /home/nginx/html
4.5配置认证文件
# echo "user:123456" > /etc/rsync.pas
# chmod 600 /etc/rsync.pas
# rsync --daemon --config=/etc/rsyncd.conf
4.6重启xinetd使其配置生效:
# /etc/init.d/xinetd restart
4.7设置开机启动:
# echo "rsync --daemon --config=/etc/rsyncd.conf" >> /etc/rc.local


5.sersync (推送端A)
5.1下载sersync源码包
# wget http://sersync.googlecode.com/files/sersync2.5_64bit_binary_stable_final.tar.gz
5.2创建sersync目录结构
# mkdir /usr/local/sersync
# mkdir /usr/local/sersync/conf
# mkdir /usr/local/sersync/bin
# mkdir /usr/local/sersync/log
# tar zxvf sersync2.5_64bit_binary_stable_final.tar.gz
# cd GNU-Linux-x86/
# cp confxml.xml /usr/local/sersync/conf
# cp sersync2 /usr/local/sersync/bin
5.3配置sersync
5.3.1首先创建连接rsyncd的密码文件
# echo "123456" >/etc/rsync.pas
# chmod 600 /etc/rsync.pas
5.3.2配置confxml.xml
# cd /usr/local/sersync/conf
# vi confxml.xml
按照注释进行修改
-----------------------------
<?xml version="1.0" encoding="ISO-8859-1"?>
<head version="2.5">
   # 设置本地IP和端口
   <host hostip="localhost" port="8008"></host>
   # 开启DUBUG模式  
   <debug start="false"/>
   # 开启xfs文件系统
   <fileSystem xfs="false"/>
   # 同步时忽略推送的文件(正则表达式),默认关闭
   <filter start="true">
  <exclude expression="(.*)\.filepart"></exclude>
   </filter>
   <inotify>
   # 设置要监控的事件
       <delete start="true"/>
       <createFolder start="true"/>
       <createFile start="true"/>
       <closeWrite start="true"/>
       <moveFrom start="true"/>
       <moveTo start="true"/>
       <attrib start="true"/>
       <modify start="true"/>
   </inotify>
   <sersync>
   # 本地同步的目录路径
   <localpath watch="/home/nginx/html">
   # 远程IP和rsync模块名  
       <remote ip="192.168.20.201" name="data"/>  
       <!--<remote ip="192.168.8.39" name="tongbu"/>-->
       <!--<remote ip="192.168.8.40" name="tongbu"/>-->
   </localpath>
   <rsync>
   # rsync指令参数
       <commonParams params="-auvzP"/>
   # rsync同步认证
       <auth start="true" users="user" passwordfile="/etc/rsync.pas"/>
   # 设置rsync远程服务端口,远程非默认端口则需打开自定义(若开启rsync+ssh, 则这里需定义SSH端口)
       <userDefinedPort start="false" port="873"/><!-- port=873 -->
   # 设置超时时间
       <timeout start="true" time="100"/><!-- timeout=100 -->
   # 设置rsync+ssh加密传输模式,默认关闭,开启需设置SSH加密证书
       <ssh start="false"/>
   </rsync>
    # sersync传输失败日志脚本路径,每隔60会重新执行该脚本,执行完毕会自动清空。
       <failLog path="/usr/local/sersync/log/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
    # 设置rsync+crontab定时传输,默认关闭
       <crontab start="false" schedule="600"><!--600mins-->
           <crontabfilter start="false">
               <exclude expression="*.php"></exclude>
               <exclude expression="info/*"></exclude>
           </crontabfilter>
       </crontab>
   # 设置sersync传输后调用name指定的插件脚本,默认关闭
       <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>
-----------------------------
5.3.3创建推送端sersync同步目录
# mkdir /home/nginx/html
5.3.4设置环境变量:
# echo "export PATH=$PATH:/usr/local/sersync/bin/" >> /etc/profile
# source /etc/profile
5.3.5启动sersync
# sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
注:重启操作如下:
# killall sersync2 && sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml
5.3.6设置开机启动
# echo "sersync2 -r -d -o /usr/local/sersync/conf/confxml.xml" >> /etc/rc.local


6.验证:
(推送端)
# cd /home/nginx/html
# touch 1 2 3 4 5
# echo "test sersync" > 1
(接收端)
# cd /home/nginx/html
# ls
--------------------
1  2  3  4  5
--------------------
# cat 1
--------------------
test sersync


7.课外拓展
7.1可以用下面这句话在源服务器验证安装配置是否成功,也可以用它来手动同步文件(手动同步时会同步所有文件,包含.或~开头的文件):
rsync -avH --port=873 --progress --delete /home/nginx/html/ user@192.168.20.201::data --password-file=/etc/rsync.pas
7.2对于sersync监控的文件,会默认过滤系统的临时文件(以“.”开头,以“~”结尾),除了这些文件外,可以自定义其他需要过滤的文件。
<filter start="true">
    <exclude expression="(.*)\.svn"></exclude>
    <exclude expression="(.*)\.gz"></exclude>
    <exclude expression="^info/*"></exclude>
    <exclude expression="^static/*"></exclude>
</filter>
将start设置为 true,在exclude标签中,填写正则表达式,默认给出两个例子分别是过滤以”.gz”结尾的文件与过滤监控目录下的info路径(监控路径 /info /*),
可以根据需要添加,但开启的时候,自己测试一下,正则表达式如果出现错误,控制台会有提示。
7.3用一下命令可以对比两个服务器文件个数:
查看某文件夹下文件的个数,包括子文件夹里的。
ls -lR|grep "^-"|wc -l
查看某文件夹下文件夹的个数,包括子文件夹里的。
ls -lR|grep "^d"|wc -l


参考:

http://www.showerlee.com/archives/59

http://blog.csdn.net/penglonghu/article/details/7030640

http://www.cnblogs.com/myblog1314/p/4569797.html


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值