您的位置:首页 > 运维架构 > Linux

CentOS6.5实现rsync+inotify实时同步

2015-11-30 16:18 801 查看
参考博文:

参考1:CentOS6.5实现rsync+inotify实时同步

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

CentOS6.3下rsync服务器的安装与配置对rsync命令解释的很详细

rsync常见错误与解决方法整理

参考1比较详细,但是有点问题,参考2主服务器端比较详细但是有小问题把二者结合培正成功

注意:从主服务器拷贝到从服务器,千万别搞混了。

1、首先从主服务器A开始

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

在安装inotify-tools前请先确认你的linux内核是否达到了2.6.13,并且在编译时开启了CONFIG_INOTIFY选项,也可以通过以下命令检测,如果出现以下输出,说明支持:

[root@localhost~]#ls/proc/sys/fs/inotify/
max_queued_eventsmax_user_instancesmax_user_watches


下载并安装inotify-tools:

wgethttp://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gztarxvfinotify-tools-3.14.tar.gz
cdinotify-tools-3.14
./configure
make;makeinstall


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

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

vim/root/inotify_init.sh


内容如下:


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

DES=backup
IP=从服务器B的IP
USER=rsync
#DST=/etc/rsyncd远程rsync模块下的目录
INWT=/usr/bin/inotifywait#注意路径我的路径为:/usr/local/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,backup,rsync'#localdir,host,rsync_module,auth_user

INWT=/usr/bin/inotifywait#注意路径我的路径为:/usr/local/bin/inotifywait
RSYNC=/usr/bin/rsync
PASS=/root/rsync.pwd
###########################

foritemin${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'\
--eventCLOSE_WRITE,create,move$dir|whilereaddatetimefileevent
do
#echo$event'-'$file
case$eventin
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"
echo$cmd>>/var/log/rsyncd.log#写入日志文件
$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"
echo$cmd>>/var/log/rsyncd.log
$cmd
fi
;;
esac
done&
done

加执行权限:
chmod
+x
/root/inotify_monitor
.sh


设置RSYNC自动登录验证密码,认证文件只用加入密码即可

保存,退出

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

chmod
600
/root/rsync
.
pwd


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

yum
install
rsync
-y
#安装rsync服务


配置RSNYD服务:

vi
/etc/rsyncd
.conf


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

uid=root
gid=root
usechroot=no
maxconnections=0#没有连接限制
pidfile=/var/run/rsyncd.pid
lockfile=/var/run/rsync.lock
logfile=/var/log/rsyncd.log

[backup]
path=/从服务器B本地用于存放备份的目录
ignoreerrors
readonly=no
list=false
hostsallow=主服务器A的IP
authusers=rsync
secretsfile=/etc/rsync.pwd


设置密码文件:

注:当配置文件中参数strictmodes为true时,rsync认证口令文件的权限一定是600,否则客户端将不能连接服务器。rsync认证口令文件中每一行指定一个用户名:口令对,格式为:username:passwd。

启动RSYNCD

rsync--daemon


添加开机自动启动服务:

添加开机自动启动服务:

vi
/etc/rc
.
local


添加以下内容:

rsync--daemon


3、主服务器开机启动

vi
/etc/rc
.
local

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

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


保存退出

运行

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



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: