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

参考博文:html

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

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

CentOS 6.3下rsync服务器的安装与配置  对 rsync命令解释的很详细github

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

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

 

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

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

须要肯定你的系统是否支持inotify:centos

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

[root@localhost ~]# ls /proc/sys/fs/inotify/
max_queued_events  max_user_instances  max_user_watches

下载并安装inotify-tools:服务器

wget http://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz 
tar xvf inotify-tools-3.14.tar.gz  
cd inotify-tools-3.14  
./configure  
make;make install

接下来须要写两个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
###########################

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 "
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自动登陆验证密码,认证文件只用加入密码便可

vi  /root/rsync . pwd
xxxxxx

保存,退出

设置只有ROOT才能够查看的权限.

chmod  600  /root/rsync . pwd
 

二、如下是备从务器B的配置:

yum install rsync -y    #安装rsync服务

配置RSNYD服务:

vi /etc/rsyncd.conf

内容以下,须要把 Apache修改为你运行网站的用户名,个人是由于原来使用apache,虽然如今用Nginx,也一直没改用户名:
uid = root
gid = root
use chroot = no
max connections = 0   #没有链接限制
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
  
[backup]
path = /从服务器B本地用于存放备份的目录
ignore errors
read only = no
list = false
hosts allow = 主服务器A的IP
auth users = rsync
secrets file = /etc/rsync.pwd

设置密码文件:

vim  /etc/rsync . pwd     #添加如下内容
rsync :123456
chmod  600  /etc/rsync . pwd     #修改密码文件权限为600

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

启动RSYNCD

rsync --daemon

添加开机自动启动服务:

添加开机自动启动服务:

  vi  /etc/rc . local

添加如下内容:

rsync --daemon

三、主服务器开机启动

vi  /etc/rc . local
添加如下内容,实时开机自动同步:
/root/inotify_init.sh
/root/inotify_monitor.sh

保存退出

运行

/root/inotify_init.sh
/root/inotify_monitor.sh
相关文章
相关标签/搜索