Rsync+inotify实现实时同步

 

一、基本介绍

1、什么是Rsync ?

RsyncRemote Synchronize)是一款开源的、快速的、多功能的、可以实现全量及增量的本地或远程数据同步备份的优秀工具,并且支持多种操作系统平台运行。

官网文档:https://rsync.samba.org/ftp/rsync/rsync.html

 

2Rsync简介

Rsync具有本地与远程两台主机之间的数据快速复制同步镜像、远程备份等功能,该功能类似scp,但是优于scp功能,还具有本地不同分区目录之间全量及增量复制数据。

Rsync同步数据镜像时,通过“quick check”算法,仅同步大小或最后修改时间发生变化的文件或目录,当然也可以根据权限,属主等属性变化的同步,所以可以实现快速同步。

rsync 具有如下的基本特性:

1.            可以镜像保存整个目录树和文件系统

2.            可以很容易做到保持原来文件的权限、时间、软硬链接等

3.            无须特殊权限即可安装

4.            优化的流程,文件传输效率高

5.            可以使用 rshssh 方式来传输文件,当然也可以通过直接的 socket 连接

6.            支持匿名传输,以方便进行网站镜象

 

3Rsync命令格式

本地: rsync [OPTION...] SRC... [DEST]

通过shell远程访问:

拉取: rsync [OPTION...][USER@]HOST:SRC... [DEST]

推送: rsync [OPTION...] SRC...[USER@]HOST:DEST

通过rsync守护进程访问:

拉取: rsync [OPTION...][USER@]HOST::SRC... [DEST] 

    rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC...[DEST]

推送: rsync [OPTION...] SRC...[USER@]HOST::DEST 

    rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST

 

4、什么Inotify

Inotify是一种文件变化通知机制,Linux内核从2.6.13开始引入。在BSDMac OS系统中比较有名的是kqueue,可以高效地实时跟踪Linux文件系统的变化,变化包括创建、删除、修改、打开等等,及时通知程序做出相应处理,可以将这种通知机制通俗的理解为触发器。

 

5、架构关系:

IP

系统

服务

192.168.0.103

CentOS 6.8 x86_64

Rsync client + inotify server

192.168.0.132

CentOS 6.8 x86_64

Rsync server

 

6、架构图

wKioL1jbGWbSy_CnAAPFNRWZiak565.jpg-wh_50


 

二、Rsync编译安装

1、检查版本本机是否存在

[root@kry132 ~]# rpm -qa  rsync

rsync-3.0.6-12.el6.x86_64

 

2、卸载

[root@kry132 ~]# yum  remove rsync

说明:需要安装最新的版本就需要卸载老版本。

 

3、下载rsync软件

[root@kry132 ~]# cd  /usr/local/src/

[root@kry132 src]# wget https://download.samba.org/pub/rsync/rsync-3.1.2.tar.gz

 

4、解压包

[root@kry132 src]# tar zxvfrsync-3.1.2.tar.gz

 

5、创建程序安装目录,并编译安装

[root@kry132 src]# cd  rsync-3.1.2

[root@kry132 rsync-3.1.2]# mkdir/usr/local/rsync

[root@kry132 rsync-3.1.2]#./configure --prefix=/usr/local/rsync

[root@kry132 rsync-3.1.2]# make&& make install

 

6、配置环境变量

[root@kry132 ~]# vim  /etc/profile.d/rsync.sh

#!/bin/sh

export PATH=$PATH:/usr/local/srync/bin

 

7、建立软连接

[root@kry132 ~]# ln -s  /usr/local/rsync/bin/rsync /usr/bin/rsync

 

8、查看当前版本

[root@kry132 ~]# rsync--version

rsync  version 3.1.2 protocol version 31

 

三、Rsync server配置

1、编辑配置文件

[root@kry132 ~]# vim /etc/ rsyncd.conf

uid = rsync

gid = rsync

port = 873

use chroot = on

max connections = 200

timeout = 120

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsyncd.lock

log file = /var/log/rsyncd.log

 

[bakup]

path = /data/bakup

read only = false

list = false

hosts allow = 192.168.0.0/24

hosts deny = 0.0.0.0/32

auth users = rsync_bakup

secrets file = /etc/rsyncd.password

 

2、创建密码文件

[root@kry132 ~]# vim  /etc/rsyncd.password

rsync_bakup:4297f44b13955235245b2497399d7a93

说明:定义格式用户:密码

 

3、修改密码文件权限

[root@kry132 ~]# chmod 600/etc/rsyncd.password

 

4、创建Rsync server服务启动的系统账户

[root@kry132 ~]# useradd rsync-M  -s /bin/nologin

 

5、创建数据存放目录

[root@kry132 ~]# mkdir -p  /data/bakup

 

6、修改目录权限

[root@kry132 ~]# chown rsync.rsync/data/bakup

 

7、启动服务

[root@kry132 ~]# rsync --daemon

说明:如果配置文件路径不是/etc/rsyncd.conf,启动是需要使用“–config参数指定配置文件。

 

8、查看服务是否启动

[root@kry132 ~]# ps  axu |grep rsync

root     9198  0.0  0.0 107652  672 ?        Ss   19:41  0:00 rsync --daemon --config /etc/rsync/rsyncd.conf

root     9205  0.0  0.0 103308  848 pts/0    S+   19:44  0:00 grep rsync

[root@kry132 ~]# netstat -nlp|grep 873

tcp       0      0 0.0.0.0:873                 0.0.0.0:*                   LISTEN      9198/rsync         

tcp       0      0 :::873                      :::*                        LISTEN      9198/rsync   

 

四、客户端配置

1、创建密码文件

[root@Kry103 ~]# vim  /etc/rsyncd.password

 

2、修改权限

[root@Kry103 ~]# chmod 600/etc/rsyncd.password

 

3、安装客户端

[root@Kry103 ~]# yum  -y install rsync

说明:如果客户端存在,跳过该步骤。

 

五、测试

1、客户端上推送数据

[root@Kry103 ~]# ll test.txt

-rw-r--r--. 1 kry kry 13 Mar 24 15:55test.txt

[root@Kry103 ~]# rsync -avzPtest.txt rsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

sending incremental file list

test.txt

         11 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

sent 91 bytes  received 27 bytes  236.00 bytes/sec

total size is 11  speedup is 0.09

 

2、服务端查看文件是否推送成功

[root@kry132 bakup]# ll

total 4

-rw-r--r--. 1 rsync rsync 13 Mar 24 15:55test.txt


3、从服务端拉取数据

[root@Kry103 ~]# ll  /tmp/

total 0

[root@Kry103 ~]#  rsync -avzPrsync_bakup@192.168.0.132::bakup  /tmp--password-file=/etc/rsyncd.password

receiving incremental file list

./

test.txt

         13 100%   12.70kB/s    0:00:00 (xfer#1, to-check=7/17)

sent 64 bytes  received 419 bytes  966.00 bytes/sec

total size is 13981  speedup is 28.95

 

4、查看拉取的文件

[root@Kry103 ~]# ll  /tmp/

total 4

-rw-r--r--. 1 kry kry 13 Mar 24 15:55test.txt

说明:文件的属主组跟推送时保持一直。

 

六、安装配置Inotify服务(rsync clinet 安装)

1、确定系统是否支持inotify服务

[root@Kry103 ~]# ll  -l /proc/sys/fs/inotify/

total 0

-rw-r--r-- 1 root root 0 Mar 24 17:09max_queued_events

-rw-r--r-- 1 root root 0 Mar 24 17:09max_user_instances

-rw-r--r-- 1 root root 0 Mar 24 17:09max_user_watches

说明:如果存在以上三个文件表示支持,实际上是事件的队列。

 

2、下载软件包

网站下载地址:https://github.com/rvoicilas/inotify-tools/wiki

[root@Kry103 ~]# cd  /usr/local/src/

[root@Kry103 src]# wgethttps://cloud.github.com/downloads/rvoicilas/inotify-tools/inotify-tools-3.14.tar.gz

 

3、解压安装包

[root@Kry103 src]# tar zxvfinotify-tools-3.14.tar.gz

 

4、编译安装

[root@Kry103 src]# cdinotify-tools-3.14

[root@Kry103inotify-tools-3.14]# mkdir /usr/local/inotify

[root@Kry103inotify-tools-3.14]# ./configure --prefix=/usr/local/inotify/

[root@Kry103inotify-tools-3.14]# make && make install

 

5、配置环境变量

[root@Kry103 ~]# vim  /etc/profile.d/inotify.sh

#!/bin/sh

export PATH=$PATH:/usr/local/inotify/bin

[root@Kry103 ~]# source  /etc/profile.d/inotify.sh

 

6、创建实时同步脚本

[root@Kry103 ~]# mkdir script

[root@Kry103 ~]# vim /realtime_rsync.sh

#!/bin/sh

Host_01=192.168.0.132

src=/data/bakup/

rsync_passfile=/etc/rsyncd.password

inotify_home=/usr/local/inotify/

users=rsync_bakup

mode_01=bakup

 

#Check file or directory

if [ ! -e "$src" ]\

|| [ ! -e "$rsync_passfile" ]\

|| [ ! -e  "$inotify_home/bin/inotifywait" ]\

|| [ ! -e "/usr/bin/rsync" ]

then

         echo "Check File and directory"

         exit 9

fi

 

$inotify_home/bin/inotifywait -mrq  --timefmt '%d/%m/%y %H:%M' --format '%T %w%f'   -e close_write,create,access,attrib $src | while read file

         do

         cd $src && /usr/bin/rsync -avzP  --timeout=120 --password-file=$rsync_passfile  $src $users@$host_1::$mode_1 > /dev/null 2>&1

         done

exit 0

说明:

Host_01:定义Rsync server ip

src:定义监控目录;

rsync_passfile:定义rsync同步用户的密码文件;

inotify_home:定义inotify程序路径;

users:定义rsync同步用户;

mode_01:定义rsync模块;

关于inotify的选项及事件可以通过以下命令了解:

[root@Kry103~]# /usr/local/inotify/bin/inotifywait -h

 

6、配置脚本权限

[root@Kry103 ~]# chmod  +x script/realtime_rsync.sh

 

7、测试(检测目录变化)

 [root@Kry103 ~]# sh  -x script/realtime_rsync.sh

+ host_01=192.168.0.132

+ src=/data/bakup/

+ rsync_passfile=/etc/rsyncd.password

+ inotify_home=/usr/local/inotify/

+ users=rsync_bakup

+ mode_01=bakup

+ '[' '!' -e /data/bakup/ ']'

+ '[' '!' -e /etc/rsyncd.password ']'

+ '[' '!' -e/usr/local/inotify//bin/inotifywait ']'

+ '[' '!' -e /usr/bin/rsync ']'

+ read file

+ /usr/local/inotify//bin/inotifywait -mrq--timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -eclose_write,create,access,attrib /data/bakup/

 

8、创建测试文件

[root@Kry103 ~]# cd  /data/bakup/

[root@Kry103 bakup]# for f in`seq 10`;do touch $f;done

 

9、查看数据是否实时同步

[root@kry132 bakup]# ll

total 0

-rw-------. 1 rsync rsync 0 Mar 28 17:06 1

-rw-------. 1 rsync rsync 0 Mar 28 17:06 10

-rw-------. 1 rsync rsync 0 Mar 28 17:06 2

-rw-------. 1 rsync rsync 0 Mar 28 17:06 3

-rw-------. 1 rsync rsync 0 Mar 28 17:06 4

-rw-------. 1 rsync rsync 0 Mar 28 17:06 5

-rw-------. 1 rsync rsync 0 Mar 28 17:06 6

-rw-------. 1 rsync rsync 0 Mar 28 17:06 7

-rw-------. 1 rsync rsync 0 Mar 28 17:06 8

-rw-------. 1 rsync rsync 0 Mar 28 17:06 9

 

10、脚本后台运行

[root@Kry103 ~]# nohup  /root/script/realtime_rsync.sh &

 

FAQ

1.Rsync Clinet 推送文件是报错

[root@Kry103 ~]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

rsync: failed to connect to 192.168.0.132: No route to host (113)

rsync error: error in socket IO (code 10)at clientserver.c(124) [sender=3.0.6]

原因:防火墙阻挡或者服务未启动;

解决办法:Rsync Server配置iptables允许对873链接,启动Rsync Server

 

2. Rsync Clinet 推送文件是报错

[root@Kry103 ~]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

password file must beowned by root when running as root

continuing without password file

原因:rsyncd.password文件属主组不为root

解决办法:修改属主组。chown root.root /etc/rsyncd.password

 

3. Rsync Clinet 推送文件是报错

[root@Kry103 ~]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

@ERROR: chrootfailed

原因:服务端数据目录不存在,就是模块中path指定的目录;

解决方法:Rsync Server创建数据目录。

 

4. Rsync Clinet 推送文件是错误

[root@Kry103 ~]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

sending incremental file list

test.txt

         11 100%    0.00kB/s   0:00:00 (xfer#1, to-check=0/1)

rsync: mkstemp"/.test.txt.zAUgyi" (in bakup) failed:Permission denied (13)

sent 80 bytes  received 27 bytes  214.00 bytes/sec

total size is 11  speedup is 0.10

rsync error: some files/attrs were nottransferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

原因:配置文件中指定uid用户(系统用户)不具操作数据目录权限。

解决办法:修改Rsync Server数据存放目录的属主组为配置文件中的uidgid

 

5. Rsync Clinet 推送文件是报错,文件成功推送到服务端(与问题4存在区别)

[root@Kry103 ~]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

sending incremental file list

test.txt

         11 100%    0.00kB/s    0:00:00 (xfer#1, to-check=0/1)

rsync: chgrp "/.test.txt.8vUp4J"(in bakup) failed: Operation not permitted (1)

sent 80 bytes  received 27 bytes  214.00 bytes/sec

原因:因为server端配置uid gid rsync的系统用户,而我在客户端同步一个所属主组都是root的用户,并且在同步的是否加入-a 归档参数,其中包括-o-g参数,同步数据到服务端是,rsync一般用户会将所属主组修改为root,

一般用户怎么能修改所属主组为root喃。所以就出现了数据能正常同步过去,但是文件或目录的权限和属主组信息并没有被保留。

解决方法:

.Rsync server配置文件中直接将uidgid修改为root

.将客户端需要上传的文件或目录的属主组修改为一般用户。

.同步数据取消保留用户所属主组信息。

 

6. Rsync Clinet 推送文件是报错

[root@Kry103 upbak]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

sending incremental file list

rsync: link_stat "/root/upbak/test.txt" failed:No such file or directory (2)

sent 14 bytes  received 8 bytes  44.00 bytes/sec

total size is 0  speedup is 0.00

rsync error: some files/attrs were nottransferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]

原因:被同步的文件或目录不存在;

解决办法:修改被同步文件或目录的路径。

 

7. Rsync Clinet 推送文件是报错

[root@Kry103 ~]# rsync -avzP test.txtrsync_bakup@192.168.0.132::bakup --password-file=/etc/rsyncd.password

rsync: failed to connect to 192.168.0.132: Connection refused (111)

rsync error: error in socket IO (code 10)at clientserver.c(124) [sender=3.0.6]

原因:防火墙阻止或服务没启动。

解决办法:Rsync Server配置iptables允许对873链接,启动Rsync Server