Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步

一、为什么要用Rsync+sersync架构?

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

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

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

1、Rsync+Inotify-tools

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

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

2、Rsync+sersync

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

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

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

三、目标机安装rsync

1、安装rsync

1

2

3

4

5

6

[root@Legion101 ~]# yum install rsync xinetd -y

[root@Legion101 ~]# sed -ri 's/(disable).*/\1 = no/' /etc/xinetd.d/rsync #修rsync为开机自动启动

[root@Legion101 ~]# grep 'disable' /etc/xinetd.d/rsync

        disable = no

[root@Legion101 ~]# service xinetd start #CentOS中是以xinetd来管理Rsync服务的所以启动rsync是启动xinetd

[root@Legion101 ~]# service xinetd stop

2、配置rysnc

01

02

03

04

05

06

07

08

09

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

[root@Legion101 ~]# vi /etc/rsyncd.conf #写入如下配置内容

[root@Legion101 ~]# cat /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启动时欢迎信息页面文件位置(文件内容自定义)

[home_wwwroot_www.dwhd.org]

#自定义名称

path = /home/wwwroot/www.dwhd.org/

#rsync服务端数据目录路径

comment = home_wwwroot_www.dwhd.org

#模块名称与[home_www.osyunwei.com]自定义名称相同

uid = root

#设置rsync运行权限为root

gid = root

#设置rsync运行权限为root

port=873

#默认端口

use chroot = no

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

read only = no

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

list = no

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

max connections = 200

#最大连接数

timeout = 600

#设置超时时间

auth users = lookback,legion

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

hosts allow = 172.16.6.100

#由于实验是1主多从,所以这里设置成主的IP

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

hosts deny = 172.16.0.1

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

Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步

3、创建用户认证文件

1

2

3

4

5

6

7

8

9

[root@Legion101 ~]# vi /etc/rsync.pass

[root@Legion101 ~]# cat /etc/rsync.pass #格式,用户名:密码,可以设置多个,每行一个用户名:密码

lookback:lookback

legion:lookback

[root@Legion101 ~]# chmod 600 /etc/rsync{d.conf,.pass} #修改/etc/rsyncd.conf和/etc/rsync.pass文件的权限

[root@Legion101 ~]# ls -l /etc/rsync{d.conf,.pass}

-rw------- 1 root root 1470 7月   5 06:27 /etc/rsyncd.conf

-rw------- 1 root root   34 7月   5 06:23 /etc/rsync.pass

[root@Legion101 ~]#

Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步
重启下rsync

1

2

3

4

5

6

7

8

[root@Legion101 ~]# service xinetd restart

停止 xinetd:                                              [确定]

正在启动 xinetd:                                          [确定]

[root@Legion101 ~]# ss -tnlp | grep xinetd

LISTEN     0      64                       :::873                     :::*      users:(("xinetd",41905,5))

[root@Legion101 ~]# ps aux | pgrep xinetd

41905

[root@Legion101 ~]#

四、源服务器安装rsync和sersync

1、安装rsync

01

02

03

04

05

06

07

08

09

10

[ -z "$(whereis rsync)" ] && yum install xinetd rsync -y || yum install xinetd -y

[root@Legion100 ~]# sed -ri 's/(disable).*/\1 = no/' /etc/xinetd.d/rsync

[root@Legion100 ~]# grep 'disable' /etc/xinetd.d/rsync

        disable = no

[root@Legion100 ~]# service xinetd start

正在启动 xinetd:                                          [确定]

[root@Legion100 ~]# echo "lookback" > /etc/passwd.txt #创建认证密码文件,密码是lookback

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

[root@Legion100 ~]# mkdir -p /home/wwwroot/www.dwhd.org/sersync`date "+%F-%H-%M-%S"` #创建一个sersynsc加当前时间的文件夹

[root@Legion100 ~]# rsync -avH --port=873 --progress  --password-file=/etc/passwd.txt --delete /home/wwwroot/www.dwhd.org/ rsync://lookback@172.16.6.101//home_wwwroot_www_dwhd_org

Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步

Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步
rsync参数解释

01

02

03

04

05

06

07

08

09

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

-v, --verbose 详细模式输出

-q, --quiet 精简输出模式

-c, --checksum 打开校验开关,强制对文件传输进行校验

-a, --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD

-r, --recursive 对子目录以递归模式处理

