当前位置: 移动技术网 > 网络运营>服务器>Linux > inotify-tools+rsync实时同步文件的配置方法

inotify-tools+rsync实时同步文件的配置方法

2019年05月03日  | 移动技术网网络运营  | 我要评论

服务器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:

复制代码 代码如下:

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:

复制代码 代码如下:

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同步.

vi /root/inotify_init.sh

复制代码 代码如下:

#!/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

保存退出.

设置可执行权限:
chmod +x /root/inotify_init.sh

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

vi /root/inotify_monitor.sh

复制代码 代码如下:

#!/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

chmod +x /root/inotify_monitor.sh

设置rsync自动登录验证密码
vi /root/rsync.pwd
xxxxxx
保存,退出

设置只有root才可以查看的权限.
chmod 0600 /root/rsync.pwd

以下是备从务器b的配置:
安装rsync
yum rsync -y

#----配置rsnyd服务

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

复制代码 代码如下:

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

vi /etc/rsync.pas
rsync:xxxxxx

chmod 0600 /etc/rsync.pas

启动rsyncd
rsync --daemon
添加开机自动启动服务:
vi /etc/rc.local
rsync --daemon
回到主服务器a,
vi /etc/rc.local
添加以下内容,实时开机自动同步:

复制代码 代码如下:

/root/inotify_init.sh
/root/inotify_monitor.sh

保存退出

运行:

复制代码 代码如下:

/root/inotify_init.sh
/root/inotify_monitor.sh

这样就能实现实时同步图片文件了,在主服务器a的同步目录下新建一个文件测试下吧。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网