rsync+innotify实现数据自动同步
IP规划:
172.16.0.111
172.16.0.112
1.Rsync的安装(默认rsync是已经安装的):
注:本文章的重点是inotify,所以这里rsync直接使用yum 安装
yum install –y rsync
2.inotify安装:
[root@master package]# tar xf inotify-tools-3.14.tar.gz
[root@master package]# cd inotify-tools-3.14
[root@master inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify && make && make install
编写自动同步脚本
[root@master ~]# cd /usr/local/inotify/
[root@master inotify]# vim inotify.sh
#!/bin/bash
SRC=/tmp/a/ #定义源目录
DST=/tmp/a #定义目标目录
DIP=172.16.0.112 #定义目录IP,如果用多个IP可以放入一个配置文件,用for循环逐个遍历。
DUSER=root #同步使用的用户
/usr/local/inotify/bin/inotifywait -mrq -e modify,delete,create,attrib /tmp/a/ | while read x y z
do
rsync $SRC $DUSER@$DIP:$DST -aHt --delete
done
rsync参数说明:
-a 规档模式,复制目录、符号链接,等价于 -rlptgoD
-H 保留硬链接
-t 保存修改时间
更多参数: rsync -h
注:有的时候当网络负载压力比较大的时候可以使用rsync进行限速。
rsync可以对复制进行限速:
比如rsync限制为 300k Bytes/s:
#rsync -auvzP --bwlimit=300 本地的文件 远程的文件
参数说明(根据情况自行选择):
v:详细提示
a:以archive模式操作,复制目录、符号连接,等价于 -rlptgoD 。
z:压缩
u:只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时
P:是综合了--partial --progress两个参数,
所以此时的rsync支持了断点续传
inotify 参数说明:
-m 监控
-r 递归
-q 静默模式
-e 指定你要同步的事件
modify 修改
delete 删除
create 创建
attrib 属性
更多参数请查看[root@master bin]# ./inotifywait --help