使用rsync+inotify配置触发式(实时)远程同步

本文介绍如何在RHEL5系统环境中利用NFS和rsync实现两台主机之间的实时文件同步功能。具体步骤包括配置NFS共享、安装inotify-tools并编写同步脚本来监控文件变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系统环境:RHEL5 [ 2.6.18-8.el5xen ]
软件环境:
    rsync-2.6.8-3.1
    nfs-utils-1.0.9-16.el5
    portmap-4.0-65.2.2.1
    inotify-tools-3.13.tar.gz
        —— http://downloads.sourceforge.net/inotify-tools/inotify-tools-3.13.tar.gz?modtime=1199213676&big_mirror=0
目标功能:
    源主机H1: 192.168.1.11/24
    目标主机H2: 192.168.1.12/24
    将H1主机中的开发数据(/var/devel/目录),上传同步至H2主机的/backup/devel/h1/目录。——当源数据有文件或目录更新时,即时启动rsync同步进程。[基于安全性考虑,建议只在内部网络中使用]
################################################################
    除inotify-tools(需要2.6.13以上内核的inotify功能支持)以外,其他软件均使用RHEL5系统自带的rpm包安装。
一、配置目标主机H2(发布NFS可写共享)
shell> mkdir -p /backup/devel/h1/
shell> vi /etc/exports
/backup/devel/h1    192.168.1.11(rw,no_root_squash)
shell> service portmap start
shell> service nfs start
shell> chkconfig portmap on
shell> chkconfig nfs on
  如有必要,可以结合防火墙规则控制访问权限
shell> iptables -I INPUT -p tcp --dport 111 -j DROP
shell> iptables -I INPUT -p tcp --dport 111 -s 192.168.1.11 -j ACCEPT
shell> iptables -I INPUT -p udp --dport 111 -j DROP
shell> iptables -I INPUT -p udp --dport 111 -s 192.168.1.11 -j ACCEPT

二、配置源主机H1(上传备份发起端)
    1、安装inotify-tools工具包
shell> tar zxvf inotify-tools-3.13.tar.gz -C /usr/src/
shell> cd /usr/src/inotify-tools-3.13
shell> ./configure
shell> make
shell> make install
—— 可以使用man inotify、man inotifywait、man inotifywatch查看相关手册页。
    2、挂载H2发布的备份目录
shell> service portmap start
shell> chkconfig portmap on
shell> mkdir -p /media/h2nfsdir/
shell> vi /etc/fstab
192.168.0.12:/backup/devel/h1    /media/h2nfsdir    nfs    defaults,noexec    0 0
shell> mount /media/h2nfsdir
    3、编写触发同步脚本
shell> vi /opt/h1-h2_inosync.sh
#!/bin/sh
SRC=/var/devel/
DST=/media/h2nfsdir/
INWT=/usr/local/bin/inotifywait
RSYNC=/usr/bin/rsync
$INWT -mrq -e create,move,delete,modify $SRC | while read D E F ; do
    $RSYNC -aHqz --delete $SRC $DST
done
shell> chkmod +x /opt/h1-h2_inosync.sh
    4、每次开机自动运行监控脚本
shell> echo "/opt/h1-h2_inosync.sh &" >> /etc/rc.local
shell> /opt/h1-h2_inosync.sh &

三、测试实时同步
    在源主机H1上,修改/var/devel/目录中的内容(如增、删、改文件,添加、移除目录等),
    ——同时在目标主机H2上,观察备份目录/backup/devel/h1/中内容的变化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值