Rsync + inotify 实现文件实时同步

简介:

实验目的:

 

实现两台Centos 7 宿主机之间文件可以实时同步

 

测试环境

 

服务端ip :192.168.10.180

 

客户端ip :192.168.10.181

 

同步目录:/home/test/html/

 

同步用户:test

 

部署服务端

 

Centos 7 默认已经安装 xinetd  rsync

 

查看 xinetd  rsync 是否安装

 

rpm -aq |grep xinetd

 

xinetd-2.3.15-13.el7.x86_64

 

rpm -aq |grep rsync

rsync-3.0.9-17.el7.x86_64

 

创建配置文件将xinetd服务下面的配置文件,将rsync交给xinetd管理,默认情况下centos7 没有此配置文件

 

cat /etc/xinetd.d/rsync

# default: off

# description: The rsync server is a goodaddition to an ftp server, as it

#  allows crc checksumming etc.

service rsync

 

{

       disable              = no 

       flags                  = IPv6

       socket_type      = stream

       wait                   = no

       user                   = root

       server                =/usr/bin/rsync

       server_args       = --daemon

       log_on_failure  += USERID

 

 

 

重启rsync 服务

 

systemctl restart xinetd

 

查看

netstat -tnlp | grep rsync

tcp       0      0 192.168.10.180:873        0.0.0.0:*               LISTEN      1016/rsync

 

 

创建同步用户(客户端也需要做同样操作)

 

useradd test

echo test | passwd test --stdin

Changing password for user test.

passwd: all authentication tokens updatedsuccessfully.

 

切换普通用户 test  创建ssh 免登陆

 

Su – test

 

ssh-keygen ## 一路回车

Generating public/private rsa key pair.

Enter file in which to save the key(/home/test/.ssh/id_rsa):

Created directory '/home/test/.ssh'.

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in/home/test/.ssh/id_rsa.

Your public key has been saved in/home/test/.ssh/id_rsa.pub.

The key fingerprint is:

74:d0:93:24:91:73:8d:1c:94:6b:20:fa:8d:cf:a5:6btest@test-bcliunx-1.novalocal

The key's randomart image is:

+--[ RSA 2048]----+

|       +B+*     |

|     . +oO .    |

|    . ..+.o     |

|   .  . .o      |

|    . oS.       |

|     o . .      |

 

将test 用户产生的龚玥拷贝到客户端服务器上面

 

ssh-copy-id -i test@192.168.10.181

 

ssh test@192.168.10.181 #测试不需要输入密码即可成功

 

安装inotify

 

下载软件:

wget https://sourceforge.net/projects/inotify-tools/files/latest/download/inotify-tools-3.14.tar.gz

 

编译安装:

 

tar zxvf  inotify-tools-3.14.tar.gz && cd inotify-tools-3.14

 

./configure

 

make && make install

 

 

配置

 

配置inotify 

 

ll  /proc/sys/fs/inotify/# 查看是否有以下信息输出,如果没有表示系统内核不支持

total 0

-rw-r--r-- 1 root root 0 Oct 27 16:28max_queued_events

-rw-r--r-- 1 root root 0 Oct 27 16:28max_user_instances

-rw-r--r-- 1 root root 0 Oct 27 16:28max_user_watches 

参数说明

max_queued_events   #表示监控事件队列

max_user_instances  #表示最多监控实例数

max_user_watches   #表示每个实例最多监控文件数

 

注:当要监控的目录、文件数量较多或者变化较频繁时,要加大这三个参数的值。

例如:可直接修改/etc/sysctl.conf配置文件,将管理队列设为32768,实例数设为1024,监控数设为9000000(建议大于监控目标的总文件数)

cat /etc/sysctl.conf

net.ipv4.conf.all.accept_redirects=0

fs.inotify.max_queued_events = 32768

fs.inotify.max_user_instances = 1024

fs.inotify.max_user_watches = 90000000

 

inotify常用参数:

-e  用来指定要监控哪些事件。

这些事件包括: create创建,move移动,delete删除,modify修改文件内容,attrib属性更改。

-m 表示持续监控

-r  表示递归整个目录

-q 表示简化输出信息。

 

测试

 

使用inotifywait命令监控 服务端 /home/test/htmll发生的变化。然后在另一个终端向/home/test/html目录下添加文件、移动文件,查看屏幕输出结果。

 

1 号终端输入:inotifywait-mrq -e modify,delete,create,attrib /home/test/html/

 

2 号终端输入: cd /home/test/html/ && touch1.HTML

 

1 号终端显示2号终端的监控事件

 

/home/test/html/ CREATE 1.HTML

/home/test/html/ ATTRIB 1.HTML

 

 

实例演示:

 

将服务端的/home/test/html/ 文件目录实时同步到客户端,使用实时同步操作前需要,配置test 用户的ssh 免登陆,上面已做配置,在服务器端执此脚本,服务器端的数据就好自动同步到客户端。参考脚本如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/bin/bash
# ---------------------------------------------------------------------------------------------------------
# Filename:    rsync_ inotifywait.sh
# Date:      2017/10/27
# Author:     http://sdsca.blog.51cto.com/10852974/1976974
# Description: Based on Centos 7.2
#
# Usage:      rsync_ inotifywait.sh
# Version:     1.0
# ------------------------------------------
Src= /home/test/html
Dst= test @192.168.10.181: /home/test/
T=` date +%Y-%m-%d-%H:%M:%S`
log= /home/test/log/rsync .log
  
/usr/local/bin/inotifywait-mrq  -e modify,delete,create,attrib ${Src} |  while  read  files   
         do
               /usr/bin/rsync  -ahqzt --delete$Src $Dst
         echo  "`date +'%F %T'` 出现事件 $files"  >>${log}2>&1
         done

 



 本文转自 水滴石川1 51CTO博客,原文链接:http://blog.51cto.com/sdsca/1976974,如需转载请自行联系原作者

相关文章
|
20天前
|
监控 安全 Unix
Rsync+Inotify实现数据实时同步
Rsync+Inotify实现数据实时同步
|
10月前
|
监控
inotify+rsync实现实时同步数据
inotify+rsync实现实时同步数据
87 0
|
7月前
|
监控 Linux
rsync+inotify实时同步
rsync+inotify实时同步
|
10月前
|
监控 安全 Shell
使用 inotify 和 rsync 实现文件实时同步
使用 inotify 和 rsync 实现文件实时同步
254 1
|
监控 Linux
配置inotify + rsync 实现实时同步
配置inotify + rsync 实现实时同步
216 0
配置inotify + rsync 实现实时同步
|
监控 安全 Shell
inotify+rsync实现实时同步
1.1 什么是实时同步:如何实现实时同步 要利用监控服务(inotify),监控同步数据服务器目录中信息的变化 发现目录中数据产生变化,就利用rsync服务推送到备份服务器上 1.2 实现实时同步的方法    inotify+rsync 方式实现数据同步    sersync 方式实现实时数据同步 详情参照:http://www.cnblogs.com/clsn/p/7707828.html 1.2.1 实时同步原理介绍   1.3 inotify+rsync 方式实现数据同步 1.3.1 Inotify简介   Inotify是一种强大的,细粒度的。
1823 0
|
数据安全/隐私保护 网络安全 Shell
|
Web App开发 监控 网络协议