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的同步目录下新建一个文件测试下吧。








