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/ [email protected]::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/   [email protected]::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

你可能感兴趣的:(linux,bash,服务器)