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服务的模式

  • ssh方式进行同步

  • C/S 方式,rsync有服务器端daemon模块和rsync客户端

1.3 rsync特点

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

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

在这里插入图片描述

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

1.4 rsync 命令及其参数

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

二、搭建rsync服务

2.1 案例环境

服务类型IP地址所需服务
Rsync同步源10.0.0.73rsync、httpd
客户机10.0.0.74rsync、httpd

2.2 案例部署

  • 同步源服务器
# 最小化安装系统,因此需要安装rsync
[root@rsync ~]# yum -y install httpd rsync		

# 修改配置文件
[root@rsync ~]# vi /etc/rsyncd.conf
uid = nobody                      #启用匿名用户
gid = nobody
port 873                          #监听端口
use chroot = yes                  #禁固在目录
log file = /var/log/rsyncd.log    #日志文件存放位置
pid file = /var/run/rsyncd.pid    #存放进程ID的文件位置
hosts allow = 10.0.0.0/24         #允许访问的客户机地址

#共享模块
[wwwroot]                         #共享模块名称
path = /var/www/html              #源目录的实际路径
comment = www.shuai.com           #描述(可以省略)
read only = yes                   #开启只读
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2	#不压缩的文件类型
auth users = backuper             #同步时不在进行也锁的文件类型
secrets file = /etc/rsyncd_users.db #存放账户信息的数据文件

# 创建账号数据文件
[root@rsync ~]# vi /etc/rsyncd_users.db
backuper:qwe123		#采用“用户名:密码”的记录格式,每行一个用户记录

# 由于账号信息采用明文存放,因此需要调整那文件权限,避免账号信息泄露
[root@rsync ~]# chmod 600 /etc/rsyncd_users.db 

# 启动服务
[root@rsync ~]# netstat -anpt | grep rsync
tcp        0      0 10.0.0.73:873           0.0.0.0:*               LISTEN      1280/rsync

# 设置apache主页信息
[root@rsync ~]# echo "Test-1" > /var/www/html/index.html

# 重启rsync服务
[root@rsync ~]# kill $(cat /var/run/rsyncd.pid)
[root@rsync ~]# rsync --daemon
[root@rsync ~]# netstat -anpt | grep rsync
tcp        0      0 10.0.0.73:873           0.0.0.0:*               LISTEN      1304/rsyn

2.3 rsync同步测试(三种方法)

  • 方法一
[root@client ~]# rsync -avz backuper@10.0.0.73::wwwroot /opt/
Password: 
receiving incremental file list
./
index.html

sent 83 bytes  received 163 bytes  25.89 bytes/sec
total size is 7  speedup is 0.03
[root@client ~]# ll /opt
total 4
-rw-r--r--. 1 root root 7 Nov 12 11:51 index.html
  • 方法二
[root@client ~]# rm -rf /opt/index.html 
[root@client ~]# rsync -avz rsync://backuper@10.0.0.73/wwwroot /opt/
Password: 
receiving incremental file list
./
index.html

sent 83 bytes  received 163 bytes  32.80 bytes/sec
total size is 7  speedup is 0.03
[root@client ~]# cat /opt/index.html 
Test-1

  • 方法三(免交互)
[root@client ~]# rm -rf /opt/index.html 
[root@client ~]# vi /etc/server.txt
[root@client ~]# chmod 600 /etc/server.txt
qwe123
[root@client ~]# rsync -zva --delete --password-file=/etc/server.txt backuper@10.0.0.73::wwwroot /opt
receiving incremental file list
./
index.html

sent 83 bytes  received 163 bytes  164.00 bytes/sec
total size is 7  speedup is 0.03
[root@client ~]# cat /opt/index.html 
Test-1

2.4 设置定期同步

  • 每天晚上十一点对服务器网站更新一次
[root@client ~]# mkdir /web
[root@client ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@client ~]# systemctl restart crond
[root@client ~]# systemctl enable crond
* 23 * * * /usr/bin/rsync -avz --delete --password-file=/etc/server.txt backuper@10.0.0.73::wwwroot /web

三、inotify + rsync 监控实时同步

3.1 简介及原理

  • 简介

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

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

  • 原理

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-i8B4mf6D-1605165232487)(C:\Users\Chu\AppData\Roaming\Typora\typora-user-images\image-20201112122930757.png)]

3.2 配置 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 目录下的变化情况。

四、搭建 inotify + rsync

4.1 案例环境

服务类型IP地址所需服务
Rsync同步源10.0.0.73rsync、httpd、inotify
Inotify+Rsync发起端服务器10.0.0.74rsync、inotify

4.2 案例部署

  • inotify+rsync发起端服务器
# 配置内核参数
[root@client ~]# vi /etc/sysctl.conf 
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@rsync ~]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

# 安装inotifi-tools
[root@client ~]# tar zxf inotify-tools-3.14.tar.gz 
[root@client ~]# cd inotify-tools-3.14
[root@client inotify-tools-3.14]# yum -y install gcc gcc-c++ make
[root@client inotify-tools-3.14]# ./configure
[root@client inotify-tools-3.14]# make && make install

# 对持续性监控的测试
[root@client ~]# inotifywait -mrq -e modify,create,move,delete	#让这个服务器端进入一个持续性监控的状态,一旦进行了文件的更改、创建、移动、删除,就会触发实时同步
# -m:持续进行监控  -r:递归监控所有子对象  -q:简述输出信息 -e:指定要监控的事件类型 

# 新开一个窗口进行操作
[root@client html]# touch test.txt
[root@client html]# echo "test" > test.txt 
[root@client html]# rm -rf test.txt 

# 监控窗口信息如下
[root@client ~]# cd /var/www/html/
[root@client ~]# 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

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

  • rsysnc源服务器
修改配置文件中的read only记录,将yes改为no
[root@rsync ~]# vi /etc/rsyncd.conf 
read only = no
[root@rsync ~]# pkill -9 rsync
[root@rsync ~]# rm -rf /var/run/rsyncd.pid
[root@rsync ~]# rsync --daemon
[root@rsync ~]# chmod 777 /var/www/html/
  • inotify+rsync发起端服务器
[root@client ~]# vi /opt/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.txt /var/www/html/ backuper@10.0.0.73::wwwroot/"    #将自己本地/var/www/html/的文件同步到10.0.0.73服务器rsync配置文件的指定目录下

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

# 重新打开一个终端
[root@client ~]# touch /var/www/html/test.txt
[root@client ~]# echo "a" > /var/www/html/test.txt

# 原来终端
[root@client ~]# /opt/inotify.sh 
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]

  • rsysnc源服务器
[root@rsync ~]# cat /var/www/html/test.txt 
a
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值