Rsync结合Inotify 实时同步配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rsync结合Inotify 实时同步配置相关的知识,希望对你有一定的参考价值。

 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/ [email protected]29::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" [email protected]$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 &




 


本文出自 “LINUX Super梦” 博客,请务必保留此出处http://215687833.blog.51cto.com/6724358/1883500

以上是关于Rsync结合Inotify 实时同步配置的主要内容,如果未能解决你的问题,请参考以下文章

配置inotify+rsync实时同步

0基础linux运维教程 Rsync结合inotify实现数据实时同步

RSYNC04-配置rsync+inotify实现实时同步

centos 7配置inotify+rsync实时同步

rsync结合inotify实现实时同步

rsync+inotify实时同步