rsync+inotify实现数据同步

Rsync原理:

    Rsync是基于rsync算法校验源(SRC)与目标(DEST)之间的差异实现数据同步的。也就是说,当使用rsync工具同步数据的时候,只复制源发生改变的文件,到目标(DEST),这类似于增量备份。所以rsync同步数据的速度是很快的。但是rsync的传输性能很差,在海量小文件需要同步时,表现很不好。因为rsync是基于文件同步的(不是块级别的),在传输之前还要进行校验。

    Rsync的工作模式有多种,使用rsync的服务模式来构建:主/从服务器的数据同步。此时rsync工作为守护进程监听在:TCP/873,能收到客户端的数据同步请求。

    

inotify原理:

   inotify可以监控文件,也可以监控目录。当监控目录时,它可以同时监控目录及目录中的各子目录及文件的变化。由于intify使用文件扫描符作为借口,因此可以使用通常的文件I/O操作例如select、pool、epoll来见识文件系统的变化


环境需求:

Host1:

  CentOS-7-x86_64

  hostname:ws1

  ipaddr:10.0.0.61

Host2:

   CentOS-6-x86_64

   hostname:ws2

   ipaddr:10.0.0.62

注意:两台主机均为最小化安装iptables和selinux均处于关闭状态


配置Host1


CentOS 7最小化安装,默认已安装rsync包

wKioL1dH7c3iLlbFAAAlVMrP6iI834.png


编辑rsync的配置文件

# vim /etc/rsyncd.conf

uid = root          //rsync同步共享用户的身份
gid = root          //rsync同步共享用户组的身份
use chroot = no         //不使用chroot
port = 873          //定义监听的端口,默认873
max connections = 200   //服务器最大并发数
timeout = 300          //会话超时时长
pid file = /var/run/rsyncd.pid    //pid文件存放路径
lock file = /var/run/rsyncd.lock    //lock文件存放路径,服务启动停止时会用到
log file = /var/log/rsyncd.log    //日志文件存放路径
log format = %h %o %1 %b      //日志记录格式
ignore errors              //忽略错误信息
read only = false           //是否允许用户上传文件,默认为true
list = false              //是否允许用户列出文件,默认为true
write only = false           //是否允许下载,默认为true
hosts allow = 10.0.0.61/8       //允许哪些主机访问
hosts deny = *   s         //拒绝哪些主机访问
auth users = sync            //同步时认证所需的用户
secrets file = /etc/rsyncd.passwd    //认证时所需的密码文件

[data]    //模块名称,客户端需精确指定模块名称
path = /data     //模块对应的目录路径


创建认证时的密码文件/etc/rsyncd.passwd(需要用户名和密码、明文存储,仅属主可读)

# echo "sync:123456" > /etc/rsynd.passwd
# chmod 600 /etc/rsynd.passwd


创建模块对应的目录,并确保属主和属组与配置文件中定义的uid和gid一致

# mkdir -p /data/
# ls -ld /data/
drwxr-xr-x 2 root root 21 5月  27 22:47 /data/
# cp /var/log/message /data/


启动服务,并检查进程和监听端口

# systemctl start rsyncd.service
# ps aux | grep rsync
root       3121  0.0  0.1 114640  1164 ?        Ss   22:47   0:00 /usr/bin/rsync --daemon --no-detach
root       3123  0.0  0.0 112660   956 pts/0    R+   22:48   0:00 grep --color rsync
# ss -tunl | grep 873
tcp    LISTEN     0      5         *:873                   *:*                  
tcp    LISTEN     0      5        :::873                  :::*


配置host2

创建认证时的密码文件~/rsyncd。passwd(仅需密码,明文存储、仅属主可读)

# echo "123456" > rsyncd.passwd
# chmod 600 rsyncd.passwd


测试查看rsync服务器上data模块中的文件

]# rsync --password-file=rsyncd.passwd [email protected]::data
drwxr-xr-x          21 2016/05/27 22:47:34 .
-rw-------      242668 2016/05/27 22:47:35 messages

注意:再一次踩坑,一定一定一定检查防火墙


建立本地数据目录文件/data,将/data目录与rsync服务器上data模块中定义的目录/data作为数据同步目录,即这两个目录中的数据要时刻保持一致。


测试:将host1的/data目录中的数据取过来。

# rsync -avz --password-file=rsyncd.passwd [email protected]::data /data/
receiving incremental file list
./
messages

sent 76 bytes  received 26839 bytes  53830.00 bytes/sec
total size is 242668  speedup is 9.02


然后在本地创建几个文件使用rsync命令同步到Host1的data目录中

# rsync -avz --password-file=rsyncd.passwd /data/ [email protected]::data

sending incremental file list
./
rsyncd.passwd

sent 2567 bytes  received 34 bytes  5202.00 bytes/sec
total size is 30649078  speedup is 11783.57


注意:目录后面的/ 如果少加了一个/,会将data目录本身而不是目录下的文件同步过去


# rm -f /data/fstab
# rsync -avz --delete --password-file=rsyncd.passwd /data/ [email protected]::data
sending incremental file list
deleting fstab

sent 2521 bytes  received 12 bytes  5066.00 bytes/sec
total size is 30649078  speedup is 12099.91

此时手动同步两个节点上目录中数据文件是没有问题了,现在需要实现的是Host2上的数据一改变立刻同步Host1上的数据,这就需要用到inotify了


inptify


inotify是内核中功能,但是需要通过系统调用接口程序才能够使用,这个接口程序由inotify-tools提供,所以嘛咱得安装!!!此处我们在host2上安装

# yum install -y inotify-tools
# # rpm -ql inotify-tools
/usr/bin/inotifywait
/usr/bin/inotifywatch
/usr/lib64/libinotifytools.so.0
/usr/lib64/libinotifytools.so.0.4.1


以下是几个inotify相关的内核参数,可能根据实际需要调整

# ls -l /proc/sys/fs/inotify/
总用量 0
-rw-r--r-- 1 root root 0 5月  28 00:23 max_queued_events
-rw-r--r-- 1 root root 0 5月  28 00:23 max_user_instances
-rw-r--r-- 1 root root 0 5月  28 00:23 max_user_watches


实时监控数据目录文件,发生改变立刻调用rsync进行数据同步,脚本如下

# vim auto_rsync.sh
#!/bin/bash

sync_user="sync"
sync_host="10.0.0.61"
sync_mod="data"
data_dir="/data/"

rsync_cmd="rsync -vzrtopg --delete --progress --password-file=./rsyncd.passwd $data_dir $sync_user@$sync_host::$sync_mod"

inotifywait -mrq $data_dir --timefmt '%d/%m/%y %H:%M' --format '%T %w%f%e' -e modify,delete,create,attrib | while read files;
do
    $rsync_cmd
done


启动脚本,然后修改Host2 数据目录中的文件,观察变化

bash auto_rsync.sh 
sending incremental file list
./
deleting issue
deleting .auto_rsync.sh.swp
auto_rsync.sh
         359 100%    0.00kB/s    0:00:00 (xfer#1, to-check=108/110)

sent 2800 bytes  received 34 bytes  5668.00 bytes/sec
total size is 30649390  speedup is 10814.89







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章