Linux系统架构-----远程同步工具rsync

目录

 

一.rsync概述

二.rsync命令与具体操作

三.rsync配置文件解析

四.开启rsync daemon工作方式

五.配合inotify工具实现触发式传输


一.rsync概述

  • r(remote)sync是unix以及类unix平台下的数据镜像备份软件,它不像ftp那样需要全备份,rsync可以根据数据的变化进行差异备份,从而减少数据流量,提高工作效率
  • rsync可以远程或者本地实现增量备份。rsync可以实现本地主机和远程主机上的文件同步(包括本地推到远程,远程拉到本地两种同步方式),也可以实现本地不同路径下(不同目录、分区之间)文件的同步,但不能实现远程路径1到远程路径2之间的同步(scp可以实现)
  • rsync配合计划性任务,可以实现定时或周期同步
  • rsync配置inotify或者sersync,可以实现触发式的实时同步
  • 这是rsync的官网界面

  • rsync主要分为三个配置文件:tsyncd.conf(主配置文件);rsync.secrets(密码文件);rsyncd.motd(服务器信息文件)
  • rsync具有以下几个特点:

1.可以镜像保存整个目录树和文件系统

2.可以很容易的保持原来文件的权限、时间、软硬链接等,无须特殊权限即可安装

3.效率高,第一次同步时rsync会复制全部内容,但是下一次只会传输修改过的文件,rsync在传输数据的过程中可以实行压缩以及解压操作,使用到的带宽更少

扫描二维码关注公众号,回复: 10114626 查看本文章

4.安全性高,rsync支持匿名传输,以方便进行网站镜像,传输数据时可以使用ssh加密传输

注意:在centos系统中rsync是系统自带的,无需额外安装,但是如果使用最小化安装,可以使用 yum install -y  rsync 安装一下。

5.支持限速、支持断点续传

  • rsync同步过程中由两部分模式组成,检查模式(决定哪些文件需要同步)和同步模式(文件同步时)

(1).检查模式是指按照指定规则来检查哪些文件需要被同步,例如哪些文件是明确被排除不传输的。默认情况下,rsync使用"quick check"算法快速检查源文件和目标文件的大小、mtime(修改时间)是否一致,如果不一致则需要传输。当然,也可以通过在rsync命令行中指定某些选项来改变quick check的检查模式,比如"--size-only"选项表示"quick check"将仅检查文件大小不同的文件作为待传输文件。rsync支持非常多的选项,其中检查模式的自定义性是非常有弹性的。

(2).同步模式是指在文件确定要被同步后,在同步过程发生之前要做哪些额外工作。例如上文所说的是否要先删除源主机上没有但目标主机上有的文件,是否要先备份已存在的目标文件,是否要追踪链接文件等额外操作。rsync也提供非常多的选项使得同步模式变得更具弹性。

相对来说,为rsync手动指定同步模式的选项更常见一些,只有在有特殊需求时才指定检查模式,因为大多数检查模式选项都可能会影响rsync的性能。

  • rsync的三种工作方式

(1).本地文件系统上实现同步。命令行语法格式为"Local"段的格式。

 Local: rsync [OPTION...] SRC... [DEST] 

(2)本地主机使用远程shell和远程主机通信,命令语法格式为

Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 

(3)本地主机通过网络套接字连接远程主机上的rsync daemon,命令语法格式为:

Access via rsync daemon: 
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
rsync [OPTION...] SRC... rsync://[USER@]HOST[:PORT]/DEST 

二.rsync命令与具体操作

本地复制时

  • 格式为:rsync [选项] 原始位置 目标位置
常用选项有:
-a    //归档模式,递归保留对象属性,等同于 -rlptgoD
-v    //显示像详细信息
-z    //在传输文件时进行压缩
-H    //保留硬链接文件
-A    //保留ACL属性
-t    //暴露修改时间属性
-p    //保留文件权限属性
-g    //保留文件所属组属性
--progress    //显示数据传输的进度信息
--password-file=FILE    //指定密码文件,将密码写入文件,实现免交互数据库同步,需要修改权限,使用这个选项需要开启rsync daemon模式
--delete    //删除那些仅在目标路径中存在的文件(源路径中不存在),在脚本中的数据同步中经常加上这个参数
--list-only    //仅列出服务器模板列表,需要rsync服务器设置list=true
--checksum    //根据对象的校验和来决定是否跳过文件
  • 实例
