企业级别应用--rsync远程同步(配置 rsync 备份源、rsync 下行和上行、inotify+rsync 实时备份)

一、配置 rsync 源服务器

1.1 rsync简介

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

rsync 的官方站点的网址是 http://rsync.samba.org/,目前最新版本是 3.1.3,由 Wayne Davison 进行维护。作为一种最常用的文件备份工具,rsync 是 Linux 和 UNIX 系统默认安装的基本组件之一。

rsync不仅可以远程同步数据(类似于scp),而且可以本地同步数据(类似于cp),但不同于scp和cp的一点是,它不会覆盖以前的数据(如果数据已经存在),而是先判断已经存在的数据和新数据的差异,只有数据不同时才会把不同的部分覆盖。支持本地复制,或者与其他SSH、rsync主机同步。

1.2 rsync特点

  • 可以镜像保存整个目录树和文件系统。
  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。
  • 无须特殊权限即可安装。
  • 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。
  • 安全:可以使用scp、ssh等方式来传输文件,当然也可以通过直接的socket连接。
  • 支持匿名传输,以方便进行网站镜像。

在远程同步任务中,负责发起 rsync 同步操作的客户机称为发起端,而负责响应来自客 户机的 rsync 同步操作的服务器称为同步源。在同步过程中,同步源负责提供文件的原始位置,发起端应对该位置具有读取权限。

在这里插入图片描述

rsync 作为同步源时以守护进程运行,为其他客户机提供备份源。配置 rsync 同步源需 要建立配置文件 rsyncd.conf,创建备份账号,然后将 rsync 程序以守护进程(“–daemon”选项)方式运行。

1.3 配置 rsync 同步源

  1. 建立rsyncd.conf配置文件、独立的账号文件
  2. 启用rsync的–daemon模式
[root@localhost ~]# hostnamectl set-hostname source    ##将主机名改为source便于区分
[root@localhost ~]# su
[root@localhost ~]# iptables -F    ##关闭防火墙
[root@localhost ~]# setenforce 0   ##关闭核心防护
[root@localhost ~]# rpm -q rsync   ##查看rsync是否安装,属于Linux内核自带软件
rsync-3.1.2-4.el7.x86_64
[root@localhost ~]# rpm -qc rsync   ##查看rsync的配置文件位置
/etc/rsyncd.conf     ##主配置文件位置
/etc/sysconfig/rsyncd

修改配置文件rsyncd.conf

uid = nobody    ##设置用户
gid = nobody    ##设置用户组
use chroot = yes   ##禁锢家目录,就是在访问的过程中,会将访问的用户锁定在指定的目录,减少系统的安全风险
port 873     ##端口号
log file = /var/log/rsync.log     ##rsync的日志文件位置
pid file = /var/run/rsyncd.pid    ##rsync的进程id位置
hosts allow = 14.0.0.0/24         ##白名单,允许14.0.0.0/24网段的IP进行访问

[wwwroot]     ##自定义的共享模块,名字自取
path = /var/www/html     ##源目录路径,自己定义
comment = www.njit.com   
read only = yes          ##设置为只读,也就是只允许下载
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2    ##同步时对这几种格式的文件不进行压缩
auth users = sx     ##授权的账户名
secrets file = /etc/rsync_users.db     ##授权的账户密码文件的位置,需要手动建立
  • 认证配置auth users、secrets file,不加则为匿名
  • rsync账号文件
  • 采用“用户名:密码”的记录格式,每行一个用户记录
  • 独立的账号数据,不依赖于系统账号
  • 启用rsync服务
  • 通过–daemon独自提供服务
[root@source ~]# vim /etc/rsync_users.db     ##建立一个授权数据文件
sx:123456
[root@source ~]# cd /etc/
[root@source etc]# ll | grep rsync_users.db
-rw-r--r--.  1 root root       10 9  11 22:22 rsync_users.db
文件的权限为644,还是会被别人给读到,所以降低权限,保证安全性
[root@source etc]# chmod 600 rsync_users.db
[root@source etc]# rsync --daemon    ##开启服务
[root@source etc]# netstat -ntap | grep rsync    ##检查服务开启状态
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      16860/rsync         
tcp6       0      0 :::873                  :::*                    LISTEN      16860/rsync         

在这里插入图片描述
如果我们想要关闭服务,找到rsync服务的pid文件,查看进程号,直接kill掉就可以。

[root@source etc]# cd /var/run/
[root@source run]# cat rsyncd.pid     ##查看rsync服务的进程号
16860
[root@source run]# kill 16860         ##杀死进程,关闭服务
[root@source run]# netstat -ntap | grep rsync   ##这时候就查看不到端口了

