利用 inotifywait 和 rsync 实现文件秒级同步

安装 inotify-tools

yum install -y inotify-tools

安装 rsync

yum install -y xinetd

配置 rsync

把 /etc/xinetd.d/rsync 里面的 disable 值由 yes 改为 no,启动 xinetd,“service xinetd start”,再看看 netstat -tunlp | grep xinetd ,发现 rsync 已经启动。

# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#       allows crc checksumming etc.
service rsync
{
        disable = no
        flags           = IPv6
        socket_type     = stream
        wait            = no
        user            = root
        server          = /usr/bin/rsync
        server_args     = --daemon
        log_on_failure  += USERID
}

以上两个工具最好在每台服务器都安装上,至于 rsync 配置则在需要同步的服务器上配置好

vim /etc/rsyncd.conf

# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
uid = root
gid = root

use chroot = no

read only = yes

#limit access to private LANs
hosts allow=192.168.1.1
hosts deny=*
max connections = 5

pid file = /var/run/rsyncd.pid

secrets file = /etc/rsyncd/rsyncd.secrets
#lock file = /var/run/rsync.lock

motd file = /etc/rsyncd/rsyncd.motd

#This will give you a separate log file
log file = /var/log/rsync.log

#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes

log format = %t %a %m %f %b
syslog facility = local3
timeout = 300

# MODULE OPTIONS
[test]
path = /var/www/html/test/
read only = no
list=yes
ignore errors
exclude = important/

重启 xinetd

service xinetd restart

同步脚本

在分发服务器把需要监听的目录写在一个文件里 (/usr/local/src/notify/notify-list),一行一个目录,如果需要同步的目录里有某个目录不需要同步,则在前面加上 @ 即可

/var/www/html/test/
@/var/www/html/test/cache

同步脚本

#!/bin/bash
inotifywait -mrq -e close_write,delete,create,attrib --fromfile /usr/local/src/notify/notify-list --timefmt '%Y/%m/%d %H:%M' --format '%T %w%f%e' | while read file
do
rsync -arqz --delete /var/www/html/test/ 192.168.1.2::test > /dev/null 2>&1
done

执行此脚本后,在分发服务器上的 test 目录的变动都会同步到对应的服务器上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值