-R, --relative 使用相对路径信息

-b, --backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为~filename。可以使用--suffix选项来指定不同的备份文件前缀。

--backup-dir 将备份文件(如~filename)存放在在目录下。

-suffix=SUFFIX 定义备份文件前缀

-t, --times 保持文件时间信息

-u, --update 仅仅进行更新,也就是跳过所有已经存在于DST,并且文件时间晚于要备份的文件。(不覆盖更新的文件)

-l, --links 保留软链结

-L, --copy-links 想对待常规文件一样处理软链结

--copy-unsafe-links 仅仅拷贝指向SRC路径目录树以外的链结

--safe-links 忽略指向SRC路径目录树以外的链结

-H, --hard-links 保留硬链结     -p, --perms 保持文件权限

-o, --owner 保持文件属主信息     -g, --group 保持文件属组信息

-D, --devices 保持设备文件信息    -t, --times 保持文件时间信息

-S, --sparse 对稀疏文件进行特殊处理以节省DST的空间

-n, --dry-run现实哪些文件将被传输

-W, --whole-file 拷贝文件,不进行增量检测

-x, --one-file-system 不要跨越文件系统边界

-B, --block-size=SIZE 检验算法使用的块尺寸,默认是700字节

-e, --rsh=COMMAND 指定使用rsh、ssh方式进行数据同步

--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息

-C, --cvs-exclude 使用和CVS一样的方法自动忽略文件,用来排除那些不希望传输的文件

--existing 仅仅更新那些已经存在于DST的文件,而不备份那些新创建的文件

--delete 删除那些DST中SRC没有的文件

--delete-excluded 同样删除接收端那些被该选项指定排除的文件

--delete-after 传输结束以后再删除

--ignore-errors 及时出现IO错误也进行删除

--max-delete=NUM 最多删除NUM个文件

--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输

--force 强制删除目录,即使不为空

--numeric-ids 不将数字的用户和组ID匹配为用户名和组名

--timeout=TIME IP超时时间,单位为秒

-I, --ignore-times 不跳过那些有同样的时间和长度的文件

--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间

--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为0

-T --temp-dir=DIR 在DIR中创建临时文件

--compare-dest=DIR 同样比较DIR中的文件来决定是否需要备份

-P 等同于 --partial

--progress 显示备份过程

-z, --compress 对备份的文件在传输时进行压缩处理

--exclude=PATTERN 指定排除不需要传输的文件模式

--include=PATTERN 指定不排除而需要传输的文件模式

--exclude-from=FILE 排除FILE中指定模式的文件

--include-from=FILE 不排除FILE指定模式匹配的文件

--version 打印版本信息

--address 绑定到特定的地址

--config=FILE 指定其他的配置文件,不使用默认的rsyncd.conf文件

--port=PORT 指定其他的rsync服务端口

--blocking-io 对远程shell使用阻塞IO

-stats 给出某些文件的传输状态

--progress 在传输时现实传输过程

--log-format=formAT 指定日志文件格式

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

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

-h, --help 显示帮助信息

2、修改inotify参数

1

2

3

4

5

6

7

[root@Legion100 ~]# ll /proc/sys/fs/inotify #列出文件目录,出现下面的内容,说明服务器内核支持inotify

总用量 0

-rw-r--r-- 1 root root 0 7月   5 13:50 max_queued_events

-rw-r--r-- 1 root root 0 7月   5 13:50 max_user_instances

-rw-r--r-- 1 root root 0 7月   5 13:50 max_user_watches

#备注:Linux下支持inotify的内核最小为2.6.13,可以输入命令:uname -a查看内核

#CentOS 5.X 内核为2.6.18,默认已经支持inotify

 

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

[root@Legion100 ~]# sysctl -a | grep 'max_queued_events\|max_user_watches\|max_user_instances' #查看系统上inotify的默认参数

fs.inotify.max_user_instances = 128 #每个用户创建inotify实例最大值

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

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

fs.epoll.max_user_watches = 385802

[root@Legion100 ~]# echo "fs.inotify.max_queued_events=99999999

fs.inotify.max_user_watches=99999999

fs.inotify.max_user_instances=65535" >> /etc/sysctl.conf

[root@Legion100 ~]# sysctl -p

[root@Legion100 ~]# sysctl -a | grep 'max_queued_events\|max_user_watches\|max_user_instances'