安装httpd服务,并在站点目录写入一个首页文件,为了有apache的站点目录,便于测试用

[root@source run]# yum install httpd -y
[root@source run]# cd /var/www/html/
[root@source html]# vim index.html
[root@source html]# echo "<h1>this is test web</h1>" > index.html

1.4 测试rsync功能

测试的时候需要开启另外一台虚拟机,这台虚拟机的IP地址应该与之前rsync配置文件中写的在一个网段,否则不能同步。
rsync的命令

  • -r:递归模式,包含目录及子目录中的所有文件。
  • -l:对于符号链接文件仍然复制为符号链接文件。
  • -v:显示同步过程的详细(verbose)信息。
  • -a:归档模式,保留文件的权限、属性等信息,等同于组合选项“-rlptgoD”。
  • -z:在传输文件时进行压缩(compress)。
  • -p:保留文件的权限标记。
  • -t:保留文件的时间标记。
  • -g:保留文件的属组标记(仅超级用户使用)。
  • -o:保留文件的属主标记(仅超级用户使用)。
  • -H:保留硬连接文件。
  • -A:保留 ACL 属性信息。
  • -D:保留设备文件及其他特殊文件。
  • –delete:删除目标位置有而原始位置没有的文件。
  • –checksum:根据校验和(而不是文件大小、修改时间)来决定是否跳过文件。

下行同步的三种方式

方式一:

[root@localhost ~]# iptables -F
[root@localhost ~]# setenforce 0
[root@localhost ~]# rsync -zva sx@14.0.0.110::wwwroot /opt/   ##将wwwroot模块中写明的条目同步到/opt下
Password:    ##输入密码
receiving incremental file list
./
index.html

sent 46 bytes  received 133 bytes  51.14 bytes/sec
total size is 26  speedup is 0.15
[root@localhost ~]# cd /opt
[root@localhost opt]# ls
index.html  rh
[root@localhost opt]# cat index.html 
<h1>this is test web</h1>

方式二:

在源服务器上新建一个文件
[root@source html]# echo "<h1>this is yy web</h1>" > new.html
[root@source html]# ls
index.html  new.html
[root@source html]# cat new.html 
<h1>this is yy web</h1>

在另一台服务器上进行同步

[root@localhost opt]# rsync -avz rsync://sx@14.0.0.110/wwwroot /opt/
Password: 
receiving incremental file list
./
new.html

sent 46 bytes  received 159 bytes  58.57 bytes/sec
total size is 50  speedup is 0.24
[root@localhost opt]# ls
index.html  new.html  rh
[root@localhost opt]# cat new.html 
<h1>this is yy web</h1>

方式三:
在目标服务器上

[root@localhost opt]# rm -rf *.html
[root@localhost opt]# ls
rh
[root@localhost opt]# vim /etc/server.pass
123456     ##写入授权用户的密码
[root@localhost opt]# chmod 600 /etc/server.pass
[root@localhost opt]# rsync -avz --delete --password-file=/etc/server.pass sx@14.0.0.110::wwwroot /opt/     ##免交互式同步,--delete表示删除目标位置有而原始位置没有的文件,--password-file指明刚刚写的密码文件的位置
receiving incremental file list
deleting rh/
./
index.html
new.html

sent 65 bytes  received 222 bytes  27.33 bytes/sec
total size is 50  speedup is 0.17
[root@localhost opt]# ls    ##删除了原来的rh目录,因为源服务器的目录下没有这个目录
index.html  new.html
[root@localhost opt]# cat index.html 
<h1>this is test web</h1>
[root@localhost opt]# cat new.html 
<h1>this is yy web</h1>

二、inotify+rsync 实时同步

Linux 内核从 2.6.13 版本开始提供了 inotify 通知接口,用来监控文件系统的各种变化情
况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、增量备份,并针对目录或文件的变化及时作出响应。

将 inotify 机制与 rsync 工具相结合,可以实现触发式备份(实时同步)——只要原始位 置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态。这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题。

在这里插入图片描述

