天天看点

rsync+inotify实时数据同步

1.yum 安装rsync

备份服务器端:

yum install -y rsync

vi /etc/xinetd.d/rsync

<a href="http://s2.51cto.com/wyfs02/M00/8C/27/wKioL1hjsUjRX-9YAABbRJmQ7aE848.png" target="_blank"></a>

yum install -y xinet*

/etc/init.d/xinetd start

<a href="http://s2.51cto.com/wyfs02/M00/8C/27/wKioL1hjsXqxdNZjAAAWFWNhYp0929.png" target="_blank"></a>

以上是安装服务,很多情况只用这个命令就可以了,不需要安装这个服务

 vim /etc/rsyncd.conf      //该rsyncd.conf不存在,需要自己手动建  

port=873

log file=/var/log/rsync.log  #指定日志

pid file=/var/run/rsyncd.pid  #指定pid

[test]    #为模块名,自定义

path=/root/rsync  # 指定该模块对应在哪个目录下

use chroot=true #是否限定在该目录下,默认为true,当有软连接时,需要改为fasle

max connections=4  # 指定最大可以连接的客户端数

read only=no  //yes指定客户端对该共享目录只有只读权限只能下载,no有读写和上传权限,共享目录必须有W写权限才能上传 

list=true  #是否可以列出模块名

uid=victor #以哪个用户的身份来传输

gid=victor  #以哪个组的身份来传输

auth users=victor #指定验证用户名,可以不设置

secrets file=/etc/rsyncd.passwd #指定密码文件,如果设定验证用户,这一项必须设置

hosts allow=104.193.95.0/24

hosts allow = 104.193.95.63  (也可以写一个IP,不用写用户名跟密码,) 

mkdir -p /root/rsync

chown victor:victor /root/rsync

/etc/init.d/xinetd restart

rsync --daemon 

vi /etc/rsyncd.passwd

victor:SX3edc!23

chmod 600 /etc/rsyncd.passwd

主服务器:

SX3edc!23

rsync -avP --password-file=/etc/rsyncd.passwd /etc/passwd [email protected]::test

<a href="http://s3.51cto.com/wyfs02/M00/8C/32/wKiom1hkxlqh467CAABSe-fXua4181.png" target="_blank"></a>

错误:

用户密码错误

检查主服务器密码文件和从服务器密码文件。

主服务器密码文件 /etc/rsyncd.secrets 格式为: username:password

从服务器密码文件 password.rsync 格式为:password

2.inotify-tools安装

inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件。 inotify-tools是用c编写的,除了要求内核支持inotify外,不依赖于其他。inotify-tools提供两种工具,一是inotifywait,它是用来监控文件或目录的变化,二是inotifywatch,它是用来统计文件系统访问的次数。如果列出的内核版本不低于 2.6.13,系统就支持 inotify。还可以检查机器的 /usr/include/sys/inotify.h 文件。如果它存在,表明内核支持 inotify。

下载地址:

1.wget http://github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

<a href="http://s4.51cto.com/wyfs02/M00/8C/2F/wKioL1hkzTuj0NxSAAAcFWBdCGw932.png" target="_blank"></a>

[root@localhost  ~]# ll /proc/sys/fs/inotify

-rw-r--r-- 1 root root 0 4月  29 08:53 max_queued_events  表示监控事件队列  (长度)

-rw-r--r-- 1 root root 0 4月  29 08:53 max_user_instances  表示最多监控实例数

-rw-r--r-- 1 root root 0 4月  29 08:53 max_user_watches    表示每个实例最多监控文件数

<a href="http://s3.51cto.com/wyfs02/M00/8C/2F/wKioL1hkze_yc3cPAAAu9EoL1sU192.png" target="_blank"></a>

inotify修改数值

vi /etc/sysctl.conf

<a href="http://s3.51cto.com/wyfs02/M01/8C/33/wKiom1hkzo3Ru4z0AAA-ewxqGKw298.png" target="_blank"></a>

在最后

sysctl -p生效

tar xzf inotify-tools-3.14.tar.gz

cd inotify-tools-3.14

 ./configure --prefix=/usr/local/inotify-tools

<a href="http://s4.51cto.com/wyfs02/M02/8C/2F/wKioL1hk0GmxeObMAAAQVW_k2VU965.png" target="_blank"></a>

以下就是安装完成了。

inotifywait 实时监控/home的所有事件(包括文件的访问,写入,修改,删除等)

inotifywatch统计文件系统的事件

<a href="http://s4.51cto.com/wyfs02/M01/8C/3F/wKioL1hmUFTjvi7TAAAR2nfqlKw082.png" target="_blank"></a>

例:rsync+inotify脚本

host=103.242.109.12  #rsync服务器IP

src=/victor/       #本地监控的目录

dst1=test         #rsync服务器上模块名

user1=victor       #rsync服务器上的虚拟用户

<a href="http://s2.51cto.com/wyfs02/M02/8C/42/wKiom1hmUtPxPAYhAABRIN3Fckk318.png" target="_blank"></a>

#!/bin/bash

host=103.242.109.12

src=/victor/

dst1=test

user1=victor

/usr/local/inotify-tools/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib $src\

| while read files

do

/usr/bin/rsync -avPz --delete --password-file\=/etc/rsyncd.passwd  $src $user1@$host::$dst1

 echo "${files}was rsynced" &gt;&gt;/tmp/rsync.log 2&gt;&amp;1

done

\自动换行, \=转义

 /root/ifyrsync.sh &amp;

将此脚本加入系统自启动文件:

echo /root/ifyrsync.sh &amp;”&gt;&gt;/etc/rc.local

在inotify服务器的/victor/下新建目录,看rsync服务器上的test下是否会同步

错误信息:

1.inotify服务器上:

syntax or usage error (code 1) at main.c(1238) [sender=3.0.6]

这个一般是服务器端的目录不存在或无权限。我遇到的问题就是inotify手写的脚本有问题,后来复制以前的脚本就解决了。

2.rsync服务器的日志错误:

<a href="http://s1.51cto.com/wyfs02/M02/8C/43/wKioL1hncWiBzcsXAADTjVzDbtQ437.png" target="_blank"></a>

本文转自 15816815732 51CTO博客,原文链接:http://blog.51cto.com/68686789/1887088