阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Rsync结合Inotify 实时同步配置

145次阅读
没有评论

共计 3467 个字符,预计需要花费 9 分钟才能阅读完成。

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
[svn]
path = /data/svn 需要同步的目录
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 /data/svn webuser@192.168.121.129::svn

# 注意:/data/svn 目录默认系统是已经有的,如果同步其他目录,源主机肯定有的,但目的主机可能没有,需要手动创建,否则会报错,找不到目录,同步失败。

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=”https://www.linuxidc.com/data/svn/”# 注意 src 如果为 /tmp,将把 tmp 目录同步至目标主机,出现 /tmp/tmp 递归目录,所以需要同步那个目录下的文件,需要以 / 结尾。
host=”192.168.121.129″
module=”svn”
/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 &

RSync 实现文件备份同步详解   http://www.linuxidc.com/Linux/2014-09/106967.htm

利用 inotifywait 监控主机文件和目录 http://www.linuxidc.com/Linux/2013-03/81075.htm

利用 inotify+rsync 实现 Linux 文件批量更新 http://www.linuxidc.com/Linux/2012-01/52132.htm

inotify-tools+rsync 实时同步文件安装和配置 http://www.linuxidc.com/Linux/2012-06/63624.htm

rsync 同步完整配置 http://www.linuxidc.com/Linux/2013-06/85781.htm

CentOS 6.5 下 Rsync 远程同步 http://www.linuxidc.com/Linux/2014-05/101084.htm

Linux 文件夹对比并提取的差分文件技巧 -rsync 的妙用 http://www.linuxidc.com/Linux/2016-02/128307.htm

Rsync 服务器的安装与配置详解  http://www.linuxidc.com/Linux/2017-01/139888.htm

Rsync 的详细介绍 :请点这里
Rsync 的下载地址 :请点这里

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-02/140877.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3467字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中