linux下两台服务器文件实时同步方案设计和实现
1、源服务器:192.168.1.66,目录/opt/test
目的服务器:192.168.1.68,目录/opt/bak/test
2、yum install rsync
3、配置目标服务器 /etc/rsyncd.conf
uid=root
gid=root
use chroot=no
max connections=10
strict modes=yes
pid file=/var/run/rsyncd.pid
lock file=/var/run/rsyncd.lock
log file=/var/run/rsyncd.log
[www]
path=/opt/bak/test
comment=analyse
read only=false
hosts allow=*
启动目标服务端:rsync --daemon
4、源服务器安装inotify-tools
wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz
tar xzf inotify-tools-3.14.tar.gz
cd inotify-tools-3.14
./configure --prefix=/usr/local && make && su -c 'make install'
5、源服务器创建脚本
创建目录/opt/soft/log/rsync.log
inotify_bak.sh
#!/bin/bash
src=/opt/test/
/usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib $src | while read file
do
/usr/bin/rsync -arzuq $src 192.168.1.68::www/
echo " ${file} was rsynced" >>/opt/soft/log/rsync.log 2>&1
done
放入后台执行 inotify_bak.sh &
6、可以从业务逻辑上实现实时双向同步,将图片资源保存在两个不同的目录A1、A2,且目录记录到数据库中,linux1主机A1目录同步linux2主机的A1目录,
linux2主机A2目录同步linux1主机A2目录即可!