配置sersync+rsync实现实时同步环境说明

配置sersync+rsync实现实时同步环境说明

一台装sersync二台装rsync服务

Sersync服务器(数据源,源机器):9.1.101.122   redhat6-7-1

Rsync服务器(备份端,目标机器):9.1.101.124   redhat6-7-2

Rsync服务器(备份端,目标机器):9.1.101.125   redhat6-7-3

配置rsync服务器

在9.1.101.124和9.1.101.125服务器上配置

[root@redhat6-7-2 ~]# service iptables stop   关闭防火墙

[root@redhat6-7-2 ~]# chkconfig iptables off 关闭防火墙自启动

[root@redhat6-7-2 ~]# yum -y install rsync xinetd  安装xinetd 和rsync

[root@redhat6-7-2 ~]#  chkconfig rsync on   开机启动rsync服务

[root@redhat6-7-2 ~]#  service xinetd start   启动xinetd服务

配置rsync配置文件:/etc/rsyncd.conf

[root@redhat6-7-2 ~]# cat /etc/rsyncd.conf

log file = /var/log/rsyncd.log          日志存放位置

pidfile = /var/run/rsyncd.pid             pid文件位置

lock file = /var/run/rsync.lock            进程PID,自动生成

motd file =/etc/rsyncd.motd              客户端登陆之后弹出的消息,需要创建

uid = root                                         进程身份

gid = root                                         进程组

use chroot = yes                               是否囚牢,锁定家目录,rsync被黑之后,黑客无法再rsync运行的家目录之外创建文件,选项设置为yes

max connections = 1200                    最大连接数

timeout = 600                                   超时时间

[book]                                              共享模块名称

path = /data/book/                           备份文件存放路径

comment = book                              描述

port=873                                          使用端口

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

list = yes                                           是否允许查看模块信息

auth users = rsyncuser                      备份的用户,和系统用户无关

hosts allow = 9.1.101.122                   允许同步客户端的IP地址,可以是网段,或者用*表示所有 192.168.1.0/24或192.168.1.0/255.255.255.0

secrets file = /etc/rsync.passwd         存放用户的密码文件,格式是  用户名:密码

配置文件分为两部分:全局参数,模块参数

全局参数:对rsync服务器生效,如果模块参数和全局参数冲突,冲突的地方模块参数生效

模块参数:定义需要通过rsync输出的目录定义的参数

创建提示文件和用户密码

[root@ redhat6-7-2 ~]# echo "Welcome to Backup Server" > /etc/rsyncd.motd

[root@ redhat6-7-2 ~]# vim /etc/rsync.passwd

rsyncuser:password123

[root@ redhat6-7-2 ~]# chmod 600 /etc/rsync.passwd                 //目录权限必须是700或者600,否则的话身份验证会失效,设置rsync user的时候

启动服务测试

[root@redhat6-7-2 ~]# rsync --daemon --config=/etc/rsyncd.conf

查看进程和端口

[root@redhat6-7-2 ~]# netstat -anput | grep 873

tcp        0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      9287/rsync         

tcp        0      0 :::873                      :::*                        LISTEN      9287/rsync 

测试

rsync语法:   rsync 选项 用户名@备份源服务器IP::共享模块名 目标目录

[root@redhat6-7-1 ~]# rsync -avz --delete  /var/www/html rsyncuser@9.1.101.124::book

Welcome to Backup Server

Password:       #输入密码password123

Rsync命令

       rsync命令和scp命令很相似

-a, --archive archive mode 权限保存模式,相当于 -rlptgoD 参数,存档,递归,保持属性等

-r, --recursive 复制所有下面的资料,递归处理

-p, --perms 保留档案权限 ,文件原有属性

-t, --times 保留时间点,文件原有时间

-g, --group 保留原有属组

-o, --owner 保留档案所有者(root only)

-D, --devices 保留device资讯(root only)

-l, --links 复制所有的连接 ,拷贝连接文件

-z, --compress 压缩模式, 当资料在传送到目的端进行档案压缩.

-H, --hard-links 保留硬链接文件

-A, --acls 保留ACL属性文件,需要配合--perms

-P,-P参数和 --partial --progress 相同.只是为了把参数简单化,表示传进度

--version, 输出rsync版本

