码迷,mamicode.com
首页 > 其他好文 > 详细

在CentOS7上配置rsync源服务器+inotify实时同步

时间:2018-07-27 14:50:37      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:注意   验证   size   客户   数据   实时同步   上传   -o   enforce   

概述

rsync是一个开源的快速备份工具,可以再不同主机之间镜像同步整个目录树,支持增量备份,保持链接和权限,且采用优化的同步算法,再传输前执行压缩,因此非常适用于异地备份、镜像服务器等应用。

原理

再远程同步任务中,负责发起rsync同步操作的客户机称为发起端,而负责响应来自客户机的rsync同步操作的服务器称为同步源。再同步过程中,同步源负责提供文档的原始位置,而发起端对该位置具有读取权限,如图所示:
技术分享图片

配置rsync源服务器

1.检查rsync是否安装

[root@localhost ~]# rpm -q rsync
rsync-3.0.9-18.el7.x86_64

2.修改rsync默认配置文件,位于/etc/rsyncd.conf。

插入以下内容
uid = nobody
 gid = nobody
 use chroot = yes        //禁锢在源目录//
 address = 192.168.126.138    //监听地址//
 port 873         //监听端口//
 log file = /var/log/rsyncd.log       //日志文件位置//
 pid file = /var/run/rsyncd.pid      //存放进程ID的文件位置//
 hosts allow = 192.168.126.0/24     //允许访问的客户机地址//
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
 [wwwroot]                //共享模块名称//
 path = /var/www/html    //源目录的实际路径//
 read only = no         //是否为只读//
 dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  //同步时不再压缩的文件类型//
 auth users = backuper         //授权账户//
 secrets file = /etc/rsyncd_users.db     //存放账户信息的数据文件//

3.为备份账户创建数据文件

根据上一步的设置,创建账号数据文件,添加一行用户记录,以冒号分隔,用户名称为backup,密码为abc123。由于账号信息采取明文存放,因此应调整文件权限,避免账号信息泄露。

[root@localhost ~]#vim /etc/rsyncd_users.db
             backuper:abc123
[root@localhost ~]#chmod 600 /etc/rsyncd_users.db

4.开启rsync服务,运行参数为 --daemon。

[root@localhost opt]# systemctl stop firewalld.service
[root@localhost opt]# setenforce 0
[root@localhost opt]# rsync --daemon
[root@localhost opt]# netstat -ntap | grep rsync
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      2934/rsync   
使用rsync备份工具

配置源的方法:

在执行运程同步任务时,rsync命令需要指定同步源服务器中的资源位置。rsync同步源的资源表示方式为“用户名@主机地址::共享模块名”或者“rsync://用户名@主机地址/共享模块名”,前者为两个冒号分隔形式,后者为URL地址形式。

rsync -avz backuper@192.168.175.129::wwwroot /opt/  
rsync -avz rsync://backuper@192.168.175.129/wwwroot /opt/

1.执行以下操作将源服务器中的wwwroot共享模块,下载到客户机的本地/var/www/html目录下。

源服务器:
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "123" > 111.txt
[root@localhost html]# echo "456" > 222.txt
[root@localhost html]# ls
111.txt  222.txt

客户端:
[root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./   //下载到当前目录//
Password: 
receiving incremental file list
./
111.txt
222.txt

sent 102 bytes  received 221 bytes  23.93 bytes/sec
total size is 8  speedup is 0.02
[root@localhost html]# ls
111.txt  222.txt

2.在客户端上传文件到源服务器

[root@localhost opt]#mkdir b1 b2 b3 b4     
[root@localhost opt]# ls
b1  b2  b3  b4  b5  rh
[root@localhost opt]# rsync -avz backuper@192.168.126.138::wwwroot/ ./   #上传
Password: 
receiving incremental file list
./
111.txt
222.txt

sent 102 bytes  received 221 bytes  23.93 bytes/sec
total size is 8  speedup is 0.02            #上传成功

源服务器上查看:
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
b1  b2  b3  b4  b5  rh
注意:
上传前需要把源服务器rsync的配置文件rsyncd.conf中的uid、gid修改为root
配置rsync+inotify实施同步

将rsync工具与inotify机制相结合,可以实现触发式备份(实时同步)——只要原始位置的文档发生变化,就立即启动增量备份操作,如图所示,否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。
技术分享图片
正因为inotify通知机制由Linux内核提供,因此要做本机监控,在触发式备份中应用时更适合上行同步。下面一次介绍其配置过程。

1.调整inotify内核参数

当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值。可以直接修改/etc/sysctl.conf的配置文件,将管理队列、实例数、监控数进行设置。

[root@localhost html]# vim /etc/sysctl.conf
# For more information, see sysctl.conf(5) and sysctl.d(5).   //添加//
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@localhost html]# sysctl -p    //启动//
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

2.安装inotifi-tools

使用inotify机制还需要安装inotifi-tools,以便提供inotifywait和inotifywatch辅助工具程序,用来监控和汇总改动情况。

[root@localhost rs]# tar zxvf inotify-tools-3.14.tar.gz -C /opt/  #解包
[root@localhost opt]# cd inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# yum install gcc gcc-c++ make -y  #安装编译软件
[root@localhost inotify-tools-3.14]# ./configure     
[root@localhost inotify-tools-3.14]#make && make install

3.编写触发式同步脚本

root@localhost opt]# vim inotify.sh 

#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ backuper@192.168.126.138::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done

上述脚本用来检测本机/var/www/html目录的变动情况,一旦有更新触发rsync同步操作,上传备份至服务器192.168.126.138的/var/www/html目录下。

4.验证

1).在源服务器运行inotifywait -mrq -e modify,create,move,delete /var/www/html/

[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/ //静默等待状态//

2)打开源服务器的另一个窗口在/var/www/html目录下创建新的文件

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# echo "this is 111" > 111.txt    //写入文件//
[root@localhost html]# echo "this is 222" > 222.txt
[root@localhost html]# rm -rf 111.txt   //删除文件//

3)查看源服务器的更新触发状态

[root@localhost html]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE 111.txt         
/var/www/html/ MODIFY 111.txt
/var/www/html/ CREATE 222.txt
/var/www/html/ MODIFY 222.txt
/var/www/html/ DELETE 111.txt      
                              //监控端已有反馈//

4)查看服务器中的/var/www/html目录下的变化情况

[root@localhost html]# ls
111.txt  222.txt
[root@localhost html]# ls
222.txt

实验成功

在CentOS7上配置rsync源服务器+inotify实时同步

标签:注意   验证   size   客户   数据   实时同步   上传   -o   enforce   

原文地址:http://blog.51cto.com/13642258/2151010

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!