您的位置:首页 > 其它

rsync+inotify实时同步文件

2015-05-05 10:37 127 查看
一、inotify简介

inotify是Linux内核2.6.13 (June 18, 2005)版本新增的一个子系统(API),它提供了一种监控文件系统(基于inode的)事件的机制,可以监控文件系统的变化如文件修改、新增、删除等,并可以将相应的事件通知给应用程序。该机制由著名的桌面搜索引擎项目beagle引入用于替代此前具有类似功能但存在诸多缺陷的dnotify。

inotify既可以监控文件,也可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的。此外,inotify 使用文件描述符作为接口,因而可以使用通常的文件I/O操作select、poll和epoll来监视文件系统的变化。

inotify 可以监视的文件系统常见事件包括:
IN_ACCESS:文件被访问
IN_MODIFY:文件被修改
IN_ATTRIB,文件属性被修改
IN_CLOSE_WRITE,以可写方式打开的文件被关闭
IN_CLOSE_NOWRITE,以不可写方式打开的文件被关闭
IN_OPEN,文件被打开
IN_MOVED_FROM,文件被移出监控的目录
IN_MOVED_TO,文件被移入监控着的目录
IN_CREATE,在监控的目录中新建文件或子目录
IN_DELETE,文件或目录被删除
IN_DELETE_SELF,自删除,即一个可执行文件在执行时删除自己
IN_MOVE_SELF,自移动,即一个可执行文件在执行时移动自己

通过/proc接口中的如下参数设定inotify能够使用的内存大小:
1、/proc/sys/fs/inotify/max_queue_events
应用程序调用inotify时需要初始化inotify实例,并时会为其设定一个事件队列,此文件中的值则是用于设定此队列长度的上限;超出此上限的事件将会被丢弃;
2、/proc/sys/fs/inotify/max_user_instances
此文件中的数值用于设定每个用户ID(以ID标识的用户)可以创建的inotify实例数目的上限;
3、/proc/sys/fs/inotify/max_user_watches
此文件中的数值用于设定每个用户ID可以监控的文件或目录数目上限;

二、环境
rsync-3:192.168.3.54

rsync-4:192.168.3.55

需求:在rsync-3上的/data/web目录文件有变化,自动同步到rsync-4的/data/web目录下

三、在rsync-4上配置rsync服务,系统一般会默认安装。
我们使用xinetd来管理rsync服务,所有需要安装xinetd。

[root@rsync-4 ~]# yum install xinetd -y
配置/etc/xinetd.d/rsync文件
[root@rsync-4 ~]# vim /etc/xinetd.d/rsync
service rsync
{
disable = no                #将yes修改成no
flags           = IPv6
socket_type     = stream
wait            = no
user            = root
server          = /usr/bin/rsync
server_args     = --daemon
log_on_failure  += USERID
}
创建rsync配置文件

[root@rsync-4 ~]# vim /etc/rsyncd.conf
# GLOBAL OPTIONS
uid = root
gid = root
use chroot = yes              #这里要使用yes,否则同步过来的软连接路径会多一个前缀,导致软连接不能正常使用
read only = no        #我们要通过inotify自动同步文件过来,这个需要有写入权限,或者在模块中添加写入权限
#limit access to private LANs
hosts allow=192.168.3.0/255.255.0.0
hosts deny=*
max connections = 5

pid file = /var/run/rsyncd.pid
secrets file = /etc/rsync.passwd
#lock file = /var/run/rsync.lock
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

# MODULE OPTIONS
[web]
path = /data/web
list=yes
ignore errors
auth users = rsync
comment = welcome to rsync server
创建rsync的认证文件/etc/rsync.passwd
[root@rsync-4 ~]# echo "rsync:test" >>/etc/rsync.passwd        #添加用户名和密码
[root@rsync-4 ~]# chmod 600 /etc/rsync.passwd                  #修改成600的权限
在iptables中放行873端口,并保存iptables规则
[root@rsync-4 ~]# iptables -I INPUT -p tcp --dport 873 -j ACCEPT
[root@rsync-4 ~]# iptables -I INPUT -p udp--dport 873 -j ACCEPT
[root@rsync-4 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]
启动xinetd服务

[root@rsync-4 ~]# service xinetd start
Starting xinetd:                                           [  OK  ]
[root@rsync-4 ~]# netstat -anpt |grep 873
tcp        0      0 :::873                      :::*                        LISTEN      1792/xinetd
上创建共享数据
[root@rsync-4 ~]# mkdir -p /data/web/www
[root@rsync-4 ~]# touch  /data/web/www/web.html


四rsync-3上的操作
创建客户端密码认证文件

[root@rsync-3 ~]# echo "test" >>/usr/local/sbin/rsync.passwd
[root@rsync-3 ~]# chmod 600 /usr/local/sbin/rsync.passwd
在rsync-3上查看有哪些共享

[root@rsync-3 ~]# rsync --list-only root@192.168.3.55::
web            	welcome to rsync server
同步数据

[root@rsync-4 ~]# rsync -avzP --delete --password-file=/usr/local/sbin/rsync.passwd rsync@192.168.3.55::web/ /root/web

receiving incremental file list
created directory /root/web
./
www/
www/web.html
0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/3)

