加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

inotify和rsync实现实时同步

发布时间:2020-12-14 01:09:13 所属栏目:Linux 来源:网络整理
导读:inotify和rsync实现实时同步 首先先用两台主机实现rsyncc同步 服务端: [[email?protected] data 14:20:35]#echo "rsyncuser:123" /etc/rsync.pass 生成验证文件 [[email?protected] data 14:21:13]#chmod 600 /etc/rsync.pass [[email?protected] data 14:2
inotify和rsync实现实时同步
首先先用两台主机实现rsyncc同步
服务端:
[[email?protected] data 14:20:35]#echo "rsyncuser:123" > /etc/rsync.pass 生成验证文件
[[email?protected] data 14:21:13]#chmod 600 /etc/rsync.pass
[[email?protected] data 14:21:19]#mkdir /backup 准备目录
[[email?protected] data 15:18:52]#vim /etc/rsyncd.conf 配置/etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 0
ignore errors
exclude = lost+found/
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
reverse lookup = no
hosts allow = 192.168.0.0/24
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass
comment = ftp export area
[ [email?protected] ~ 14:45:22]#systemctl start rsyncd 开启服务,监听873端口

客户端:
[[email?protected] ~ 12:02:50]#echo "123" > /etc/rsync.pass 生成密码文件
[[email?protected] ~ 15:45:39]#chmod 600 /etc/rsync.pass
[[email?protected] ~ 15:45:47]#rsync -avz --password-file=/etc/rsync.pass /data/
[email?protected]::backup 同步数据
sending incremental file list
./
ERROR: daemon refused to receive directory "lost+found"
*** Skipping any contents from this failed directory ***
data/

sent 72 bytes received 16 bytes 176.00 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

结合inotify+rsync实现同步:
[[email?protected] ~ 16:08:40]#yum -y install epel-release
[[email?protected] ~ 16:08:57]#yum -y install inotify-tools
[[email?protected] ~ 16:11:33]#vim inotify_rsync.sh
br/>#!/bin/bash
SRC=‘/data/‘
DEST=‘[email?protected]::backup‘
inotifywait -mrq --timefmt ‘%Y-%m-%d %H:%M‘ --format ‘%T %w %f‘ -e create,delete,moved_to,close_write,attrib ${SRC} |while read DATE TIME DIR FILE;do
FILEPATH=${DIR}${FILE}
rsync -az --delete --password-file=/etc/rsync.pass $SRC $DEST && echo "At ${TIME} on ${DATE},file $FILEPATH was backuped up via rsync" >> /var/log/changelist.log
#注意:因为ext系统文件系统有lost+found文件夹,所有rsync会同步成功,但命令的执行结果是失败的,所有不会写日志至/var/log/changelist.log中,要想成功,在ext系统文件系统中把&&换成||即可
done
[[email?protected] ~ 16:13:53]#./inotify_rsync.sh


在客户端创建文件


服务端几乎瞬间就能够同步

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读