CentOS 7.6 rsync远程同步实例部署

一、场景

正确、有效的备份方案是保障系统及数据安全的重要手段。在服务器中,通常会结合计划任务、Shell 脚本来执行本地备份。为了进一步提高备份的可靠性,使用异地备份也是非常有必要的。 本文章将要演示 rsync 工具的使用,以实现快速、安全、高效的异地备份,如针对 Web 站点的同步备份

二、概述

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

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

三、发起端与同步源

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

四、实验实例

1、环境准备

准备两台虚拟机,一个作为rsync客户端(发起端),一个作为rsync服务器(同步源)

rsync:192.168.245.205
client:192.168.245.206

一般来说,rsync是系统安装时就默认安装好的,不需要单独再进行安装,非常方便

[root@rsync ~]# rpm -q rsync
rsync-3.1.2-4.el7.x86_64

默认的配置文件/etc/rsyncd.conf

[root@rsync ~]# rpm -qc rsync
/etc/rsyncd.conf   <----默认配置文件
/etc/sysconfig/rsyncd

2、修改配置文件

[root@rsync ~]# vim /etc/rsyncd.conf


# /etc/rsyncd: configuration file for rsync daemon mode

# See rsyncd.conf man page for more options.

# configuration example:

uid = nobody
gid = nobody
use chroot = yes   <----是否禁锢在家目录
# max connections = 4
pid file = /var/run/rsyncd.pid   <----pid文件位置
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2  <----这些后缀名结尾的文件同步时不进行压缩
address = 192.168.245.205    <----监听地址(本机地址)
port = 873   <----默认监听端口
log file = /var/log/rsyncd.log   <----日志文件位置
hosts allow = 192.168.245.0/24    <----白名单,只允许这个网段的主机进行远程同步

[wwwroot]  <----共享模块名称(自定义)
path = /var/www/html   <----共享模块的实际路径
comment = www.yjs.cn   <----说明
read only = yes    <----只读
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   
auth users = backuper   <----认证用户名,表示用该用户进行同步操作
secrets file = /etc/rsyncd_users.db   <----认证用户的密码文件

基于安全性考虑,对于 rsync 的同步源最好仅允许以只读方式做同步。另外,同步可以采用匿名的方式,只要将其中的“auth users”和“secrets file”配置记录去掉就可以了

3、添加密码文件

创建认证用户的密码文件,在文件里写入用户名和密码,用户名和密码直接用冒号分隔

[root@rsync ~]# vim /etc/rsyncd_users.db

backuper:abc123

出于安全性考虑,该密码文件不允许别人看,设置600权限

[root@rsync ~]# chmod 600 /etc/rsyncd_users.db 

4、启动rsync

以守护进程的方式运行rsync,如果要关闭rsync服务,可以kill进程

[root@rsync ~]# rsync --daemon
[root@rsync ~]# 
[root@rsync ~]# netstat -antp | grep rsync
tcp        0      0 192.168.245.205:873     0.0.0.0:*               LISTEN      102138/rsync  

5、验证下行同步

模拟同步web站点下的网页文件,安装apache然后编辑一个主页,这里需要给站点目录放开最大权限

[root@rsync ~]# yum -y install httpd

[root@rsync ~]# cd /var/www/html/
[root@rsync html]# echo "this is test web" > index.html
[root@rsync html]# cd ..
[root@rsync www]# chmod 777 html/

在客户机上也安装apache,将站点目录当做同步的目的地,并开放权限,此时这个目录是空的

[root@client ~]# yum -y install httpd

[root@client ~]# cd /var/www/html/
[root@client html]# cd ..
[root@client www]# chmod 777 html/

配置将rsync服务器上的共享模块下的文件同步到本地的/var/www/html下

rsync 同步源的资源表示方式为“用户名@主机地址::共享模块名”或者“rsync://用户名@主机地址/共享 模块名”,前者为两个冒号分隔形式,后者为 URL 地址形式

第一种:冒号分隔形式:
注意:在这里需要写共享模块的名称而不是真实路径,ip地址与共享模块直接用双冒号

[root@client ~]# rsync -avz backuper@192.168.245.205::wwwroot /var/www/html
Password: 
receiving incremental file list
./
index.html

sent 46 bytes  received 128 bytes  12.89 bytes/sec
total size is 17  speedup is 0.10

客户端验证已同步到了服务器上的主页

[root@client ~]# cd /var/www/html
[root@client html]# ls
index.html
[root@client html]# cat index.html 
this is test web

从以上操作可以看出,同步的基本格式为rsync [选项] 原始位置 目标位置

rsync命令的常用选项:

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

第二种:URL 地址形式

[root@client html]# rm -rf index.html 
[root@client html]# 
[root@client html]# ls
[root@client html]# 

[root@client html]# rsync -avz rsync://backuper@192.168.245.205/wwwroot /var/www/html
Password: 
receiving incremental file list
./
index.html

sent 46 bytes  received 128 bytes  13.92 bytes/sec
total size is 17  speedup is 0.10

[root@client html]# ls
index.html
[root@client html]# cat index.html 
this is test web

免交互密码方式:
创建一个密码文件,里面只保存用户的密码

[root@client html]# vim /etc/server.pass

abc123

用–password-file来指定密码文件就可以实现免交互

[root@client html]# rsync -avz --delete --password-file /etc/server.pass backuper@192.168.245.205::wwwroot /var/www/html
receiving incremental file list
./
index.html

sent 46 bytes  received 128 bytes  16.57 bytes/sec
total size is 17  speedup is 0.10
[root@client html]# ls
index.html

6、inotify+rsync 实时同步(上行同步)

Linux 内核从 2.6.13 版本开始提供了 inotify 通知接口,用来监控文件系统的各种变化情 况,如文件存取、删除、移动、修改等。利用这一机制,可以非常方便地实现文件异动告警、 增量备份,并针对目录或文件的变化及时作出响应。 将 inotify 机制与 rsync 工具相结合,可以实现触发式备份(实时同步)——只要原始位 置的文档发生变化,则立即启动增量备份操作;否则处于静默等待状态,这样,就避免了按固定周期备份时存在的延迟性、周期过密等问题
在这里插入图片描述
正因为 inotify 通知机制由 Linux 内核提供,因此主要做本机监控,在触发式备份中应用时更适合上行同步

在 Linux 内核中,默认的 inotify 机制提供了三个调控参数:max_queue_events、 max_user_instances、max_user_watches,分别表示监控事件队列(16384)、最多监控实例数(128)、每个实例最多监控文件数(8192)当要监控的目录、文件数量较多或者变化较频繁时,建议加大这三个参数的值

发起端配置内核参数:

[root@client html]# vim /etc/sysctl.conf
fs.inotify.max_queued_events = 16384 
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576


[root@client html]# sysctl -p
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

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

安装inotify工具:

[root@client opt]# tar zvxf inotify-tools-3.14.tar.gz
[root@client opt]# yum -y install gcc gcc-c++ make
[root@client opt]# cd inotify-tools-3.14/
[root@client inotify-tools-3.14]# ./configure 
[root@client inotify-tools-3.14]# make && make install

现在验证监控功能,在客户端的web默认站点目录下,删掉一个网页文件,再新建一个文件

[root@client ~]# cd /var/www/html
[root@client html]# ls
index.html
[root@client html]# rm -rf index.html 
[root@client html]# touch test

执行“inotifywait”命令,跟踪屏幕输出结果
选项“-e”用来指定要监控哪些事件,选项“-m”表示持续监控,选项“-r”表示递归整个目录,选项“-q”简化输出信息

发起实时监控
[root@client ~]# inotifywait -mrq -e modify,create,move,delete /var/www/html/
/var/www/html/ DELETE index.html
/var/www/html/ CREATE test

inotifywait 可监控 modify(修改)、create(创建)、move(移动)、delete(删除)、 attrib(属性更改)等各种事件,一有变动立即输出结果

编写脚本实现自动两端同步:
为了简单,只要检测到变动时执行 rsync 上行同步操作即可。需要注意的是, 当更新较频繁时,应避免并发执行 rsync 备份——若 rsync 进程已经存在,则忽略本次同步, 或者根据 rsync 进程数量(取决于实际任务)来决定是否同步

[root@client ~]# cd /opt
[root@client 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/ [email protected]::wwwroot/"  

$INOTIFY_CMD | while read DIRECTORY EVENT FILE  <----一旦读取到$INOTIFY_CMD 命令的输出结果就执行do后面的语句
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        $RSYNC_CMD
    fi
done

给脚本增加执行权限

[root@client opt]# chmod +x inotify.sh

到源服务器上,修改配置文件,将read only的yes改为no,我们要对文件进行修改,就不能只读

[root@rsync ~]# vim /etc/rsyncd.conf
read only = no

重启服务

[root@rsync html]# netstat -anpt | grep 873
tcp        0      0 192.168.245.205:873     0.0.0.0:*               LISTEN      102138/rsync        
tcp        0      0 192.168.245.205:873     192.168.245.206:39282   ESTABLISHED 102574/rsync        
[root@rsync html]# pkill -9 rsync

[root@rsync html]# rsync --daemon
failed to create pid file /var/run/rsyncd.pid: File exists
[root@rsync html]# 
[root@rsync html]# rm -rf /var/run/rsyncd.pid
[root@rsync html]# 
[root@rsync html]# 
[root@rsync html]# rsync --daemon

查看权限是否全部放通

[root@rsync ~]# ll /var/www/
总用量 0
drwxr-xr-x 2 root root  6 4   2 21:14 cgi-bin
drwxrwxrwx 2 root root 24 9  13 14:47 html

将目录下的文件全部删掉

[root@rsync ~]# cd /var/www/html
[root@rsync html]# ls
index.html
[root@rsync html]# rm -rf index.html 
[root@rsync html]# 

开启监控脚本

[root@client opt]# ./inotify.sh 
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.test.pPXwSr" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
rsync: failed to set times on "/." (in wwwroot): Operation not permitted (1)
rsync: chgrp "/.test.92fP3a" (in wwwroot) failed: Operation not permitted (1)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

在发起端新建一个文件

[root@client html]# touch test

查看已经同步到源服务器上了

[root@rsync html]# ls
test
[root@rsync html]# ll
总用量 0
-rw------- 1 nobody nobody 0 9  13 21:41 test  <---属主属组改为了nobody

猜你喜欢

转载自blog.csdn.net/shengjie87/article/details/108569977