需求:
线上也业务需求,意思就是N台机器需要维护各自机器上的一个目录,并且他们都是相同滴。so,这两天想了下,决定使用rsync server+inotify来解决这个问题。当然有的人会用自动化工具来推,但是我这里这个文件可能每次只改里面1小点内容,推个一百来兆的东西(这句话有错也莫喷了亲 = =)。。反正出于种种原因决定要使用 rsync
作业环境:
CentOS 6.3
rsync version 3.0.6 (为系统自带版本 = = 当然编译的下面会说)
inotify-tools 3.14
-------------------
首先说明:
其实rsync和inotify-tools使用yum或者系统自带的都可以的。
inotify需要注意内核是否支持:(ls /proc/sys/fs/inotify 下是否有max_queued_events max_user_instances max_user_watches三个文件)
rsync的编译安装也不过多介绍了,tar xf *** && cd *** && ./configure && make && make install
-------------------
场景说明:
我这里是1台server(rsync+inotify)来主动推送文件到N台机器(rsync守护进程),并且我N台机器配置文件配置都一样(喊密码认证文件)。
-------------------
server端配置:
|
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
|
mkdir
/data/sync-server
cd
/data/sync-server
yum -y
install
inotify-tools
rsync
#然后编写inotify-tools脚本
vim
sync
.sh
#!/bin/bash
#
source
/etc/profile
&>
/dev/null
basedir=$(
cd
`
dirname
$0`;
pwd
)
log=
"$basedir/sync.log"
synclog=
"$basedir/result.log"
file
=
"$basedir/iplist"
/usr/bin/inotifywait
-mrq --timefmt
'%d/%m/%y %H:%M'
--
format
'%T %w%f'
-e modify,delete,create,attrib,move
/data/karaf-sync/ccms-build/
\
|
while
read
file
do
while
read
line
do
echo
"$line sync start..."
/usr/bin/rsync
-vzrtopg --delete --progress
/data/karaf-sync/ccms-build/
ccms@$line::ccms --password-
file
=
/data/karaf-sync/scripts/pwd
&>> $log
if
[ $? -
eq
0 ];
then
echo
"`date +%F-%H:%M` $line was rsynced Success !!!"
&>> $synclog
echo
"-----------------------------------------------------------"
&>> $synclog
echo
""
&>> $synclog
else
echo
"`date +%F-%H:%M` $line was rsynced Failed !!!"
&>> $synclog
echo
"-----------------------------------------------------------"
&>> $synclog
echo
""
&>> $synclog
fi
done
< $basedir
/iplist
#我外面有相关的定时同步iplist的操作
done
|
|
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
|
inotifywait:
-r, --recursive递归查询目录。
-q, --quiet,打印出监控事件。
-e, --event,指定要监控的事件,常见的事件有move、modify、delete、create、attrib等。全量:(access、modify、 attrib、 close_write、 close_nowrite、close、
open
、 moved_to、 moved_from、move、 move_self、 create、delete、delete_self、unmount)
--timefmt:时间的输出格式
--
format
:指定变化文件的详细信息
------------------------------------------------------
inotify 可以监视的文件系统事件包括:
IN_ACCESS,即文件被访问
IN_MODIFY,文件被 write
IN_ATTRIB,文件属性被修改,如
chmod
、
chown
、
touch
等
IN_CLOSE_WRITE,可写文件被 close
IN_CLOSE_NOWRITE,不可写文件被 close
IN_OPEN,文件被
open
IN_MOVED_FROM,文件被移走,如
mv
IN_MOVED_TO,文件被移来,如
mv
、
cp
IN_CREATE,创建新文件
IN_DELETE,文件被删除,如
rm
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己
IN_UNMOUNT,宿主文件系统被
umount
IN_CLOSE,文件被关闭,等同于(IN_CLOSE_WRITE | IN_CLOSE_NOWRITE)
IN_MOVE,文件被移动,等同于(IN_MOVED_FROM | IN_MOVED_TO)
注:上面所说的文件也包括目录。
rsync
的命令请另外搜索。。
|
client端配置:
|
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
|
vim
/etc/rsyncd
.conf
uid = root
#运行RSYNC守护进程的用户
gid = root
#运行RSYNC守护进程的组
port = 873
#监听端口
use chroot = no
#不使用chroot
max connections = 100
#最大连接数,0为不限制
timeout = 600
#通过该选项可以覆盖客户指定的IP超时时间.通过该选项可以确保rsync服务器不会永远等待一个崩溃的客户端.超时单位为秒钟,0表示没有超时定义,这也是默认值.对于匿名rsync服务器来说,一个理想的数字是600.
pid
file
=
/var/run/rsyncd
.pid
lock
file
=
/var/run/rsyncd
.lock
log
file
=
/var/log/rsyncd
.log
#log format --- google
[ccms]
path =
/data/ccms-build/
#需要同步的路径
comment = ccms
dir
#名称
ignore errors
#可以忽略一些无关的IO错误
read
only = no
list = no
#不允许列文件
hosts allow = 192.168.***.***
/255
.255.255.0
#允许的客户端
auth
users
= ccms
#认证用户
secrets
file
=
/etc/rsync
.
pwd
#密码文件:600
vim
/etc/rsync
.
pwd
ccms:********
#同server端相同
chmod
600
/etc/rsync
.
pwd
|
启动进程
|
1
2
|
/usr/bin/rsync
--daemon
echo
"/usr/bin/rsync --daemon"
>>
/etc/init
.d
/rc
.
local
|
到这里就差不多了,我这里使用的是screen 启动之前的server的脚本,然后就可以两边写文件进行测试了。
另外一些命令的具体用法 ,可以搜一下,作为参考,这里就不再详细说明了,抱歉!