inotify和rsync实现实时同步

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了inotify和rsync实现实时同步相关的知识,希望对你有一定的参考价值。

1.搭建rsync服务端
systemctl start rsyncd
systemctl enable rsyncd
systemctl status rsyncd
ss -ntl |grep 873
rpm -ql rsync
2.修改配置文件
vi /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.8.0/24 #客户端IP
[backup]
path = /backup/
comment = backup
read only = no
auth users = rsyncuser
secrets file = /etc/rsync.pass

3.创建rsync备份服务器目录
mkdir /backup

服务器端生成验证文件
echo "rsyncuser:magedu" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
? 服务器端准备目录
mkdir /backup
服务器端启动rsync服务
rsync --daemon 可加入/etc/rc.d/rc.local实现开机启动
systemctl start rsyncd CentOS 7
客户端配置密码文件
echo "magedu" > /etc/rsync.pass
chmod 600 /etc/rsync.pass
客户端
开启inotify
yum -y install inotify-tools
4.客户端测试同步数据
rsync -avz --password-file=/etc/rsync.pass /data/backup rsyncuser@rsync服务器IP::backup

5.客户端创建inotify_rsync.sh脚本
vim inotify_rsync.sh br/>#!/bin/bash
SRC=‘/data/backup/‘
DEST=‘rsyncuser@rsync服务器IP::backup‘
inotifywait -mrq --timefmt ‘%Y-%m-%d %H:%M‘ --format ‘%T %w %f‘ -e create,delete,moved_to,close_write ${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
done

chmod +x inotify_rsync.sh
开启后台监控进程

6.sh inotify_rsync.sh &
ps -ef |grep inotify_rsync
注意:两台主机必须开启ssh免密登录,允许客户端把数据被分到rsync服务器上.


以上是关于inotify和rsync实现实时同步的主要内容,如果未能解决你的问题,请参考以下文章

rsync与inotify实现数据实时同步

inotify+rsync实现实时同步

inotify和rsync实现实时同步

inotify和rsync实现实时同步

rsync+inotify实现实时同步

0基础linux运维教程 Rsync结合inotify实现数据实时同步