[root@192 ~]# ls
1.txt                                initial-setup-ks.cfg  视频  下载
anaconda-ks.cfg                      公共                  图片  音乐
harbor-offline-installer-v1.2.2.tgz  模板                  文档  桌面
[root@192 ~]# rsync -avz harbor-offline-installer-v1.2.2.tgz /opt    //保留文件属性、压缩、详细信息等
sending incremental file list
harbor-offline-installer-v1.2.2.tgz

sent 533945011 bytes  received 31 bytes  71192672.27 bytes/sec
total size is 533765727  speedup is 1.00
[root@192 ~]# cd /opt
[root@192 opt]# ls
harbor-offline-installer-v1.2.2.tgz  rh

通过远程shell复制

  • 下载数据格式以及实例
rsync [选项] [user@a]HOST:SRC...[DEST]     #不加user@表示用root用户进行登陆远程主机下载数据到 本地的DEST路径

##client为
[root@client ~]# echo "123" > 1.txt
[root@client ~]# ls
1.txt            initial-setup-ks.cfg  模板  图片  下载  桌面
anaconda-ks.cfg  公共                  视频  文档  音乐
[root@client ~]# 


##从client上下载数据
[root@server ~]# rsync -avz [email protected]:1.txt /root/
[email protected]'s password: 
receiving incremental file list
1.txt

sent 36 bytes  received 72 bytes  30.86 bytes/sec
total size is 4  speedup is 0.04
[root@server ~]# ls
1.txt                                initial-setup-ks.cfg  视频  下载
anaconda-ks.cfg                      公共                  图片  音乐
harbor-offline-installer-v1.2.2.tgz  模板                  文档  桌面
[root@server ~]#
  • 上传数据格式以及实例
##格式为:
上传数据:rsync[选项] SRC...[user@]HOST:DEST   #这里的SRC表示本地数据,DEST表示远端主机目录


##实例如下:
[root@server ~]# rsync -avz --progress harbor-offline-installer-v1.2.2.tgz [email protected]:/root/
[email protected]'s password: 
sending incremental file list
harbor-offline-installer-v1.2.2.tgz
   533765727 100%   50.62MB/s    0:00:10 (xfer#1, to-check=0/1)

sent 533945011 bytes  received 31 bytes  39551484.59 bytes/sec
total size is 533765727  speedup is 1.00
[root@server ~]# 

--progress显示进度条

通过rsync进程复制

  • 下载数据格式
rsync [选项] [user@] HOST::SRC...[DEST] #这里双冒号后的SRC表示远端服务器端的模块名
rsync [选项] rsync://[user@]HOST[:port]/src...[DEST] #这里的SRC表示实际的同步目录名,可以指定端口
  • 上传数据格式
rsync [选项] SRC...[user]@HOST::DEST #上传本地客户端数据到远端服务端的DEST模块名指定的路径
rsync [选项] SRC...rsync://@HOST[:port]/DEST

三.rsync配置文件解析

  • 配置文件rsync.conf是由全局配置和若干模块配置组成、
  • 配置文件的语法为:
模块以 [模块名] 开始

参数配置行的格式是 name = value ,其中 value 可以有两种数据类型:

字符串(可以不用引号定界字符串)

布尔值(1/0 或 yes/no 或 true/false)

以 # 或 ; 开始的行为注释

\ 为续行符
  • 在文件中[module]之外的所有配置行都是全局参数,当然也可以在全局参数部分定义模块参数,这时该参数的值就是所有模块的默认值。
  • 模块参数主要用来定义rsync服务器哪个目录要被同步。模块声明的格式必须为[module]形式;这个名字就是rsync客户端看到的名字。类似于Samba服务器提供的共享名。而服务器真正同步的数据是通过path来指定的,可以根据自己的需要,来指定多个模块
  • rsync认证口令文件权限一定是600,否则客户端将不能连接服务端
  • rsync认证口令文件中每一行指定一个用户名:口令对,格式为:username:passwd
vim /etc/rsyncd.conf

motd file = /etc/rsyncd.motd    #设置服务器信息提示文件,在该文件中编写提示信息

transfer logging = yes    #开启rsync数据传输日志功能

log file = /var/log/rsyncd.log    #设置日志文件名,可通过log format参数设置日志格式

pid file = /var/run/rsyncd.log    #设置rsync进程号保存文件名称

lock file = /var/run/rsync.lock    #设置锁文件名称

port = 873    #设置服务器监听的端口号,默认是873

address = 192.168.0.230    #设置本服务器所监听网卡接口的ip地址

uid = nobody    #设置进行数据传输时所使用的帐户名或ID号,默认使用nobody

gid = nobody    #设置进行数据传输时所使用的组名或GID号,默认使用nobody

#若为yes, rsync会首先进行chroot设置,将根映射在下面的path参数路径下,对客户端而言,系统的根就是path参数指定的路径。但这样做需要root权限,并且在同步符号连接资料时只会同步名称,不会同步内容。

use chroot = no 

read only = yes    #是否允许客户端上传数据,yes表示不允许

max connections =10    #设置并发连接数,0表示无限制

[common]    #自定义模块名,rsync通过模块定义同步的目录,可定义多个

comment = web content    #定义注释说明字串

path = /common    #同步目录的真是路径通过path指定

ignore errors    #忽略一些IO错误

#exclude = test/    #exclude指定common目录下某个目录可以不同步数据

auth users = tom, jerry    #设置允许连接服务器的账户,此账户可以是系统中不存在的用户

secrets file = /etc/rysncd.secrets    #密码验证文件名,该文件权限要求为只读,建议为600,仅在设置auth users后有效

hosts allow = 192.168.0.0/255.255.255.0   #设置哪些主机可以同步数据,多ip和网段之间使用空格分隔

hosts deny=*    #除了hosts allow定义的主机外,拒绝其他所有

list = false    #客户端请求显示模块列表时,本模块名称是否显示,默认为true

四.开启rsync daemon工作方式

配置server

  • 安装一个apache服务,提供一个站点目录
[root@server ~]# yum install httpd -y
[root@server ~]# cd /var/www/html/
[root@server html]# ls
[root@server html]# echo "this is test web" > index.html
[root@server html]# chmod 777 index.html 
[root@server html]# 
  • 修改rsync配置文件
vim /etc/rsyncd.conf
1、开启以下功能:
uid = nobody
gid = nobody
use chroot = yes   
//注:用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能。
pid file = /var/run/rsyncd.pid    //存放进程ID的文件位置
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2   //同步时不再压缩的文件类型

2、添加以下选项:
address = 192.168.43.101        //添加本机IP地址
log file = /var/log/rsyncd.log    //日志文件位置
port 893                          //端口号
hosts allow = 192.168.43.0/24    //设置白名单,允许哪些地址可以访问

3、添加共享模块名:
[wwwroot]                 //共享文件名,这个自定义
path = /var/www/html      //源目录的实际路径
comment = www.kgc.com     //描述
read only = yes           //是否为只读
auth users = backuper     //授权账户名
secrets file = /etc/rsyncd_users.db   //存放账户信息的数据文件
  • 编辑账户信息的数据文件
[root@server ~]# cat /etc/rsyncd_users.db
backuper:abc123
//权限设置为600,必须设置为600,否则客户端认证会失败
[root@server ~]# chmod 600 /etc/rsyncd_users.db 
[root@server ~]# 
  • 启动服务
[root@server ~]# rsync --daemon
[root@server ~]# netstat -natp | grep rsync
tcp        0      0 192.168.43.101:873      0.0.0.0:*               LISTEN      37983/rsync         
[root@server ~]# 

client配置

  • 交互模式,下载数据
[root@client ~]# rsync -avz [email protected]::wwwroot /opt
Password: 
receiving incremental file list
./
index.html

sent 83 bytes  received 172 bytes  102.00 bytes/sec
total size is 17  speedup is 0.07
[root@client ~]# cd /opt
[root@client opt]# ls
index.html  rh
[root@client opt]# cat index.html 
this is test web
[root@client opt]# 

报错:rsync: failed to connect to 192.168.43.101 (192.168.43.101): No route to host (113)
rsync error: error in socket IO (code 10) at clientserver.c(122) [Receiver=3.0.9]

解决:关闭server的防火墙和selinux(systemctl stop firewalld /sentenforce 0)

或者firewall-cmd --permanent --add-port=873/tcp    #添加防火墙规则,允许873端口的数据访问

  • 免交互模式,在client写一个密码文件,利用 --password-file=FILE 选项进行免交互
##在server的/index.html追加文本
[root@server html]# cat index.html 
this is test web
[root@server html]# echo "123" >> index.html 
[root@server html]# cat index.html 
this is test web
123
[root@server html]#


##在client上下载数据,会覆盖原有的文件
[root@client ~]# echo "abc123" > /etc/server.pass
[root@client ~]# chmod 600 /etc/server.pass 
[root@client ~]# rsync -avz --password-file=/etc/server.pass [email protected]::wwwroot /opt/
receiving incremental file list
index.html

sent 86 bytes  received 177 bytes  526.00 bytes/sec
total size is 21  speedup is 0.08
[root@client ~]# cat /opt/index.html 
this is test web
123
[root@client ~]# 

使用shell脚本,在客户端定期对rsync服务器进行数据备份

vi /root/run.sh
#!/bin/bash

export PATH=/bin:/usr/bin:/usr/local/bin

SRC=common #模块名

DEST=/data

server=192.168.0.230

user=tom

passfile=/root/rsync.pass

#if the DEST directory not found, then create one

[ ! -d $DEST ] && mkdir $DEST

[ ! -e $passfile ] && exit 2

rsync -az --delete --password-file=$passfile ${user}@${server}::$SRC $DEST/$(data +%Y%m%d)   #加上日期


##在使用crotab定时任务执行这个脚本
crotab -e
0 0 * * * bash /root/run.sh




五.配合inotify工具实现触发式传输

rsync实现同步的优劣

  • rsync的缺点

执行备份的时间固定,延迟明细,实时性差;

当同步源长期不变化时,密集的定期任务是不必要的

  • rysnc的优点

一旦同步源出现变化,立即启用备份;

只要同步源不变化,则不执行备份

inotify的介绍

  • Inotify是一个Linux内核机制,它可以监控文件系统的操作,比如读取、写入、创建等。
  • Inotify反应灵敏,用法非常简单,并且比cron任务的繁忙轮询高效
  • 从版本 2.6.13 开始提供;
  • 可以监控文件系统的变化情况,并作出通知响应;
  • 辅助软件:inotify-tools

配置rsync+inotify实时同步

  • 在客户端修改内核参数
[root@client ~]# vim /etc/sysctl.
sysctl.conf  sysctl.d/    
[root@client ~]# vim /etc/sysctl.conf 
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576

[root@client ~]# sysctl -p    //生效
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 1024
fs.inotify.max_user_watches = 1048576
[root@client ~]# 


//参数解析
max_queue_event : 监控队伍大小
max_user_instances : 最多监控实例数
max_user_watches : 每个实例最多监控文件数
  • 在客户端编译安装inotify-tools辅助工具
yum install gcc gcc-c++ make -y       //安装编译工具
tar zxvf inotify-tools-3.14.tar.gz -C /opt/    //解压
cd /opt/inotify-tools-3.14/
./configure            //编译
make && make install    //安装
  • 在客户端进行文件监控
[root@client opt]# mkdir myweb
[root@client opt]# ls
index.html  inotify-tools-3.14  myweb  rh
[root@client opt]# inotifywait -mrq -e modify,create,move,delete  /opt/myweb




##
inotifywait:用于持续监控,实时输出结果;
inotifywatch:用于短期监控,任务完成后再出结果
  • 另外再开一个客户端的终端,写一个shell脚本
[root@client opt]# ls
index.html  inotify.sh  inotify-tools-3.14  myweb  rh
[root@client opt]# cat inotify.sh 
#!/bin/bash
INOTIFY_CMD="inotifywait -mrq -e modify,create,move,delete /opt/myweb"
RSYNC_CMD="rsync -avz --delete --password-file=/etc/server.pass /opt/myweb/ [email protected]::wwwroot/"
$INOTIFY_CMD | while read DIRECTORY EVENT FILE ##读取输出的监控记录
 do
   if [ $(pgrep rsync | wc -l) -le 0 ]; then  ##如果rsync未在执行,则立即启动
      $RSYNC_CMD
   fi
done
[root@client opt]# chmod +x inotify.sh
  • 在server上,关闭只读模式,并且重启服务
[root@server html]# vi /etc/rsyncd.conf 
......
read only = no    
......
[root@server html]# kill $(cat /var/run/rsyncd.pid)
[root@server html]# rsync --daemon  
[root@server html]#  netstat -anpt |grep rsync
tcp        0      0 192.168.43.101:873      0.0.0.0:*               LISTEN      38855/rsync         
[root@server html]# chmod 777 /var/www/html/
[root@server html]# 
  • 在客户端上启动脚本
[root@client opt]#  chmod 777 /opt/myweb/
[root@client opt]# ./inotify.sh 

测试是否数据同步

  • 在client的/opt/myweb/目录下添加数据,查看server的/var/www/hmtl中没有数据

  • 在client删除这个数据。查看server中数据会不会被删除

发布了139 篇原创文章 · 获赞 168 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_42761527/article/details/105062562
今日推荐