fs.inotify.max_user_instances = 65535

fs.inotify.max_user_watches = 99999999

fs.inotify.max_queued_events = 99999999

fs.epoll.max_user_watches = 385802

[root@Legion100 ~]#

3、安装sersync

01

02

03

04

05

06

07

08

09

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

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

[root@Legion100 ~]# mkdir /tmp/src && cd /tmp/src

[root@Legion100 /tmp/src]# wget https://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@Legion100 /tmp/src]# tar xf sersync2.5.4_64bit_binary_stable_final.tar.gz

[root@Legion100 /tmp/src]# ls GNU-Linux-x86/

confxml.xml  sersync2

[root@Legion100 /tmp/src]# mv GNU-Linux-x86  /usr/local/sersync

[root@Legion100 /tmp/src]# ls /usr/local/sersync/

confxml.xml  sersync2

[root@Legion100 /tmp/src]# cd /usr/local/sersync

[root@Legion100 /usr/local/sersync]# cp -a confxml.xml confxml.xml_backup_`date +%F`

[root@Legion100 /usr/local/sersync]# ls -l

总用量 1776

-rwxr-xr-x 1 root root    2214 10月 26 2011 confxml.xml

-rwxr-xr-x 1 root root    2214 10月 26 2011 confxml.xml_backup_2015-07-05

-rwxr-xr-x 1 root root 1810128 10月 26 2011 sersync2

[root@Legion100 /usr/local/sersync]# cat 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="/home/wwwroot/www.dwhd.org">

            <remote ip="172.16.6.101" name="home_wwwroot_www_dwhd_org"/>

            <!--<remote ip="192.168.8.39" name="tongbu"/>-->

            <!--<remote ip="192.168.8.40" name="tongbu"/>-->

        </localpath>

        <rsync>

            <commonParams params="-artuz"/>

            <auth start="true" users="lookback" passwordfile="/etc/passwd.txt"/>

            <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>

[root@Legion100 /usr/local/sersync]#

 

#参数说明:

localpath watch="/home/wwwroot/www.dwhd.org"#源服务器同步目录

name="home_wwwroot_www_dwhd_org"#目标服务器rsync同步目录模块名称

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

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

remote ip="72.16.6.101"#目标服务器ip,每行一个

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

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

4、测试运行

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

[root@Legion100 /usr/local/sersync]# /usr/local/sersync/sersync2 -d -r -o  /usr/local/sersync/confxml.xml

set the system param

execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches

execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events

parse the command param

option: -d      run as a daemon

option: -r      rsync all the local files to the remote servers before the sersync work

option: -o      config xml name:  /usr/local/sersync/confxml.xml

daemon thread num: 10

parse xml config file

host ip : localhost     host port: 8008

daemon start,sersync run behind the console

Start the crontab       Every 600 minutes rsync all the files to the remote servers entirely

config xml parse success

please set /etc/rsyncd.conf max connections=0 Manually

sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)

Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)

please according your cpu ,use -n param to adjust the cpu rate

------------------------------------------

rsync the directory recursivly to the remote servers once

working please wait...

execute command: cd /home/wwwroot/www.dwhd.org && rsync -artuz -R --delete ./ 172.16.6.101::home_wwwroot_www_dwhd_org >/dev/null 2>&1

5、设置开机启动

1

2

3

4

5

6

7

8

[root@Legion100 ~]# wget http://www.dwhd.org/script/sersync_init -O /etc/rc.d/init.d/sersync

[root@Legion100 ~]# chmod +x /etc/rc.d/init.d/sersync

[root@Legion100 ~]# chkconfig sersync on

[root@Legion100 ~]# echo "export PATH=\$PATH:/usr/local/sersync" > /etc/profile.d/sersysn.sh

[root@Legion100 ~]# . /etc/profile.d/sersync.sh

[root@Legion100 ~]# which sersync2

/usr/local/sersync/sersync2

[root@Legion100 ~]# echo -e "\nsersync_script" >> /etc/rc.d/rc.local

6、测试

01

02

03

04

05

06

07

08

09

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

[root@Legion100 ~]# tree /home/ && date

/home/

