服务器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 $USER@$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 $user@$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 $user@$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实时同步文件安装和配置的更多相关文章

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

    inotify-tools+rsync实时同步文件安装和配置 注:转载https://www.linuxidc.com/Linux/2012-06/63624.htm

  2. CentOS7之Rsync+Inotify架构实现实时同步文件和文件夹

    简介:rsync是用来同步文件和文件夹的,inotify是用来实现监听变动而自动同步的 OS:Centos7.3 服务器端:172.16.13.157 客 户 端  :172.16.13.156 目  ...

  3. rsync实时同步文件

    http://rsync.samba.org/download.html [root@v01 src]# yum install git [root@v01 src]# git clone git:/ ...

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

    安装 #安装inotify 工具 [root@localhost ~]# yum install inotify-tools -y 常用命令 [root@localhost ~]# inotifywa ...

  5. linux设置rsync+inotify实时同步文件

    linux设置rsync+inotify实时同步文件   应用场景: 同步接收方:test01 接收目录:/opt/software/test/a/ 同步发起方:test02 同步目录:/opt/so ...

  6. CentOS 6.5 rsync+inotify实现数据实时同步备份

    CentOS 6.5 rsync+inotify实现数据实时同步备份 rsync    remote sync 远程同步,同步是把数据从缓冲区同步到磁盘上去的.数据在内存缓存区完成之后还没有写入到磁盘 ...

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

    一.环境描述 需求:服务器A与服务器B为主备服务模式,需要保持文件一致性,现采用sersync基于rsync+inotify实现数据实时同步 主服务器A:192.168.1.23 从服务器B:192. ...

  8. Centos7 rsync+inotify两台服务器同步文件(单向)

    注:本篇介绍的是单向同步,即A文件同步到B,但B的文件不同步到A,双向同步的在下一篇文章中. rsync与inotify不再赘述,直接进入实战. 0.背景 两台服务器IP地址分别为: 源服务器:192 ...

  9. 真正的inotify+rsync实时同步 彻底告别同步慢

    真正的inotify+rsync实时同步 彻底告别同步慢       http://www.ttlsa.com/web/let-infotify-rsync-fast/     背景 我们公司在用in ...

随机推荐

  1. Vue组件的三种调用方式

    最近在写fj-service-system的时候,遇到了一些问题.那就是我有些组件,比如Dialog.Message这样的组件,是引入三方组件库,比如element-ui这样的,还是自己实现一个?虽然 ...

  2. HTML实现图片360度循环旋转

    <style> .header{ -webkit-animation:rotateImg 5s linear infinite;<!--修改旋转周期--> border: 1p ...

  3. 运行Tomcat闪退问题,报的错误:Unsupported major.minor version 51.0

    在MyEclipse中运行tomcat,tomcat闪退并且报以下错误. java.lang.UnsupportedClassVersionError: org/apache/catalina/sta ...

  4. RAID5存储上parted进行分期及UUID对应关系

    #parted [设备] [命令 [参数]]命令功能: 新增分区:mkpart [primary|logical|extended] [ext3|vfat] 开始 结束 分区表:print 删除分区: ...

  5. ML及AI资源索引

    原文链接:http://blog.csdn.net/pongba/article/details/2915005 机器学习与人工智能学习资源导引 TopLanguage(https://groups. ...

  6. Python笔记11------一个K-means聚类的小例子

    #导入scipy库,库中已经有实现的kmeans模块,直接使用, #根据六个人的分数分为学霸或者学渣两类 import numpy as np from scipy.cluster.vq import ...

  7. BJOI2012 最多的方案

    BJOI2012 最多的方案 Description ​ 第二关和很出名的斐波那契数列有关,地球上的OIer都知道:F1=1, F2=2, Fi = Fi-1 + Fi-2,每一项都可以称为斐波那契数 ...

  8. CNN卷机网络在自然语言处理问题上的应用

    首先申明本人的英语很搓,看英文非常吃力,只能用这种笨办法来方便下次阅读.有理解错误的地方,请别喷我. 什么是卷积和什么是卷积神经网络就不讲了,自行google.从在自然语言处理的应用开始(SO, HO ...

  9. Mybatis拦截器执行过程解析

    上一篇文章 Mybatis拦截器之数据加密解密 介绍了 Mybatis 拦截器的简单使用,这篇文章将透彻的分析 Mybatis 是怎样发现拦截器以及调用拦截器的 intercept 方法的 小伙伴先按 ...

  10. poj 2377 最大生成树

    #include<stdio.h> #include<stdlib.h> #define N 1100 struct node { int u,v,w; }bian[11000 ...