rsync+inotifywait

0、rsync+inotify是实现文件实时同步的,加什么参数才能实现实时同步,--delete参数又是什么意思?

yum -y install inotify-tools 

1.运行模式(服务器)

    rsync有两种常用的认证方式,一种是rsync-daemon方式,我们使用最多的是rsync-daemon方式。

    这种模式是基于C/S模式的,rsync在后台启用了一个守护进程,这个守护进程在rsync服务器永久运行,用于接收请求传输文件,因此,客户端既可以把文件推送(push)给守护进程,也可以向守护进程拉取(pull)文件。rsync的服务器模式非常适合为异地的中心备份服务器或数据异地存储库来使用。

我的实验架构:

                                    10.1.1.36    ---------------push---------------------10.1.185

                                     (client)                                                          (server,备份用的)

2.安装配置

a.在客户端10.1.1.36(这是一台邮件服务器)vim /etc/rsyncd.pass  (写入123456)   

chmod 600 /etc/rsyncd.pass
yum install rsync -y

b.在服务端10.1.1.85(专门用来备份的机器)

yum install rsync -y

创建 mkdir /data/test,首先进行配置文件的设定,我们设置的如下:

# cat /etc/rsyncd.conf

uid = root
gid = root
use chroot = no
max connections = 10
pid file = /var/run/rsyncd.pid
log file = /var/log/rsync.log
transfer logging = yes
timeout = 900
ignore nonreadable = yes
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

[nextcloud]
path = /data/nextcloud
comment = pan.comratings.com
read only = no
write only = no
hosts allow = 10.0.2.30
list = no
ignore errors
auth users = backup
secrets file = /etc/rsyncd.pass

   增加密码认证文件:(在服务端)

# vim /etc/rsyncd.pass
backup:123456  
chmod 600 /etc/rsyncd.pass

 

   启动服务器:

systemctl start rsyncd.service

在10.1.1.36(client)rsync -vzrtopg  --progress ./ --password-file=/etc/rsyncd.pass backup@10.1.1.85::nextcloud   ------push   增量推送  把  ./下的文件增量得同步到10.1.1.85:/data/test/下

在10.1.1.36 (client) rsync -vzrtopg  --progress  --password-file=/etc/rsyncd.pass backup@10.1.1.85::nextcloud   ./  -------pull   增量拉下   把10.1.1.85:/data/test/下文件增量拉到36的./

 

v是“--verbose”显示详细输出模式
z是“--compress”压缩模式
r是“--recursive”以子目录递归模式
t是“--times“用来保持文件信息时间
o是”--owner“用来保持文件的属主信息
p是”--perms“用来保持文件权限
g是”--group“用来保持文件的属组
--progress:用来显示数据镜像同步的过程
--delete:指定以rsync服务器为基准进行数据镜像同步,也就是要保持rsync服务器端目录与客户端目录的完全一致
rsync中的-delete参数是指“ 删除那些DST中SRC没有的文件”。 --exclude:用于排除不需要文件传输的类型

下面是我在客户端的小脚本,把客户端本地的文件同步到rsync服务器
通过inotifywait 中的-m参数可以实现“始终保持事件监听状态”

#!/bin/bash
#author:xiaoweige
host_slave=10.1.1.85
master_src=/data/test/
inotify_home=/usr/local/inotify

${inotify_home}/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e close_write,modify,delete,create,attrib $master_src \
| while read file

do
echo $file
rsync -vzrtopg --progress /data/test/ --password-file=/etc/rsyncd.pass backup@10.1.1.85::nextcloud

done

posted @ 2018-01-19 15:21  littlevigra  阅读(252)  评论(1编辑  收藏  举报