linux下rsync+inotify同步文件

来源:互联网 发布:php 生成pdf文件 编辑:程序博客网 时间:2024/05/28 18:45

inotify进行对服务器文件监听,一旦检测到有改动则发起同步rsync服务器文件到客户端

一、安装和下载rsync

下载地址:http://rsync.samba.org/

1、安装步骤:

  1)# tar zxvf rsync-3.0.6.tar.gz
  2)# cd rsync-3.0.6
  3)# ./configure  
  4)# make 
  5)# make install

2、配置服务(rsync主服务器ip地址:192.168.13.132 )

2.1先配置rsync服务器192.168.13.132

在/etc/目录下新建rsyncd.conf文件,其中配置信息如下:

uid = rootgid = rootuse chroot = nomax connections = 4pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.log[backup]path = /data/test #同步的目录地址ignore errorsread only = nolist = noauth users = rsync #同步该模块时用户名  需要与rsync.pass文件下的用户名保持一致即可secrets file = /etc/rsync.pass

2.2 在/etc/目录下新建rsync.pass文件

 注意:如果当前服务器做主服务器(一旦改变文件则需要向其他服务器发送同步文件是),则rsync.pass文件中内容为 123456,如果文件改变,由其他服务器连接当前服务器进行下载文件同步时,rsync.pass文件中内容为rsync:123456

更改rsync.pass的文件权限

#chmod 600 /opt/app/rsync/ect/rsyncucweb.password   (如果不是600,启动服务时候会提示报错)

2.3启动192.168.13.132(主)服务器的rsync服务

rsync --daemon --config=/etc/rsync.conf

编辑/etc/rc.d/rc.local,在最后添加,设置开机启动

rsync --daemon --config=/etc/rsync.conf

查看是否启动成功

[root@localhost /]# cd /
[root@localhost /]# lsof -i:873
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
rsync   56860 root    4u  IPv4 134802      0t0  TCP *:rsync (LISTEN)
rsync   56860 root    5u  IPv6 134803      0t0  TCP *:rsync (LISTEN)


3、配置服务(rsync备份服务器ip地址:192.168.13.131 )

3.1配置备份服务器配置文件

  rsync.conf的配置文件和主服务器配置一致,只是rsync.pass文件内容为rsync:123456

3.2启动192.168.13.131(备份)服务器的rsync服务

rsync --daemon --config=/etc/rsync.conf

编辑/etc/rc.d/rc.local,在最后添加,设置开机启动

rsync --daemon --config=/etc/rsync.conf

待两台rsync服务都启动,可以先测试一波是否能够达到同步

[root@localhost /]#rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pass  /data/test/  rsync@192.168.13.131::backup

以上命令是服务器端跟新后同步到客服端(将主服务器的/data/test目录下的文件同步到192.168.13.131的backup模块中配置的path下

[root@localhost srv]# rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pass  /data/test/  rsync@192.168.13.131::backup
sending incremental file list
./
aaa
           9 100%    0.00kB/s    0:00:00 (xfer#1, to-check=6/8)
ccc
           8 100%    7.81kB/s    0:00:00 (xfer#2, to-check=5/8)
ddddd
           4 100%    3.91kB/s    0:00:00 (xfer#3, to-check=4/8)
dsaddsad
           4 100%    3.91kB/s    0:00:00 (xfer#4, to-check=3/8)
fds
           5 100%    4.88kB/s    0:00:00 (xfer#5, to-check=2/8)
fdsf
           4 100%    3.91kB/s    0:00:00 (xfer#6, to-check=1/8)
nnn
           5 100%    4.88kB/s    0:00:00 (xfer#7, to-check=0/8)


sent 447 bytes  received 144 bytes  394.00 bytes/sec
total size is 39  speedup is 0.07
[root@localhost srv]# 

出现以上信息则代表同步成功

二、安装和下载inotify

用inotify-tool来检测主服务器的文件目录,如果有改动则直接触发同步服务器和客户端的文件,该inoty

1、下载并且安装

# wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
# tar xzvf inotify-tools-3.14.tar.gz
# cd inotify-tools-3.13
# ./configure  --prefix=/usr/local/inotify
# make
# make install

2、服务端编写文件夹监视脚本

#vi /srv/rsync.sh  脚本内容如下

#!/bin/bashsrc=/data/test/   #监视改文件路径,文件夹内容发生改变触发服务器数据同步des=backup       #客服端配置的模块(客服端会介绍)host="192.168.13.131"  #同步到的IP地址,如有多个用空格隔开。例如:host="192.168.10.6 192.168.10.7"/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e  modify,delete,create,attrib $src | while read files #这边要主要下inotifywait,只有Linux 2.6.13 或更高版的才会兼容do  for hostip in $host do  #rsync -vzrtopg --delete --progress rsync@$hostip::$des $src --password-file=/etc/rsync.pass 该命令是客服端跟新后同步到服务器端  rsync -vzrtopg --delete --progress --password-file=/etc/rsync.pass $src rsync@$hostip::$des #改命令是服务器端跟新后同步到客服端(看具体使用情况选择)#--password-file=/etc/rsync.pass 配置到客服端,也就是客户端路径下有rsync.pas 用于配置登陆密码#rsync 是客户端配置的登陆名  done  echo "${files} was rsynced" >>/tmp/rsync.log 2>&1  #生成日志文件 done

3、启动nohup

 #nohup /bin/bash /srv/rsync.sh &  //后台不挂断地运行命令
 #echo "nohup /bin/bash /root/bin/rsync.sh &" >> /etc/rc.local //设置linux服务器启动自动启动nohup










0 0
原创粉丝点击