-v , --verbose 复杂的输出信息

-u, --update 仅仅进行更新,也就是跳过已经存在的目标位置,并且文件时间要晚于要备份的文件,不覆盖新的文件

--port=PORT, 定义rsyncd(daemon)要运行的port(预设为tcp 873)

--delete, 删除那些目标位置有的文件而备份源没有的文件

--password-file=FILE ,从 FILE 中得到密码

--bwlimit=KBPS, 限制 I/O 带宽

--filter “-filename”,需要过滤的文件

--exclude=filname,需要过滤的文件

--progress,显示备份过程

 

另外,使用rsync一定要注意的一点是源路径如果是一个目录的话,带上尾随斜线和不带尾随斜线是不一样的,不带尾随斜线表示的是整个目录包括目录本身,带上尾随斜线表示的是目录中的文件,不包括目录本身例如:

[root@xuexi ~]# rsync -a /etc /tmp
[root@xuexi ~]# rsync -a /etc/ /tmp

第一个命令会在/tmp目录下创建etc目录,而第二个命令不会在/tmp目录下创建etc目录,源路径/etc/中的所有文件都直接放在/tmp目录下。

配置sersync服务器

上传文件sersync2.5.4_64bit_binary_stable_final.tar.gz到9.1.101.122的/root目录下并解压:

[root@redhat6-7-1 ~]# tar xvf sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@redhat6-7-1 ~]# mv GNU-Linux-x86 sersync  方便后期使用

配置sersync

[root@ redhat6-7-1 sersync]# cp confxml.xml confxml.xml.bak

更改优化sersync配置文件:

 修改24--28行

<sersync>

        <localpath watch="/opt/data">    #本地同步目录

            <remote ip="9.1.101.124" name="book"/>   #rsync模块名称

               <remote ip="9.1.101.125" name="book"/>

修改31--34行,认证部分【rsync密码认证】

<rsync>

            <commonParams params="-artuz"/>

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

            <userDefinedPort start="false" port="874"/><!-- port=874 -->

            <timeout start="false" time="100"/><!-- timeout=100 -->

            <ssh start="false"/>

 

开启sersync守护进程同步数据

[root@ redhat6-7-1 sersync]#/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml

[

设置sersync监控开机自动执行

 

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

/opt/sersync/sersync2 -d -r -o /opt/sersync/confxml.xml

 

 

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

 

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

#!/bin/sh

sersync="/root /sersync/sersync2"

confxml="/root /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

 

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

 

把这个脚本加到任务计划,定期执行检测

 

补充: 多实例情况

 1、配置多个confxml.xml文件(比如:www、bbs、blog....等等)

 2、根据不同的需求同步对应的实例文件

/root/sersync/sersync2  -d -o /root/sersync/www_confxml.xml

/root/sersync/sersync2  -d -o /root/sersync/bbs_confxml.xml

 

Sersync参数说明
./sersync -r

-r参数作用是:开启实时监控的之前对主服务器目录与远程目标机器的目录进行一次整体同步;如果需要将sersync运行前,主服务器目录下已经存在的所有文件或目录全部同步到远端,则要以 -r参数运行sersync,将本地与远程整体同步一次;

提别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步;
./sersync -o xx.xml

不指定 -o参数: sersync使用sersync可执行文件目录下的默认配置文件confxml.xml

指定 -o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步
./sersync -n num

-n参数为:指定默认的线程池的线程总数;

例如: ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率;
./sersync -d-d参数为:后台服务,通常情况下使用 -r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步;在第一次整体同步时,-d 和 -r参数经常会联合使用;

./sersync -m

pluginName

-m参数:不进行同步,只运行插件 ./sersync -m pluginName

例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件

 

sersync服务配置文件参数详解

4.1 初始的配置文件

    sersync可选功能是通过xml配置文件来实现的,基本配置文件如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

[root@cache sersync]# cat confxml.xml.2015-06-05 

     1 <?xml version="1.0" encoding="ISO-8859-1"?>

     2 <head version="2.5">

     3     <host hostip="localhost" port="8008"></host>

     4     <debug start="false"/>

     5     <fileSystem xfs="false"/>

     6     <filter start="false">

     7         <exclude expression="(.*)\.svn"></exclude>

     8         <exclude expression="(.*)\.gz"></exclude>

     9         <exclude expression="^info/*"></exclude>

    10         <exclude expression="^static/*"></exclude>

    11     </filter>

    12     <inotify>

    13         <delete start="true"/>

    14         <createFolder start="true"/>

    15         <createFile start="false"/>

    16         <closeWrite start="true"/>

    17         <moveFrom start="true"/>

    18         <moveTo start="true"/>

    19         <attrib start="false"/>

    20         <modify start="false"/>

    21     </inotify>

    22

    23     <sersync>

    24         <localpath watch="/opt/tongbu">

    25              <remoteip="127.0.0.1" name="tongbu1"/>

    26              <!--<remoteip="192.168.8.39" name="tongbu"/>-->

    27              <!--<remoteip="192.168.8.40" name="tongbu"/>-->

    28         </localpath>

    29         <rsync>

    30              <commonParamsparams="-artuz"/>

    31              <auth start="false"users="root" passwordfile="/etc/rsync.pas"/>

    32              <userDefinedPortstart="false" port="874"/><!-- port=874 -->

    33              <timeoutstart="false" time="100"/><!-- timeout=100 -->

    34              <sshstart="false"/>

    35         </rsync>

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

    37         <crontab start="false"schedule="600"><!--600mins-->

    38              <crontabfilterstart="false">

    39                  <excludeexpression="*.php"></exclude>

    40                  <excludeexpression="info/*"></exclude>

    41              </crontabfilter>

    42         </crontab>

    43         <plugin start="false" name="command"/>

    44     </sersync>

    45

    46     <plugin name="command">

    47         <param prefix="/bin/sh" suffix=""ignoreError="true"/> <!--prefix /opt/tongbu/mmm.sh suffix-->

    48         <filter start="false">

    49              <includeexpression="(.*)\.php"/>

    50              <includeexpression="(.*)\.sh"/>

    51         </filter>

    52     </plugin>

    53

    54      <plugin name="socket">

    55         <localpath watch="/opt/tongbu">

    56              <deshostip="192.168.138.20" port="8009"/>

    57         </localpath>

    58     </plugin>

    59     <plugin name="refreshCDN">

    60         <localpath watch="/data0/htdocs/cms.xoyo.com/site/">

    61              <cdninfodomainname="ccms.chinacache.com" port="80"username="xxxx" passwd="xxxx"/>

    62              <sendurlbase="http://pic.xoyo.com/cms"/>

    63              <regexurlregex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>

    64         </localpath>

    65     </plugin>

    66 </head>

4.2 xml配置文件说明

    说明: xml配置文件的注释不用“#”,而是<!-- 中间是注释内容 -->

1

     3     <host hostip="localhost"port="8008"></host>

hostip与port是针对插件的保留字段,对于同步功能没有任何作用,保留默认即可;

4.3 Debug开启开关

1

     4     <debug start="false"/>

设置为true,表示开启debug模式,会在sersync正在运行的控制台打印inotify时间与rsync同步命令;

4.4 XFS文件系统开关

1

     5     <fileSystem xfs="false"/>

对于xfs文件系统的用户,需要将这个选项开启,才能使用sersync正常工作;

4.5 filter文件过滤功能

    说明:一般情况下,不给客户端添加过滤,如有必要才添加;

1

2

3

4

5

6

     6     <filter start="false">

     7         <exclude expression="(.*)\.svn"></exclude>

     8         <exclude expression="(.*)\.gz"></exclude>

     9         <exclude expression="^info/*"></exclude>

    10         <exclude expression="^static/*"></exclude>

    11     </filter>

对于大多数应用,可以尝试把createFile(监控文件事件选项)设置为false来提高性能,减少rsync通讯;

因为拷贝文件到监控目录会产生create事件与close_write事件,所以如果关闭create事件,只监控文件拷贝结束时的时间close_write,同样可以实现文件完整同步;

注意:强将creatFolder保持为true,如果将createFolder设为false,则不会对产生的目录进行监控,该目录下的子文件与子目录也不会被监控;所以除非特殊需要,请开启; 默认情况下对创建文件(目录)事件与删除文件(目录)事件都进行监控,如果项目中不需要删除远程目标服务器的文件(目录),则可以将delete参数设置为false,则不对删除事件进行监控;

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值