sent 80 bytes  received 176 bytes  512.00 bytes/sec
total size is 0  speedup is 0.00
[root@rsync-4 ~]# ll
total 60
-rw-------. 1 root root  2790 May  5 10:21 anaconda-ks.cfg
-rw-r--r--. 1 root root  3305 May  5 10:21 cobbler.ks
-rw-r--r--. 1 root root 20504 May  5 10:21 install.log
-rw-r--r--. 1 root root  5882 May  5 10:20 install.log.syslog
-rw-r--r--. 1 root root  2241 May  5 10:21 ks-post.log
-rw-r--r--. 1 root root     1 May  5 10:21 ks-post-nochroot.log
-rw-r--r--. 1 root root   978 May  5 10:18 ks-pre.log
drwxr-xr-x  3 root root  4096 May  5 11:15 web
[root@rsync-4 ~]# tree web/
web/
└── www
└── web.html

1 directory, 1 file


到此我们的基本rsync服务已配置完成。接下来配置inotify。
因为我们是要监控rsync-3上的/data/web目录的文件变化,所有要将inotify部署在rsync-3上

五、安装inotify-tools
[root@rsync-3 ~]# tar xf inotify-tools-3.14.tar.gz
[root@rsync-3 ~]# cd inotify-tools-3.14
[root@rsync-3 inotify-tools-3.14]# ./configure
[root@rsync-3 inotify-tools-3.14]# make && make install
创建要同步的目录
[root@rsync-3 ~]# mkdir -p /data/web
[root@rsync-3 ~]# touch /data/web/docker.txt
[root@rsync-3 ~]# mkdir /data/web/kvm
[root@rsync-3 ~]# tree /data/web/
/data/web/
├── docker.txt
└── kvm

1 directory, 1 file
创建rsync脚本

[root@rsync-3 ~]# vim /usr/local/sbin/rsync.sh
#!/bin/bash
SRC=/data/web
DEST=web
HOST=192.168.3.55
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $SRC | while read files;
do
rsync -vzrtopg --delete --progress --password-file=/usr/local/sbin/rsync.passwd $SRC/ rsync@$HOST::$DEST &>dev/null
done

#inotifywait参数详解:
#-m,表示始终保持事件监听状态
#-r,表示递归查询目录
#-q,表示打印出监控事件
#-e,指定要监控的事件,包括modify、delete、create、attrib等
#--timefmt:指定时间的输出格式
#--format:指定变化文件的详细信息
#说明$SRC/,表示同步的是目录下面的文件,并不包含$SRC本身
赋予脚本执行权限

[root@rsync-3 ~]# cd /usr/local/sbin/
[root@rsync-3 sbin]# chmod +x rsync.sh
先看看/data/web目录下的文件

[root@rsync-3 sbin]# ll /data/web/
total 4
-rw-r--r-- 1 root root    0 May  5 11:46 3.txt
drwxr-xr-x 2 root root 4096 May  5 11:37 kvm
在/data/web目录下创建一个docket.txt,删除3.txt

#先运行脚本rsync.sh
[root@rsync-3 sbin]# sh -x rsync.sh

#创建文件docker,和删除文件
[root@rsync-3 web]# touch docker.txt
[root@rsync-3 web]# rm -rf 3.txt

#查看脚本执行过程
[root@rsync-3 sbin]# sh -x rsync.sh
+ SRC=/data/web
+ DEST=web
+ HOST=192.168.3.55
+ read files
+ /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib /data/web
+ rsync -vzrtopg --delete --progress --password-file=/usr/local/sbin/rsync.passwd /data/web/ rsync@192.168.3.55::web

sending incremental file list
./
docker.txt
0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=1/4)

sent 122 bytes  received 31 bytes  102.00 bytes/sec
total size is 0  speedup is 0.00
+ read files
+ rsync -vzrtopg --delete --progress --password-file=/usr/local/sbin/rsync.passwd /data/web/ rsync@192.168.3.55::web

sending incremental file list

sent 83 bytes  received 9 bytes  184.00 bytes/sec
total size is 0  speedup is 0.00
+ read files
+ rsync -vzrtopg --delete --progress --password-file=/usr/local/sbin/rsync.passwd /data/web/ rsync@192.168.3.55::web

sending incremental file list
./
deleting 3.txt

sent 72 bytes  received 12 bytes  56.00 bytes/sec
total size is 0  speedup is 0.00
+ read files

#rsync-3上的/data/web目录现在结果
[root@rsync-3 web]# ll /data/web/
total 4
-rw-r--r-- 1 root root    0 May  5 11:51 docker.txt
drwxr-xr-x 2 root root 4096 May  5 11:37 kvm


在目标机器rsync-4上查看/data/web目录

[root@rsync-4 ~]# ll /data/web/
total 4
-rw-r--r-- 1 root root    0 May  5 11:51 docker.txt
drwxr-xr-x 2 root root 4096 May  5 11:37 kvm


通过以上步骤rsync就能实时同步文件了

最后将脚本加入到开机自动启动去
[root@rsync-3 sbin]# echo "/usr/local/sbin/rsync.sh  &"  >>/etc/rc.local


本文出自 “ly36843运维” 博客,请务必保留此出处http://ly36843.blog.51cto.com/3120113/1641980
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: