inotify-tools+rsync实时同步文件安装和配置

服务器A:论坛的主服务器,运行DZ X2论坛程序;服务器B:论坛从服务器,需要把X2的图片附件和MySQL数据实时从A主服务器实时同步到B服务器.MySQL同步设置会在下一编中说到.以下是用于实时同步两台服务器的图片.

因为一般的RSYNC需要CRON来定期运行SH脚本来实现同步,这样会带来一些问题.比如用户从主服务器上传上一个图片,需要最少一分钟才能从从服务器显示出来.自从Linux 2.6内核后,支持了inotify机制,当某些文件或文件夹有改变时,发出相应的事件,这样,第三方程序只要订阅这些事件,就可以处理相应的操作了.这时,只要有文件被修改,就执行一次RSNYN,把修改的文件主动地上传到另一台服务器上就可以了.

我使用的是google的inotify-tools,比较简单.国内有功能很强大的类似的程序,但是好复杂.另外需要注意的是:如果使用inotify-tools来实现实时同步,我们的主服务器--源文件服务器(也就是服务器A)实现是RSYNC的从服务器,我们的从服务器--目标同步的服务器(服务器B)才是RSYNC的主服务器.不要搞混了哦.

好了,开始吧!

首先从主服务器A开始,

需要确定你的系统是否支持inotify:


1

2

3

4

5

ll /proc/sys/fs/inotify

total 0

-rw-r--r-- 1 root root 0 Jan 4 17:56 max_queued_events

-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_instances

-rw-r--r-- 1 root root 0 Jan 4 17:56 max_user_watches

能输出这样的结果表示支持.

下载并安装inotify-tools:


1

2

3

4

5

6

wget --no-check-certificate http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

tar xzvf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

./configure --prefix=/usr

make

make install

这样就完成了inotify-tools的当然.

接下来需要写两个SH脚本,inotify_init.sh和inotify_monitor.sh:

inotify_init.sh 用于第一次初始化,也就是运行一次完整的RSYNC同步.


1

vi /root/inotify_init.sh

内容如下:


1

2

3

4

5

6

7

8

9

10

11

#!/bin/sh

SRC=/主服务器A需要同步的目录/ #记得在最后面加/不然RYNC会自动增加一层目录

 

DES=bbsatt

IP=从服务器B的IP

USER=rsync

#DST=/etc/rsyncd 远程rsync模块下的目录

INWT=/usr/bin/inotifywait

RSYNC=/usr/bin/rsync

 

$RSYNC -zahqt --password-file=/root/rsync.pwd $SRC [email protected]$IP::$DES

保存退出.

设置可执行权限:


1

chmod +x /root/inotify_init.sh

接下是inotify_monitor.sh,用于订阅文件修改事件.注意,因为特别原因,我在这里做的是增量备份+实时同步,也就是说,当主服务器A上的图片被删除是,从服务器B是不会删除图片的.


1

vi /root/inotify_monitor.sh


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

#!/bin/bash

 

###########################

sync[0]=‘/主服务器需要同步的目录/,从服务器B的IP,bbsatt,rsync‘ # localdir,host,rsync_module,auth_user

 

INWT=/usr/bin/inotifywait

RSYNC=/usr/bin/rsync

PASS=/root/rsync.pwd

###########################

 

for item in ${sync[@]}; do

 

dir=`echo $item | awk -F"," ‘{print $1}‘`

host=`echo $item | awk -F"," ‘{print $2}‘`

module=`echo $item | awk -F"," ‘{print $3}‘`

user=`echo $item | awk -F"," ‘{print $4}‘`

 

$INWT -mrq --timefmt ‘%d/%m/%y %H:%M‘ --format ‘%T %w%f %e‘

--event CLOSE_WRITE,create,move $dir while read date time file event

do

#echo $event‘-‘$file

case $event in

MODIFY|CREATE|MOVE|MODIFY,ISDIR|CREATE,ISDIR|MODIFY,ISDIR)

if "${file: -4}" != ‘4913‘ ] && [ "${file: -1}" != ‘~‘ ]; then

cmd="$RSYNC -zahqzt --exclude=‘*‘ --password-file=$PASS

--include=$file $dir [email protected]$host::$module > /dev/null 2>1&"

echo $cmd

$cmd

fi

;;

 

MOVED_FROM|MOVED_FROM,ISDIR|DELETE,ISDIR)

if "${file: -4}" != ‘4913‘ ] && [ "${file: -1}" != ‘~‘ ]; then

cmd="$RSYNC -zahqzt --password-file=$PASS --exclude=$file

$dir [email protected]$host::$module > /dev/null 2>1&"

echo $cmd

$cmd

fi

;;

esac

done &

done


1

chmod +x /root/inotify_monitor.sh

设置RSYNC自动登录验证密码


1

2

vi /root/rsync.pwd

xxxxxx

保存,退出

设置只有ROOT才可以查看的权限.


1

chmod 0600 /root/rsync.pwd

以下是备从务器B的配置:

安装RSYNC


1

yum rsync -y

配置RSNYD服务:


1

vi /etc/rsyncd.conf

内容如下,需要把Apache修改成你运行网站的用户名,我的是因为原来使用apache,虽然现在用Nginx,也一直没改用户名:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

uid = apache

gid = apache

use chroot = no

max connections = 4

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

 

[bbsatt]

path = /从服务器B本地用于存放备份的目录

ignore errors

read only = no

list = false

hosts allow = 主服务器A的IP

auth users = rsync

secrets file = /etc/rsync.pas


1

2

vi /etc/rsync.pas

rsync:xxxxxx


1

chmod 0600 /etc/rsync.pas

启动RSYNCD


1

rsync --daemon

添加开机自动启动服务:


1

vi /etc/rc.local

添加以下内容:


1

rsync --daemon

回到主服务器A,


1

vi /etc/rc.local

添加以下内容,实时开机自动同步:


1

2

/root/inotify_init.sh

/root/inotify_monitor.sh

保存退出

运行

/root/inotify_init.sh

1

/root/inotify_monitor.sh

好了,这样就能实现实时同步图片文件了.随便在主服务器A的同步目录下新建一个文件试试吧.

inotify-tools+rsync实时同步文件安装和配置

时间: 2024-08-01 09:30:12

inotify-tools+rsync实时同步文件安装和配置的相关文章

linux下实现多台服务器同步文件(inotify-tools+rsync实时同步文件安装和配置)

inotify-tools+rsync实时同步文件安装和配置 注:转载https://www.linuxidc.com/Linux/2012-06/63624.htm 原文地址:https://www.cnblogs.com/ccw869476711/p/9007156.html

Inotify+Rsync实时同步文件

一.网络拓补图: 原文地址:http://blog.51cto.com/hbgslz/2064576

rsync实时同步文件

http://rsync.samba.org/download.html [[email protected] src]# yum install git [[email protected] src]# git clone git://git.samba.org/rsync.git [[email protected] src]# git clone git://git.samba.org/rsync.git Initialized empty Git repository in /usr/l

rsync+inotify安装配置 实时同步文件

安装 #安装inotify 工具 [root@localhost ~]# yum install inotify-tools -y 常用命令 [[email protected] ~]# inotifywait -rm /data/ \\实时监控/data的所有事件(包括文件的访问.写入.修改.删除等) [[email protected] ~]# inotifywait -mrq --timefmt '%Y/%m/%d-%H:%M:%S' --format '%T %w %f' > -e mo

rsync+inotify实时同步文件

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

利用rsync+inotify实现数据实时同步脚本文件

将代码放在Server端,实现其它web服务器同步.首先创建rsync.shell,rsync.shell代码如下: #!/bin/bash host1=133.96.7.100 host2=133.96.7.101 host3=133.96.7.102 src=/data/www/ dst1=web1 dst2=web2 dst3=web3 user1=web1 user2=web2 user3=web3 /usr/local/inotify/bin/inotifywait -mrq --ti

Inotify+rsync实时同步工具实战

Inotify+rsync实时同步工具实战 分别有机器:server-178/24,client-b-179/24,client-c-180/24 中心分发服务器Master:client-c-180/24 备份服务器    :client-b-179/24和server-178/24 基于备份服务器已经提供rsync --daemon的基础上,在中心分发服务器(rsync客户端)配置inotify,监控的目录设置为/www/ 1.查看当前系统是否支持inotify ls -l /proc/sy

配置inotify+rsync实时同步

对rsync服务及命令不熟悉的,可以参考博文通过rsync实现远程同步这里就不多说了! Linux内核从2.6.13版本开始就已经提供了inotify通知接口,用来监控文件系统的各种变化情况,如文件存取.删除.移动.修改等.利用这一机制,可以非常方便的实现文件异动告警.增量备份,并针对目录或文件的变化及时作出响应. 将inotify机制与rsync工具相结合,可以实现出发时备份(实时同步)--只要原始位置的文档发生变化,则立即启动增量备份,否则处于静默等待状态,如图所示:这样,就避免了按固定周期

sersync基于rsync+inotify实现数据实时同步

一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192.168.1.243 实时同步/var/atlassian目录到从服务器. 二.实施 1.从服务器192.168.1.243 rsync服务搭建 1.1安装软件包 wget http://rsync.samba.org/ftp/rsync/src/rsync-3.1.1.tar.gz tar xf r