Chinaunix首页 | 论坛 | 博客
  • 博客访问: 510828
  • 博文数量: 101
  • 博客积分: 1635
  • 博客等级: 上尉
  • 技术积分: 1282
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-05 01:51
文章分类

全部博文(101)

文章存档

2019年(2)

2018年(16)

2013年(14)

2012年(69)

我的朋友

分类: LINUX

2012-11-12 15:55:20

实现实时同步将A服务器的/home/test/目录下面的所有文件同步到B服务器的/tmp/data/目录下面
并且修改A的文件之后实时同步到B下面
删除B下面的文件A并不受到影响
首先在A服务器下面 检查 系统是否支持inotify
[root@htdb ~]# ll /proc/sys/fs/inotify/
total 0
-rw-r--r-- 1 root root 0 Nov  2 16:25 max_queued_events
-rw-r--r-- 1 root root 0 Nov  2 16:25 max_user_instances
-rw-r--r-- 1 root root 0 Nov  2 16:25 max_user_watches
上面表示支持
下载和安装inotify-tools:

下面对两个脚本的编写
[root@htdb ~]# vi inotify_init.sh
#!/bin/sh
SRC=/home/test/  #记得在最后面加/不然RYNC会自动增加一层目录
 
DES=bbsatt
IP=192.168.55.34
USER=root
#DST=/tmp/data/ #远程rsync模块下的目录
INWT=/usr/bin/inotifywait
RSYNC=/usr/bin/rsync
 
$RSYNC -zahqt --password-file=/root/rsync.pwd $SRC
之后chmod +x inotify_init.sh
接下来的是编辑 inotify_monitor.sh 脚本
[root@htdb ~]# vi inotify_monitor.sh
#!/bin/bash
 
###########################
sync[0]='/home/test/,192.168.55.34,bbsatt,root'
 
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 --password-file=$PASS --include=$file $dir "
echo $cmd >> a.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 "
echo $cmd >>a.log
$cmd
fi
;;
esac
done &
done
设置rsync自动登陆密码验证
vi /root/rsync.pwd
chmod 600 /root/rsync.pwd
之后在B服务器端
要安装rsync

修改配置文件
vi /etc/rsyncd.conf
[root@htdb data]#more /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 400
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
 
[bbsatt]
path = /tmp/data/
ignore errors
read only = no
list = false
hosts allow = 192.168.55.15
auth users = root
secrets file = /etc/rsync.pas

vi /etc/rsync.pas
root:xxxx
chmod 600 /etc/rsync.pas
启动RSYNCD
rsync --daemon
回到主服务器A
vi /etc/rc.local
/root/inotify_init.sh
/root/inotify_monitor.sh
ok
 

 
阅读(931) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~