Rsync 结合 Inotify 实时同步配置
系统环境:192.168.121.128(源) 192.168.121.129(目的)
192.168.121.129(目的)安装 rsync 服务:
yum install rsync 或者 wget rsync 官网的 rsync-3.1.2.tar.gz 编译安装,不需要带参数 ./configure --prefix=/usr/local/rsync
- cat /etc/rsyncd.conf
- uid=root//RSYNC守护进程的用户
- git=root////运行RSYNC守护进程的组
- use chroot = no // //不使用chroot
- max connections = 10 //最大连接数限制
- strict modes = yes//如果为true,则密码文件只能被rsync服务器运行身份的用户访问,其他任何用户不可以访问该文件。默认值为true。
- pid file = /var/run/rsyncd.pid
- lock file = /var/run/rsync.lock
- log file = /var/log/rsyncd.log
- [tmp]
- path = /tmp//需要同步的目录
- comment = rsync from 192.168.121.128
- read only = no
- write only = no
- hosts allow = 192.168.121.128
- hosts deny = *
- list = false
- uid = root
- gid = root
- auth users = webuser//此用户与系统用户无关
- secrets file = /etc/rsync.passwd//定义认证的用户密码文件
cat /etc/rsync.passwd
- webuser: password
- //用户和密码以分号隔开
并且设置 600 文件属性:
- chmod 600 /etc/rsync.passwd
启动 rsync 服务:
- /usr/local/rsync/bin/rsync --port=873 --address=192.168.121.129 --daemon
在 192.168.121.128(源) 主机做推送测试:
- rsync -avH --delete --password-file=/etc/rsync.passwd /tmp/ ::tmp
#注意:/tmp 目录默认系统是已经有的,如果同步其他目录,源主机肯定有的,但目的主机可能没有,需要手动创建,否则会报错,找不到目录,同步失败。
192.168.121.128 源主机安装 inotify 服务
下载安装
- # wget http://nchc.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
- # tar xzvf inotify-tools-3.14.tar.gz
- # cd inotify-tools-3.13
- # ./configure --prefix=/usr/local/inotify
- # make
- # make install
创建 inotify_rsync.sh 脚本:
- cat inotify_rsync.sh
- #!/bin/sh
- #function:rysnc 192.168.121.128 to 192.168.121.129
- if [ ! -f /etc/rsync.passwd ];then
- echo "password">/etc/rsync.passwd
- /bin/chmod 600 /etc/rsync.passwd
- fi
- log=/usr/local/inotify/logs/rsync.log
- src="/tmp/"#注意src如果为/tmp,将把tmp目录同步至目标主机,出现/tmp/tmp递归目录,所以需要同步那个目录下的文件,需要以/结尾。
- host="192.168.121.129"
- module="tmp"
- /usr/local/inotify/bin/inotifywait -mr --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write,modify,delete,create,attrib $src | while read DATE TIME DIR FILE; do
- FILECHANGE=${DIR}${FILE}
- /usr/bin/rsync -avH --delete --progress --password-file=/etc/rsync.passwd $src --exclude-from="/usr/local/inotify/logs/rules.txt" webuser@$host::$module &
- echo "At ${TIME} on ${DATE}, file $FILECHANGE was backed up via rsync" >> $log
- done
#脚本中有 delete 参数,测试环境可以随便来,生产环境建议禁止 delete 参数。
- mkdir /usr/local/inotify/logs//如果没有目录或文件需要手动创建
- touch /usr/local/inotify/logs/rules.txt
相关注解如下:
/usr/local/bin/inotifywait -mrq -e modify,delete,create,attrib ${src}
-m 是保持一直监听
-r 是递归查看目录
-q 是打印出事件
-e close_write,modify,delete,create,attrib 是指 "监听 创建 移动 删除 写入 权限" 事件
/usr/bin/rsync -avH --delete --progress --password-file
-a 存档模式
-H 保存硬连接
-delete 删除于多余文件
--password-file 密码文件
今天参数可以 man rsync
要排除同步某个目录时,为 rsync 添加 --exculde=PATTERN 参数,注意,路径是相对路径,具体查看 man rsync。
要排除某个目录的事件监听的处理时,为 inotifywait 添加 --exclude 或 --excludei 参数,具体查看 man inotifywait。
--exclude-from="/usr/local/inotify/logs/rules.txt" 可以匹配过滤文件:
如排除包括 .svn 的文件:
#cat /usr/local/inotify/logs/rules.txt
- *.svn*
inotifywait 命令产生三个返回值,分别是 "日期,时间,文件" 这 3 个返回值会做为参数传给 read,因此脚本中的 "while read D E F" 写法细化了返回值。
赋予脚本可执行权限
- #chmod +x inotify_rsync.sh
运行
- #sh iotify_rsync.sh &
来源: