一、环境centos6.5,已关闭防火墙及selinux

二、serviceA:192.168.159.132

   serviceB:192.168.159.131

要求:serviceA上的/mnt下的数据能实时同步到serviceB上的/backup文件夹

三、实现步骤:

1、服务器A上配置

 1.1、查看是否安装rsync

[root@serviceA mnt]# rpm -qa | grep rsync

rsync-3.0.6-12.el6.x86_64

 1.2、安装xinetd

[root@serviceA mnt]# yum install xinetd

 1.3、修改rsync.conf参数修改disable=no使之可用

[root@serviceA mnt]# vi /etc/xinetd.d/rsync

 1.4、启动xinetd服务

[root@serviceA mnt]# service xinetd start

 1.5、同步/mnt上的数据到serviceB上的/backup文件夹

[root@serviceA mnt]# rsync -azvP /mnt [email protected]:/backup/

 1.6、实现ssh无交互连接传输(不用输入密码)

[root@serviceA mnt]# ssh-keygen

[root@serviceA mnt]# ssh-copy-id [email protected]

[root@serviceA mnt]# rsync -azvP /mnt [email protected]:/backup/

2、以上的方案需要ssh的用户名和密码,存在着很大的风险,因此我们通过部署

  rsync服务器的方式实现同步

 2.1、创建rsyncd.conf

[root@serviceA mnt]# vi /etc/rsyncd.conf 

内容添加如下:

uid = rsync

gid = rsync

use chroot =no

max connections = 200

timeout = 300

pid file = /var/run/rsyncd.pid

lock file = /var/run/rsync.lock

log file = /var/log/rsyncd.log

[mnt]

path = /mnt

ignore errors

read only = false

list = false

hosts allow = 192.168.159.0/24

hosts deny = *

auth users = rsync_backup

secrets file = /etc/rsync.password


 2.2、配置虚拟用户的用户名和密码

[root@serviceA mnt]# echo "rsync_backup:ok" > /etc/rsync.password 

[root@serviceA mnt]# chmod 600 /etc/rsync.password #调整密码文件权限


3、serviceB配置

 3.1、新建rsync.password文件并导入密码

[root@serviceB ~]# echo "ok" >/etc/rsync.password

 3.2、同步数据到serviceB上的/backup文件夹

[root@serviceB ~]# rsync -avzP [email protected]::mnt --password-file=/etc/rsync.password  /backup/

 3.3、不用跟密码文件直接运行

[root@serviceB ~]# export RSYNC_PASSWORD=ok

[root@serviceB ~]# rsync -avzP [email protected]::mnt /backup/


4、serviceA上配置inotify

 4.1、查看内核是否直接inotify

[root@serviceA mnt]# uname -r #2.6.13之后的内核都支持inotify

或查看文件夹时候有一下文件确定时候支持inotify

[root@serviceA mnt]# cd /proc/sys/fs/inotify/

[root@serviceA inotify]# ll !$

ll /proc/sys/fs/inotify/

total 0

-rw-r--r-- 1 root root 0 Feb  7 02:13 max_queued_events

-rw-r--r-- 1 root root 0 Feb  7 02:13 max_user_instances

-rw-r--r-- 1 root root 0 Feb  7 02:13 max_user_watches

因以上三个文件默认的值比较小,如果业务量比较大的话需要增加,步骤如下

[root@serviceA inotify]# vim /etc/sysctl.conf

添加如下内容:

fs.inotify.max_queued_events = 32768

fs.inotify.max_user_instances = 1024

fs.inotify.max_user_watches = 90000000

然后sysctl -p使之生效

[root@serviceA inotify]# sysctl -p

 4.2、安装inotify-tools-3.13.tar.gz,解压、安装

[root@serviceA ~]# tar zxvf inotify-tools-3.13.tar.gz 

[root@serviceA mnt]# cd inotify-tools-3.13

[root@serviceA inotify-tools-3.13]# ./configure 

[root@serviceA inotify-tools-3.13]# make && make install

 4.3、通过inotifywait命令实时监控,并通过复制会话,增添删改回还信息。

[root@serviceA inotify-tools-3.13]# inotifywait -mrq -e create,move,delete,modify /mnt

/mnt/ DELETE,ISDIR test

/mnt/test/ IGNORED 

/mnt/ CREATE ceshi

5、通过脚本实时文件同步

[root@serviceA ~]# vi inotify_rsync.sh

脚本如下:

#!/bin/bash

inotifywait -mrq -e create,move,delete,modify /mnt | while read A B C

do 

rsync -azvP /mnt [email protected]:/backup/

done

在/mnt文件下新建heheheeh文件后执行脚本如下显示。

[root@serviceA ~]# sh inotify_rsync.sh 

sending incremental file list

mnt/

mnt/ceshi

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

mnt/heheheeh

           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=101/104)


sent 2380 bytes  received 67 bytes  1631.33 bytes/sec

total size is 3799430  speedup is 1552.69