Linux下rsync+inotify实现实时文件同步

一、【接收端配置】
#cat /etc/rsyncd.conf
uid = root
gid = root
max connections = 8
pid file = /var/run/rsyncd.pid
log file=/var/log/rsyncd.log
hosts allow = 192.168.3.0/24
[dkms]
read only=no
write only=no
path=/data/digitalkey/upload/dkms
comment=dkms update
auth users=root #必须设置,否则无密码访问
secrets file=/etc/rsync.password

#chmod 600 /etc/rsync.password 必须为600权限
-rw------- 1 root root 14 May 15 13:38 /etc/rsync.password
#vi /etc/rsync.password 访问权限
root:pass1234

以deamon模式运行
#rsync --daemon --config=/etc/rsyncd.conf

二、【发送端(rsync + inotify)】
rsync只需要配置密码权限
#cat /etc/rsync.password
pass1234

测试
#rsync -avu /data/digitalkey/upload/dkms/ root@192.168.3.243::dkms --password-file=/etc/rsync.password

三、【inotify安装】
1、下载inotify安装包
wget --no-check-certificate https://jaist.dl.sourceforge.net/project/inotify-tools/inotify-tools/3.13/inotify-tools-3.13.tar.gz
2、解压安装包并进入解压文件夹
tar -zxf inotify-tools-3.13.tar.gz
cd inotify-tools-3.13/
3、安装inotify
./configure
make
make install
4、检查是否安装成功
#inotifywait
No files specified to watch!

四、【创建脚本】
#vi inotify_rsync.sh

#!/bin/bash
/usr/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e close_write,delete,create,attrib /data/digitalkey/upload/dkms \
        | while read files
do
     rsync -avu /data/digitalkey/upload/dkms/   root@192.168.3.243::dkms  --password-file=/etc/rsync.password > /dev/null 2>&1
      echo "rsync was finished !" >> /tmp/rsync.log 2>&1
done

#/yunwei/sh/inotify_rsync.sh & 后台运行
#echo “/yunwei/sh/inotify_rsync.sh &” >> /etc/rc.local 脚本加入系统自启动文件
#ps -ef|grep rsync 查看进程
root 2504 1 0 13:27 ? 00:00:00 rsync --daemon --config=/etc/rsyncd.conf
root 8615 2421 0 14:12 pts/0 00:00:00 /bin/bash /yunwei/sh/inotify_rsync.sh
root 8618 8615 0 14:12 pts/0 00:00:00 /bin/bash /yunwei/sh/inotify_rsync.sh
root 8620 2421 0 14:12 pts/0 00:00:00 grep --color=auto rsync

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值