天天看点

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

The key's randomart image is:

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

|       +B+*     |

|     . +oO .    |

|    . ..+.o     |

|   .  . .o      |

|    . oS.       |

|     o . .      |

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

ssh-copy-id -i [email protected]

ssh [email protected] #测试不需要输入密码即可成功

安装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

<code>#!/bin/bash</code>

<code># ---------------------------------------------------------------------------------------------------------</code>

<code># Filename:    rsync_ inotifywait.sh</code>

<code># Date:      2017/10/27</code>

<code># Author:     http://sdsca.blog.51cto.com/10852974/1976974</code>

<code># Description: Based on Centos 7.2</code>

<code>#</code>

<code># Usage:      rsync_ inotifywait.sh</code>

<code># Version:     1.0</code>

<code># ------------------------------------------</code>

<code>Src=</code><code>/home/test/html</code>

<code>Dst=</code><code>test</code><code>@192.168.10.181:</code><code>/home/test/</code>

<code>T=`</code><code>date</code><code>+%Y-%m-%d-%H:%M:%S`</code>

<code>log=</code><code>/home/test/log/rsync</code><code>.log</code>

<code> </code> 

<code>/usr/local/bin/inotifywait-mrq</code> <code>-e modify,delete,create,attrib ${Src} | </code><code>while</code> <code>read</code> <code>files   </code>

<code>        </code><code>do</code>

<code>              </code><code>/usr/bin/rsync</code> <code>-ahqzt --delete$Src $Dst</code>

<code>        </code><code>echo</code> <code>"`date +'%F %T'` 出现事件 $files"</code> <code>&gt;&gt;${log}2&gt;&amp;1</code>

<code>        </code><code>done</code>

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

继续阅读