2.1 配置 inotify+rsync 实时同步的步骤

  1. 调整 inotify 内核参数
    在 Linux 内核中,默认的 inotify 机制提供了三个调控参数:max_queue_events、 max_user_instances、max_user_watches,分别表示监控事件队列(16 384)、最多监控实例数(128)、每个实例最多监控文件数(8192)。

  2. 安装 inotify-tools
    使用 inotify 机制还需要安装 inotify-tools,以便提供 inotifywait、inotifywatch 辅助工具程序 ,用来监控、汇总改动情况 。 inotify-tools 可从网站 https://github.com/rvoicilas/inotify-tools/releases 下载,版本为 3.14。

  3. 编写触发式同步脚本
    使用 inotifywait 输出的监控结果中,每行记录中依次包括目录、事件、文件,据此可以 识别变动情况。为了简单,只要检测到变动时执行 rsync 上行同步操作即可。需要注意的是, 当更新较频繁时,应避免并发执行 rsync 备份——若 rsync 进程已经存在,则忽略本次同步, 或者根据 rsync 进程数量(取决于实际任务)来决定是否同步。

  4. 检测inotify+rsync 实时同步
    在本机运行/opt/inotify_rsync.sh 脚本程序。
    切换到本机的/var/www/html 目录,执行增加、删除、修改文件等操作。
    查看服务器中的/var/www/html 目录下的变化情况。

2.2 inotify+rsync 实时同步实验

1. 实验之前将之前写在站点的文件全部删除

源服务器
[root@source opt]# cd /var/www/html/
[root@source html]# ls
index.html  new.html
[root@source html]# rm -rf *
[root@source html]# ls
inotify+rsync发起端
[root@localhost opt]# yum install httpd -y
[root@localhost opt]# cd /var/www/html/
[root@localhost html]# ls   ##保证这个目录下为空,便于实现实时同步
[root@localhost html]# vim /etc/sysctl.conf 
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. 手工编译安装inotify

将软件包拷贝到当前目录下
[root@localhost html]# tar zvxf inotify-tools-3.14.tar.gz -C /opt
[root@localhost html]# yum install gcc gcc-c++ make -y   ##安装环境包
[root@localhost html]# cd /opt/inotify-tools-3.14/
[root@localhost inotify-tools-3.14]# ./configure     ##configure配置
[root@localhost inotify-tools-3.14]# make && make install   ##编译安装
[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/    ##让这个服务器端进入一个持续性监控的状态,一旦进行了文件的更改、创建、移动、删除,就会触发实时同步

3. 对持续性监控的测试

对inotify+rsync发起端服务器重新开启一个窗口,进行操作

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# touch test.txt
[root@localhost html]# echo "hahaha" > test.txt 
[root@localhost html]# rm -rf test.txt 

在持续型监控的窗口可以看到以下的信息

[root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ CREATE test.txt
/var/www/html/ MODIFY test.txt
/var/www/html/ DELETE test.txt

4. 编写脚本实现两台服务器的实时同步

inotify+rsync发起端服务器
[root@localhost html]# cd /opt
[root@localhost opt]# vim inotify.sh
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e create,delete,move,modify,attrib,move,delete /var/www/html/"
RSYNC_CMD="rsync -azH --delete --password-file=/etc/server.pass /var/www/html/ sx@14.0.0.110::wwwroot/"    ##将自己本地/var/www/html/的文件同步到14.0.0.110服务器rsync配置文件的指定目录下

$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done
[root@localhost opt]# chmod +x inotify.sh

因为脚本中牵扯着文件的上传,所以要修改配置文件中的read only记录,将yes改为no

在源服务器
[root@source html]# vim /etc/rsyncd.conf 
read only = no
[root@source html]# chmod 777 /var/www/html/    ##提升目录权限,防止同步时候目录权限不够导致失败
[root@source html]# ls    ##现在这个目录下为空,没有文件,一会执行脚本后,查看文件有没有实时同步过来

在这里插入图片描述

inotify+rsync发起端服务器
[root@localhost opt]# ls /var/www/html/
inotify-tools-3.14.tar.gz
[root@localhost opt]# ./inotify.sh    ##执行脚本,进入持续性监听状态

但是这个时候在源服务器端/var/www/html目录下仍然没有将文件同步过来,这是因为我们没有对这个目录下的文件进行操作,没有触发实时同步,这时候我们在这个目录下创建一个文件,触发实时同步

重新打开一个终端进行操作
[root@localhost html]# touch home.txt
[root@localhost html]# echo "实验成功" > home.txt

在这里插入图片描述
这时候在源服务器的 /var/www/html/目录下进行查看

[root@source html]# ls    ##文件都被同步过来
home.txt  inotify-tools-3.14.tar.gz
[root@source html]# cat home.txt 
实验成功

查看同步文件的属主 、属组,都为nobody

[root@source html]# ll
总用量 352
-rw-------. 1 nobody nobody      0 9  12 18:56 home.txt
-rw-------. 1 nobody nobody 358772 9  12 18:56 inotify-tools-3.14.tar.gz

属主和属组为nobody的原因是因为在之前rsync的配置文件中写的

[root@source html]# vim /etc/rsyncd.conf

在这里插入图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值