└── wwwroot

    └── www.dwhd.org

        ├── fileTest2015-07-05-01

        ├── fileTest2015-07-05-02

        ├── fileTest2015-07-05-03

        ├── fileTest2015-07-05-04

        ├── fileTest2015-07-05-05

        ├── fileTest2015-07-05-06

        ├── fileTest2015-07-05-07

        ├── fileTest2015-07-05-08

        ├── fileTest2015-07-05-09

        └── fileTest2015-07-05-10

 

2 directories, 10 files

2015年 07月 05日 星期日 16:26:33 CST

[root@Legion100 ~]# ls -l /home/wwwroot/www.dwhd.org/

总用量 0

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-01

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-02

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-03

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-04

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-05

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-06

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-07

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-08

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-09

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-10

[root@Legion100 ~]# \rm -rf /home/wwwroot/www.dwhd.org/*

[root@Legion100 ~]# touch /home/wwwroot/www.dwhd.org/fileTest`date +%F-%H-%M-%S`-{01..10}

[root@Legion100 ~]# ls -l /home/wwwroot/www.dwhd.org/

总用量 0

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-01

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-02

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-03

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-04

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-05

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-06

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-07

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-08

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-09

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-10

[root@Legion100 ~]#

 

01

02

03

04

05

06

07

08

09

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

[root@Legion101 ~]# tree /home/ && date

/home/

├── lookback

└── wwwroot

    └── www.dwhd.org

        ├── fileTest2015-07-05-01

        ├── fileTest2015-07-05-02

        ├── fileTest2015-07-05-03

        ├── fileTest2015-07-05-04

        ├── fileTest2015-07-05-05

        ├── fileTest2015-07-05-06

        ├── fileTest2015-07-05-07

        ├── fileTest2015-07-05-08

        ├── fileTest2015-07-05-09

        └── fileTest2015-07-05-10

 

3 directories, 10 files

2015年 07月 05日 星期日 16:25:24 CST

[root@Legion101 ~]# ls -l /home/wwwroot/www.dwhd.org/

总用量 0

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-01

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-02

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-03

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-04

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-05

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-06

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-07

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-08

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-09

-rw-r--r-- 1 root root 0 7月   5 16:14 fileTest2015-07-05-10

[root@Legion101 ~]# ls -l /home/wwwroot/www.dwhd.org/

总用量 0

[root@Legion101 ~]# ls -l /home/wwwroot/www.dwhd.org/

总用量 0

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-01

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-02

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-03

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-04

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-05

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-06

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-07

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-08

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-09

-rw-r--r-- 1 root root 0 7月   5 16:27 fileTest2015-07-05-16-27-19-10

[root@Legion101 ~]#

Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步
Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步

7、sersync参数说明
Linux之Rsync+Sersync实现负载均衡节点文件数据实时同步

8、Sersync配置文件说明

01

02

03

04

05

06

07

08

09

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

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

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

