设为首页 收藏本站
查看: 452|回复: 0

[经验分享] rsync+inotify实时同步文件

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-5-6 09:10:01 | 显示全部楼层 |阅读模式
一、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。

1
[iyunv@rsync-4 ~]# yum install xinetd -y



    配置/etc/xinetd.d/rsync文件
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@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配置文件

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
[iyunv@rsync-4 ~]# vim /etc/rsyncd.conf
# GLOBAL OPTIONS
uid = root
gid = root
use chroot = no  
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
1
2
[iyunv@rsync-4 ~]# echo "rsync:test" >>/etc/rsync.passwd        #添加用户名和密码
[iyunv@rsync-4 ~]# chmod 600 /etc/rsync.passwd                  #修改成600的权限



    在iptables中放行873端口,并保存iptables规则
1
2
3
4
[iyunv@rsync-4 ~]# iptables -I INPUT -p tcp --dport 873 -j ACCEPT
[iyunv@rsync-4 ~]# iptables -I INPUT -p udp--dport 873 -j ACCEPT
[iyunv@rsync-4 ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  OK  ]



    启动xinetd服务

1
2
3
4
[iyunv@rsync-4 ~]# service xinetd start
Starting xinetd:                                           [  OK  ]
[iyunv@rsync-4 ~]# netstat -anpt |grep 873
tcp        0      0 :::873                      :::*                        LISTEN      1792/xinetd



    上创建共享数据   
1
2
[iyunv@rsync-4 ~]# mkdir -p /data/web/www
[iyunv@rsync-4 ~]# touch  /data/web/www/web.html




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

1
2
[iyunv@rsync-3 ~]# echo "test" >>/usr/local/sbin/rsync.passwd
[iyunv@rsync-3 ~]# chmod 600 /usr/local/sbin/rsync.passwd



    在rsync-3上查看有哪些共享

1
2
[iyunv@rsync-3 ~]# rsync --list-only root@192.168.3.55::
web             welcome to rsync 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
[iyunv@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
[iyunv@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
[iyunv@rsync-4 ~]# tree web/
web/
└── www
    └── web.html

1 directory, 1 file




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

五、安装inotify-tools
1
2
3
4
[iyunv@rsync-3 ~]# tar xf inotify-tools-3.14.tar.gz
[iyunv@rsync-3 ~]# cd inotify-tools-3.14
[iyunv@rsync-3 inotify-tools-3.14]# ./configure
[iyunv@rsync-3 inotify-tools-3.14]# make && make install



    创建要同步的目录
1
2
3
4
5
6
7
8
9
[iyunv@rsync-3 ~]# mkdir -p /data/web
[iyunv@rsync-3 ~]# touch /data/web/docker.txt
[iyunv@rsync-3 ~]# mkdir /data/web/kvm
[iyunv@rsync-3 ~]# tree /data/web/
/data/web/
├── docker.txt
└── kvm

1 directory, 1 file



    创建rsync脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[iyunv@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本身



    赋予脚本执行权限

1
2
[iyunv@rsync-3 ~]# cd /usr/local/sbin/
[iyunv@rsync-3 sbin]# chmod +x rsync.sh



   先看看/data/web目录下的文件

1
2
3
4
[iyunv@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

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
#先运行脚本rsync.sh
[iyunv@rsync-3 sbin]# sh -x rsync.sh

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

#查看脚本执行过程
[iyunv@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目录现在结果
[iyunv@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目录

1
2
3
4
[iyunv@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就能实时同步文件了

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



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-64069-1-1.html 上篇帖子: CentOS提示 -bash: patch: command not found 解决办法 下篇帖子: linux时间和时区修改
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表