您的位置:首页 > 其它

rsync+inotify实现无间隔文件同步

2014-08-23 20:47 351 查看

1. 工具简介

1) rsync

rsync能同步更新两处计算机的文件与目录,并适当利用差分编码以减少数据传输。rsync中一项与其他大部分类似程序或协议中所未见的重要特性是镜像对每个目标只需要一次发送。rsync可拷贝/显示目录属性,以及拷贝文件,并可选择性的压缩以及递归拷贝。其默认监听端口为873/tcp,在Centos中,其服务由xinetd代为监听。2) inotyify
inotify是Linux核心子系统之一,做为文件系统的附加功能,它可监控文件系统并将异动通知应用程序。于Linux核心2.6.13发布时,被正式纳入Linux内核。所以内核版本在2.6.13之前的用户,就只能通过给内核打补丁的方式来使用此功能了。

2. 搭建rsync服务

1)rsync命令工作模式

shell模式(本地模式),类似于cp
远程shell模式,可以利用ssh协议承载其远程传输过程
列表模式,仅列出源中的内容 -nv
服务模式,此时,rsync工作为守护进程,能接受客户端同步请求

2)rsync命令选项:

-n:传输测试,不执行真正的同步
-v:详细输出模式
-q:静默模式
-c:开启校验功能
-r:递归复制
注意:当源路径末尾有斜线时复制目录下的内容,没有斜线则复制目录
目标有无斜线不影响复制效果
-a:归档,保留文件原有属性
-p:保留文件权限
-t:保留时间戳
-l:保留符号链接
-g:保留属组
-o:保留属主
-D:保留设备文件

-e ssh:使用ssh作为传输承载;
-z:压缩传输

--progress:显示进度条
--stats:显示如何执行压缩和传输
3) 搭建rsync工作为服务器模式
#由于rsyncd服务依赖于xinetd来代为关系,我们需要先安装xinetd
[root@host1 ~]# yum -y install xinetd
#编辑/etc/xinetd.d/rsync ,将diaable改为no
[root@host1 ~]# vim /etc/xinetd.d/rsync
disable = no
#添加rsync配置文件并编辑
[root@host1 ~]# vim /etc/rsyncd.conf
#=====================
#Gloable Setting
uid = root#设定运行用户
gid = root#设定运行组
use chroot = no#不实用chroot
max connections = 5#设置最大连接数
strict modes = yes#使用密码文件时,是否检查其权限
pid file = /var/run/rsyncd.pid#pid文件位置
log file = /var/log/rsyncd.log#日志文件位置

#Directory Setting
[test]#定义一个新的资源
path = /test#指定资源路径
ignore errors = no#不忽略错误
read only = no#不使用只读模式
hosts allow = 172.16.21.0/24#允许哪些网段登录
hosts deny = *#不允许登录的网段
list = true #是否允许列出文件列表
#===========================
[root@host1 ~]# mkdir /test#创建目录

[root@host1 ~]# touch /test/file{1..9}#创建几个测试文件
[root@host1 ~]# ls /test
file1  file2  file3  file4  file5  file6  file7  file8  file9

[root@host1 ~]# chkconfig --add rsync#设置开机自动启动
[root@host1 ~]# chkconfig --list
..........
xinetd based services:
.......
rsync:         on
.........

[root@host1 ~]# service xinetd restart#重启xinetd以重新读入rsync配置文件
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]

#在host2测试
[root@host2 ~]# rsync -v 172.16.21.101::test/file1 test/#获取文件测试
file1

sent 45 bytes  received 92 bytes  274.00 bytes/sec
total size is 0  speedup is 0.00
[root@host2 ~]# rsync -v /etc/issue 172.16.21.101::test#上传文件测试
issue

sent 168 bytes  received 27 bytes  390.00 bytes/sec
total size is 103  speedup is 0.53

####配置密码认证####

[root@host1 ~]# vim /etc/rsyncd.conf  #在模块[test]下添加认证用户和认证文件
#=================
#Directory Setting
[test]
......
auth users = test1, test2#可登录用户
secrets file = /etc/rsyncd.pw#密码文件,确保只有属主有权限
#==================
[root@host1 ~]# vim /etc/rsyncd.pw
#===============
#文件格式为用户名:密码
test1:test1
test2:test2
test3:test3
#=============
[root@host1 ~]# chmod 600 /etc/rsyncd.pw

#在host2上测试密码访问
[root@host2 ~]# rsync  test1@172.16.21.101::test
Password:
drwxr-xr-x        4096 2014/08/23 13:21:32 .
-rw-r--r--           0 2014/08/23 13:18:34 file1
..........

3. 配置inotify-tools

1) 软件编译及安装

[root@host2 ~]# tar xf inotify-tools-3.14.tar.gz -C /usr/local/src
[root@host2 ~]# cd /usr/local/src/inotify-tools-3.14/
[root@host2 inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify-tools
[root@host2 inotify-tools-3.14]# make && make install
2)附带命令语法
inotifywait
语法:
inotifywait [-hcmrq] [-e ] [-t ] [--format ] [--timefmt ]  file1 [ file2 ] [ ... ]
参数:
-h,–help输出帮助信息
@排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-m, –monitor接收到一个事情而不退出,无限期地执行。默认的行为是接收到一个事情后立即退出。
-d, –daemon跟–monitor一样,除了是在后台运行,需要指定–outfile把事情输出到一个文件。也意味着使用了–syslog。
-o, –outfile 输出事情到一个文件而不是标准输出。
-s, –syslog输出错误信息到系统日志
-r, –recursive监视一个目录下的所有子目录。
-q, –quiet指定一次,不会输出详细信息,指定二次,除了致命错误,不会输出任何信息。
–exclude 正则匹配需要排除的文件,大小写敏感。
–excludei 正则匹配需要排除的文件,忽略大小写。
-t , –timeout 设置超时时间,如果为0,则无限期地执行下去。
-e , –event 指定监视的事件。
-c, –csv输出csv格式。
–timefmt指定时间格式,用于–format选项中的%T格式。
–format 指定输出格式。
%w 表示发生事件的目录
%f 表示发生事件的文件
%e 表示发生的事件
%Xe 事件以“X”分隔
%T 使用由–timefmt定义的时间格式
inotifywatch
语法:
inotifywatch [-hvzrqf] [-e ] [-t ] [-a ] [-d ] [ ... ]
参数:
-h, –help输出帮助信息
-v, –verbose输出详细信息
@排除不需要监视的文件,可以是相对路径,也可以是绝对路径。
–fromfile 从文件读取需要监视的文件或排除的文件,一个文件一行,排除的文件以@开头。
-z, –zero输出表格的行和列,即使元素为空
–exclude 正则匹配需要排除的文件,大小写敏感。
–excludei 正则匹配需要排除的文件,忽略大小写。
-r, –recursive监视一个目录下的所有子目录。
-t , –timeout 设置超时时间
-e , –event 只监听指定的事件。
-a , –ascending 以指定事件升序排列。
-d , –descending 以指定事件降序排列。


3) indentify可监视事件类型

ACCESS,即文件被访问
MODIFY,文件被 write
ATTRIB,文件属性被修改,如 chmod、chown、touch 等
CLOSE_WRITE,可写文件被 close
CLOSE_NOWRITE,不可写文件被 close
OPEN,文件被 open
MOVED_FROM,文件被移走,如 mv
MOVED_TO,文件被移来,如 mv、cp
CREATE,创建新文件
DELETE,文件被删除,如 rm
DELETE_SELF,自删除,即一个可执行文件在执行时删除自己

4) 配置rsync,使其可以自己读入密码

[root@host2 bin]# vim /etc/rsyncd.auth
test1
[root@host2 bin]# chmod 600 /etc/rsyncd.auth
[root@host2 bin]# rsync --password-file=/etc/rsyncd.auth  test1@172.16.21.101::test
drwxr-xr-x        4096 2014/08/23 13:21:32 .
-rw-r--r--           0 2014/08/23 13:18:34 file1
.....

5) 编辑一个配置脚本,以实现向自动向服务器上传变动文件

#!/bin/bash
LocalDir=/test
RemoteDir=test
#Sync Host List
declare -a  host=(
172.16.21.101
)
delete(){

for ((i=0;i<${#host[@]};i++));do

rsync -ar --exclude $1 --delete-excluded --password-file=/etc/rsyncd.auth ./ test1@${host[i]}::$RemoteDir/ &>/dev/null
done;
}

add(){
for ((i=0;i<${#host[@]};i++));do
rsync  -ar --exclude tmp --password-file=/etc/rsyncd.auth $LocalDir/$1 test1@${host[$i]}::$RemoteDir/`dirname $1` &>/dev/null
done;
}

/usr/local/inotify-tools/bin/inotifywait -qmr -e modify,delete,create,attrib  $LocalDir |while read A B C ;do
A=${A#"$LocalDir/"}

case $B in
DELETE,ISDIR|MOVED|DELETE|DELETE_SELF)
delete $A$C
;;
CREATE,ISDIR|MODIFY|CREATE|ATTRIB)
add $A$C
;;
esac
done

通过以上脚本可以判断host2上哪个文件发生改变并上传到host1,以保持文件同步,在host属组中添加IP,每行一个便可以将此服务器的内容同步到多台服务器上.
完成!!!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: