=
=
=
-
1. 在同步服务器(Master)上开启sersync服务,sersync负载监控配置路径中的文件系统事件变化;
-
2. 调用rsync命令把更新的文件同步到目标服务器(S1 和 S2);
-
3. 需要在主服务器配置sersync,在同步目标服务器配置rsync server(注意:是rsync服务)
1
2
3
4
5
6
|
[root@web ~]
# cat/etc/redhat-release
CentOS release 6.6 (Final)
[root@web ~]
# uname -r
2.6.32-504.el6.x86_64
[root@web ~]
# uname -m
x86_64
|
1
2
3
|
[root@web1 ~]
# rsync --version|head -2
rsync
version 3.0.6 protocol version 30
Copyright (C) 1996-2009 byAndrew Tridgell, Wayne Davison, and others.
|
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
|
[root@web1 ~]
# vim /etc/rsyncd.conf
#Rsync server
uid = root
gid = root
use chroot = no
# 安全相关
max connections = 2000
# 并发连接数
timeout = 600
# 超时时间(秒)
pid
file
=
/var/run/rsyncd
.pid
# 指定rsync的pid目录
lock
file
=
/var/run/rsync
.lock
# 指定rsync的锁文件【重要】
log
file
=
/var/log/rsyncd
.log
# 指定rsync的日志目录
ignore errors
read
only =
false
list =
false
hosts allow = 172.16.1.0
/24
hosts deny = 0.0.0.0
/32
auth
users
= rsync_backup
secrets
file
=
/etc/rsync
.password
#################################################
[www]
# 模块
comment = www
path =
/data/www/
#################################################
[bbs]
comment = bbs
path =
/data/bbs/
#################################################
[blog]
comment = blog
path =
/data/blog/
#rsync_config____________end
|
1
2
3
4
5
6
|
[root@web1 ~]
# echo"rsync_backup:liubl">/etc/rsync.password
[root@web1 ~]
# chmod 600 /etc/rsync.password
[root@web1 ~]
# ll /etc/rsync.password
-rw-------. 1 root root 19Jun 3 18:19
/etc/rsync
.password
[root@web1 ~]
# cat /etc/rsync.password
rsync_backup:liubl
|
1
2
3
4
5
6
7
8
9
10
11
|
[root@web1 ~]
# rsync --daemon
[root@web1 ~]
# lsof -i:873
COMMAND PID USER FD TYPE DEVICE SIZE
/OFF
NODE NAME
rsync
1070 root 4u IPv4 17190189 0t0 TCP *:
rsync
(LISTEN)
[root@web1 ~]
#
[root@web1 ~]
# netstat -nulpt| grep rsync
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 1070
/rsync
设置开机自启动【写入到
/etc/rc
.
local
里面】
[root@web1 ~]
# vim /etc/rc.local
# rsync server progress
/usr/bin/rsync
--daemon
|
1
2
3
|
mkdir
-p
/data/
{www,bbs,blog}
tree
/data
提示: 此步骤在S1,S2都要执行,否则
rsync
服务会因为没有PATH路径而无法启动
|
1
2
3
4
5
6
|
[root@web ~]
# echo"liubl">/etc/rsync.password
[root@web ~]
# chmod 600 /etc/rsync.password
[root@web ~]
# ll/etc/rsync.password
-rw-------. 1 root root 19Jun 5 05:57
/etc/rsync
.password
[root@web ~]
# cat /etc/rsync.password
liubl
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@web ~]
# mkdir -p /data/{www,bbs,blog}
[root@web ~]
# touch /data/www/www.log /data/bbs/bbs.log/data/blog/blog.log
[root@web ~]
# tree /data/
/data/
├── bbs
│ └── bbs.log
├── blog
│ └── blog.log
└── www
└── www.log
3 directories, 3 files
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# rsync-avzP /data/www/ [email protected]::www/--password-file=/etc/rsync.password
sending incremental
file
list
sent 38 bytes received 8 bytes 92.00 bytes
/sec
total size is 0 speedup is 0.00
提示:
在后面进行部署sersync之前,sersync主服务器上必须要确保手工可以把文件推送到S1,S2上,这样后续sersync才能调用这些命令来自动推送
在推送前关闭iptables
|
1
2
3
4
5
6
7
8
9
10
|
[root@web1 ~]
# tree /data/
/data/
├── bbs
│ └── bbs.log
├── blog
│ └── blog.log
└── www
└── www.log
3 directories, 3 files
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
mkdir
-p
/applition/tools
cd
/applition/tools
wgethttps:
//sersync
.googlecode.com
/files/sersync2
.5.4_64bit_binary_stable_final.
tar
.gz
【有时下载失败,所有要本地留存才行】
[root@web ~]
# tar fxzsersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
[root@web ~]
# cd /usr/local/
[root@cache
local
]
# mv GNU-Linux-x86 sersync
[root@cache
local
]
# treesersync/
sersync/
├── confxml.xml
# 配置文件
└── sersync2
# 二进制文件【启动sersync使用】
0 directories, 2 files
|
1
2
3
4
5
6
|
[root@cache
local
]
# cp sersync/confxml.xmlsersync/confxml.xml.$(date +%F)
[root@cache
local
]
# ll sersync/confxml.xml
-rwxr-xr-x. 1 root root 2214Oct 26 2011 sersync
/confxml
.xml
[root@cache
local
]
# llsersync/confxml.xml*
-rwxr-xr-x. 1 root root 2214Oct 26 2011 sersync
/confxml
.xml
-rwxr-xr-x. 1 root root 2214Jun 5 06:38sersync
/confxml
.xml.2015-06-05
|
1
2
3
4
5
|
24 <localpathwatch=
"/opt/tongbu"
>
# 定义本地要同步的目录
25 <remote ip=
"127.0.0.1"
name=
"tongbu1"
/>
26 <!--<remoteip=
"192.168.8.39"
name=
"tongbu"
/>-->
# 同步到哪台机器上 tongbu模块rsync端模块名字
27 <!--<remoteip=
"192.168.8.40"
name=
"tongbu"
/>-->
# 同步到哪台机器上 tongbu模块
28 <
/localpath
>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<localpathwatch=
"/data/www"
>
<remoteip=
"172.16.1.25"
name=
"www"
/>
<remoteip=
"172.16.1.26"
name=
"www"
/>
<
/localpath
>
<!--
################################################## -->
<localpathwatch=
"/data/bbs"
>
<remoteip=
"172.16.1.25"
name=
"bbs"
/>
<remoteip=
"172.16.1.26"
name=
"bbs"
/>
<
/localpath
>
<!--
################################################## -->
<localpathwatch=
"/data/blog"
>
<remote ip=
"172.16.1.25"
name=
"blog"
/>
<remoteip=
"172.16.1.26"
name=
"blog"
/>
<
/localpath
>
<!--
################################################## -->
提示: 此步
watch
=
"/data/blog"
就是定义服务端待同步的目录,和目标服务器的模块name=
"blog"
|
1
2
3
4
5
6
7
|
<
rsync
>
<commonParamsparams=
"-artuz"
/>
<auth start=
"false"
users
=
"root"
passwordfile=
"/etc/rsync.pas"
/>
<userDefinedPortstart=
"false"
port=
"874"
/><!-- port=874 -->
<timeoutstart=
"false"
time
=
"100"
/><!-- timeout=100 -->
<sshstart=
"false"
/>
<
/rsync
>
|
1
2
3
4
5
6
7
8
9
|
<rsync>
<commonParamsparams=
"-artuz"
/>
<auth start=
"true"
users=
"rsync_backup"
passwordfile=
"/etc/rsync.password"
/>
<userDefinedPortstart=
"false"
port=
"874"
/><!-- port=
874
-->
<timeout start=
"true"
time=
"100"
/><!--timeout=
100
-->
<sshstart=
"false"
/>
</rsync>
# ***修改内容为 rsync的密码文件以及 同步所使用的账号类似:
rsync -avzP /data/www/rsync_backup@
172.16.
1.25
::www/ --password-file=/etc/rsync.password
|
1
|
<failLogpath=
"/tmp/rsync_fail_log.sh"
timeToExecute=
"60"
/><!--default every 60mins execute once-->
|
1
2
|
<failLog path=
"/usr/local/sersync/logs/rsync_fail_log.sh"
timeToExecute=
"60"
/><!--
default
every 60mins execute once-->
# 当同步失败后,日志记录到/usr/local/sersync/logs/rsync_fail_log.sh文件中,并且每
60
分钟对失败的log进行重新同步
|
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
68
69
70
71
72
73
74
75
76
77
|
[root
@cache
local]# cat sersync/confxml.xml
<?xmlversion=
"1.0"
encoding=
"ISO-8859-1"
?>
<headversion=
"2.5"
>
<host hostip=
"localhost"
port=
"8008"
></host>
<debug start=
"false"
/>
<fileSystem xfs=
"false"
/>
<filter start=
"false"
>
<excludeexpression=
"(.*)\.svn"
></exclude>
<excludeexpression=
"(.*)\.gz"
></exclude>
<excludeexpression=
"^info/*"
></exclude>
<excludeexpression=
"^static/*"
></exclude>
</filter>
<inotify>
<delete start=
"true"
/>
<createFolderstart=
"true"
/>
<createFilestart=
"false"
/>
<closeWritestart=
"true"
/>
<moveFromstart=
"true"
/>
<moveTo start=
"true"
/>
<attrib start=
"false"
/>
<modify start=
"false"
/>
</inotify>
<sersync>
<localpathwatch=
"/data/www"
>
<remoteip=
"172.16.1.25"
name=
"www"
/>
<remoteip=
"172.16.1.26"
name=
"www"
/>
</localpath>
<!--################################################## -->
<localpathwatch=
"/data/bbs"
>
<remoteip=
"172.16.1.25"
name=
"bbs"
/>
<remoteip=
"172.16.1.26"
name=
"bbs"
/>
</localpath>
<!--################################################## -->
<localpathwatch=
"/data/blog"
>
<remoteip=
"172.16.1.25"
name=
"blog"
/>
<remoteip=
"172.16.1.26"
name=
"blog"
/>
</localpath>
<!-- ##################################################-->
<rsync>
<commonParamsparams=
"-artuz"
/>
<auth start=
"true"
users=
"rsync_backup"
passwordfile=
"/etc/rsync.password"
/>
<userDefinedPortstart=
"false"
port=
"874"
/><!-- port=
874
-->
<timeout start=
"true"
time=
"100"
/><!-- timeout=
100
-->
<sshstart=
"false"
/>
</rsync>
<failLogpath=
"/usr/local/sersync/logs/rsync_fail_log.sh"
timeToExecute=
"60"
/><!--
default
every 60mins execute once-->
<crontab start=
"false"
schedule=
"600"
><!--600mins-->
<crontabfilterstart=
"false"
>
<excludeexpression=
"*.php"
></exclude>
<excludeexpression=
"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"
/>
<includeexpression=
"(.*)\.sh"
/>
</filter>
</plugin>
<plugin name=
"socket"
>
<localpathwatch=
"/opt/tongbu"
>
<deshostip=
"192.168.138.20"
port=
"8009"
/>
</localpath>
</plugin>
<plugin name=
"refreshCDN"
>
<localpathwatch=
"/data0/htdocs/cms.xoyo.com/site/"
>
<cdninfodomainname=
"ccms.chinacache.com"
port=
"80"
username=
"xxxx"
passwd=
"xxxx"
/>
<sendurlbase=
"http://pic.xoyo.com/cms"
/>
<regexurlregex=
"false"
match=
"cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"
/>
</localpath>
</plugin>
</head>
|
1
2
3
4
5
|
[root@web ~]
# /usr/local/sersync/sersync2 -d -r -o /usr/local/sersync/confxml.xml
配置sersync环境变量
[root@web ~]
# echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
[root@web ~]
# source /etc/profile
[root@web ~]
# sersync2
|
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
|
set
the system param
execute:echo50000000 >
/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 remoteservers 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 runbehind the console
use
rsync
password-
file
:
user is rsync_backup
passwordfile is
/etc/rsync
.password
config xml parse success
please
set
/etc/rsyncd
.confmax connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) +20(Sub threads)
please according your cpu ,use -n paramto adjust the cpu rate
chmod
: cannot access`
/usr/local/sersync/logs/rsync_fail_log
.sh': No such
file
or directory
------------------------------------------
rsync
the directory recursivlyto the remote servers once
working please wait...
execute
command
:
cd
/data/www
&&
rsync
-artuz -R --delete ./ --timeout=100 [email protected]::www--password-
file
=
/etc/rsync
.password >
/dev/null
2>&1
run the sersync:
watch
path is:
/data/www
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/usr/local/sersync/sersync2
-d -o
/usr/local/sersync/www_confxml
.xml
/usr/local/sersync/sersync2
-d -o
/usr/local/sersync/bbs_confxml
.xml
/usr/local/sersync/sersync2
-d -o
/usr/local/sersync/blog_confxml
.xml
# 多实例初始化同步命令:
/usr/local/sersync/sersync2
-r-d -o
/usr/local/sersync/www_confxml
.xml
/usr/local/sersync/sersync2
-r-d -o
/usr/local/sersync/bbs_confxml
.xml
/usr/local/sersync/sersync2
-r-d -o
/usr/local/sersync/blog_confxml
.xml
/bin/cp
/etc/rc
.
local
/etc/rc
.local_$(data +%F)
cat
>>
/etc/rc
.
local
<<EOF
#sync data to 25 26
/usr/local/sersync/sersync2
-d -o
/usr/local/sersync/www_confxml
.xml
/usr/local/sersync/sersync2
-d -o
/usr/local/sersync/bbs_confxml
.xml
/usr/local/sersync/sersync2
-d -o
/usr/local/sersync/blog_confxml
.xml
EOF
|
1
2
3
4
5
6
7
8
9
10
|
# for n in `seq 10000`;do echodddd>www/$n.txt;done
# ps -ef |greprsync
root 17283 1 0 Jun05 ? 00:00:02
/usr/local/sersync/sersync2
-d-r -o
/usr/local/sersync/confxml
.xml
root 19363 1 0 Jun05 ? 00:00:01
/usr/local/sersync/bin
-d -o
/usr/local/sersync/confxml
.xml
root 19394 1 0 Jun05 ? 00:00:01
/usr/local/sersync/bin
-r -d-o
/usr/local/sersync/confxml
.xml
root 19414 1 0 Jun05 ? 00:00:01
/usr/local/sersync/bin
-r -d-o
/usr/local/sersync/confxml
.xml
root 29484 17283 0 01:33 ? 00:00:00 sh -c
cd
/data/www
&&
rsync
-artuz -R --timeout=100
"./395.txt"
[email protected]::www--password-
file
=
/etc/rsync
.password >
/dev/null
2>&1
root 29487 29484 0 01:33 ? 00:00:00
rsync
-artuz -R --timeout=100.
/395
.txt [email protected]::www --password-
file
=
/etc/rsync
.password
root 29490 17283 0 01:33 ? 00:00:00 sh -c
cd
/data/www
&&
rsync
-artuz -R --timeout=100
"./396.txt"
[email protected]::www--password-
file
=
/etc/rsync
.password >
/dev/null
2>&1
提示:我们发现本地已经写完了10000个,但是同步的线程,依然在同步;甚至才同步了1000多个
|
WEB1 同步查看对比 | WEB2 同步查看对比 |
27036 /data/www/ | 26960 /data/www/ |
每秒同步20--30个文件 | 每秒同步20-30个文件 |
Sersync参数 | 说明 |
./sersync -r | 提别说明:如果设置了过滤器,即在xml文件中,filter为true,则暂时不能使用-r参数进行整体同步; |
./sersync -o xx.xml | 指定 -o 参数:可以指定多个不同的配置文件,从而实现sersync多进程多实例的数据同步 |
./sersync -n num | 例如: ./sersync -n 5 则指定线程总数为5,如果不指定,默认启动线程池数量是10,如果cpu使用过高,可以通过该参数调低,如果机器配置较高,可以调高默认的线程总数,提升同步效率; |
./sersync -d | -d参数为:后台服务,通常情况下使用 -r参数对本地到远端整体同步一遍后,在后台运行此参数启动守护进程实时同步;在第一次整体同步时,-d 和 -r参数经常会联合使用; |
pluginName | 例如:./sersync -m command,则在监控到事件后,不对远程目标服务器进行同步,而是直接运行command插件 |
组合命令使用说明: | |
-n 8 -o liubl.xml -r -d | 多个参数可以配合使用,例如:./sersync -n 16 -o config.xml -r -d 表示设置线程池工作线程为16个,指定liubl.xml作为配置文件,在实时监控前 做一次整体同步,以守护进程方式在后台运行; |
./sersync --help | 很遗憾,它没有查看帮助(需要的话2条路,要么看源代码,要么自测求验证) |
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>
|
1
|
3 <host hostip=
"localhost"
port=
"8008"
><
/host
>
|
1
|
4 <debug start=
"false"
/>
|
1
|
5 <fileSystem xfs=
"false"
/>
|
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
>
|
=
=
=