<head version="2.5">

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

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

    <debug start="false"/>

    <!--该行为Debug开启开关。true为开启debug模式,会在sersync正在运行的控制台,打印 inotify 事件与 rsync 同步命令,生产环境一般不开启.-->

    <fileSystem xfs="false"/>

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

    对于sersync监控的文件,会默认过滤系统的临时文件(以"."开头,以"~"结尾),除了这些文件外,

    在15-20行中,我们还可以自定义其它需要过滤的文件。

    通过将 start 设置为 true 后可开启过滤功能,在exclude标签中可使用正则表达式。

    默认给出的两个例子分别是过滤以".gz"结尾的文件与过滤监控目录下的info路径(监控路径/info/*),可以根据需求自己添加。

    但在开启的时候,自己一定要测试下,如果正则表达式出现错误,控制台会有相应提示。

    相比较使用 Rsync 的 exclude 功能,被过滤的路径,不会加入监控,大大减少 Rsync 同步的通讯量-->

    <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>

    <!--用来定义 inotify 监控参数,我们可以根据项目的特点来优化 Sersync。

    对于大多数应用,可以尝试把 createFile(监控文件事件选项)设置为false来提高性能,进一步减少 Rsync通讯。因为拷贝文件到监控目录会产生 create 事件与 close_write 事件,所以如果关闭create 事件,只监控文件拷贝结束时的事件 close_write,同样可以实现文件完整同步。

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

    <sersync>

    <localpath watch="/home/wwwroot/www.dwhd.org">

        <remote ip="172.16.6.101" name="home_wwwroot_www_dwhd_org"/>

        <!--<remote ip="192.168.8.39" name="tongbu"/>-->

        <!--<remote ip="192.168.8.40" name="tongbu"/>-->

    </localpath>

    <!--/home/wwwroot/www.dwhd.org目录为sersync主服务器本地待同步的目录,ip="172.16.6.100"为从服务器的ip地址,如果有多个服务器,依次列出来即可。name="home_wwwroot_www_dwhd_org",这里的home_wwwroot_www_dwhd_org为rsyncd.conf中的模块名字,即中括号中的名称。-->

    <rsync>

        <commonParams params="-artuz"/>

        <auth start="true" users="lookback" passwordfile="/etc/passwd.txt"/>

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

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

        <ssh start="false"/>

    </rsync>

    <!--在 commonParams 项,我们可以自定义rsync的同步参数,默认是-artuz,auth start="false"设置为true的时候,使用rsync的认证模式传送,需要配置user与passwordfile(-password-file=/etc/passwd.txt)来使用。userDefinedPort 当远程同步目标服务器的rsync端口不是默认端口的时候使用(-port=874)。timeout设置rsync的timeout事件(-timeout=100)。<ssh start="false"/>如果开启表示ssh使用rsync -e ssh的方式进行传输。-->

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

    <!--用来定义失败日志脚本配置,如果文件同步传输失败,会重新传送,再次失败就会写入 rsync_fail_log.sh,然后每隔一段时间(timeToExecute进行设置)执行该脚本再次重新传送,然后清空该脚本。可以通过path来设置日志路径-->

    <crontab start="true" schedule="600"><!--600mins-->

        <crontabfilter start="false">

        <exclude expression="*.php"></exclude>

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

        </crontabfilter>

    </crontab>

    <!--用来定义Crontab定期整体同步功能,Crontab可以对监控路径与远程目标主机每隔一段时间进行一次整体同步,可能由于一些原因两次失败重传都失败了,这个时候如果开启了 crontab 功能,还可以进行一次保证各个服务器文件一致,如果文件量比较大,crontab的时间间隔要设的大一些,否则可能增加通讯开销,schedule这个参数是设置crontab的时间间隔,默认是600分钟。

    如果开启了 filter 文件过滤功能,那么crontab整体同步也需要设置过滤,否则虽然实时同步的时候文件被过滤了,但 crontab 整体同步的时候,如果不单独设置crontabfilter,还会将需过滤的文件同步到远程从服务器,crontab的过滤正则与filter过滤的不同,也给出了两个实例分别对应与过滤文件与目录,总之如果同时开启了filter与crontab,则要开启crontab的crontabfilter,并按示例设置使其与filter的过滤一一对应。-->

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

    </sersync>

 

    <!--此处到行尾,都是插件的相关信息。当plugin标签设置为true时候,在同步文件或路径到远程服务器之后,会调用插件。通过name参数指定需要执行的插件。目前支持的有command、refreshCDN、socket、http四种插件。其中,http插件目前由于兼容性原因已经去除,以后会重新加入。-->

    <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>

    <!--socket插件,开启该模块,则向指定ip与端口发送inotify所产生的文件路径信息-->

 

    <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>

    <!--refreshCDN插件的相关配置,refreshCDN 用来在同步过程中将文件发送到目地服务器后,刷新cdn接口。如果不想使用,则将start属性设为false即可。该模块根据chinaCDN的协议,进行设计,当有文件产生的时候,就向cdn解耦发送需要刷新的路径为止。其中localpath watch="/data0/htdocs/cms.xoyo.com/site/"是需要监控的目录。cdinfo标签指定了cdn接口的域名,端口号,以及用户名与密码。sendurl 标签是需要刷新的url的前缀。regexurl 标签中,regex属性为true时候,使用match属性的正则语句匹配inotify返回的路径信息,并将正则匹配到的部分作为url一部分,

    上面配置文件自带的意思为,如果产生文件事件为:/data0/htdoc/cms.xoyo.com/site/jx3.xoyo.com/image/a/123.txt

    经过上面的match正则匹配后,最后刷新的路径是:http://pic.xoyo.com/cms/a/123.txt

    如果regex属性为false,最后刷新的路径就是:http://pic.xoyo.com/cms/jx3.xoyo.com/images/a/123.txt-->

 

</head>

 

原文出处:https://www.dwhd.org/20150705_055954.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值