debian inotify+rsync文件实时同步
kevin.Zhu 发布于:2013-1-16 0:54 分类:Debian 有 17 人浏览,获得评论 0 条
案例:两台机器 192.168.3.1 192.168.3.2 , 实时同步 3.2 中/var/www/xiumeier/games/static 至 3.1中 /var/www/xiumeier/games/static_syn
3.1 ssh登录端口为 60180
1. 在两台机器上都安装 rsync 。 apt-get install rsync-tools
2. 机器 3.1是服务端 ,3.2运行rsync客户端 。 当3.2机器需要同步的目录有变化时 ,向3.1机器同步。
3. 配置3.1 rsync端口为 60181 。打开3.1中的rsync 。 /etc/init.d/rsync start
4. 机器3.1打开对机器3.2的密码登录。 ssh-keygen -t dsa ssh-copy-id -i ........(具体看其它教程)
5. 写脚本监控目录 。 如下例子: (监测到文件有 modify和create事件时 ,同步。)
#!/bin/bash
src=/var/www/xiumeier/games/static
#des=/var/www/xiumeier/games/static_sync
ip=192.168.3.1
inotifywait -mrq --format '%w%f' \
--exclude '.*cache$' \
-e modify,create \
${src} \
| while read file
do
cp_file=${file/static/static_syn}
rsync -az --port=60181 -e 'ssh -p 60180' "$file" root@${ip}:${cp_file}
echo $file
#echo $cp_file
#echo "---------------------------------------------------------------------------"
done
6. 将脚本放入 /etc/inittab 文件中 ,以保证脚本无中断运行(挂掉自动重启) 。
7. telinit q 刷新 /etc/inittab 生效
完成。