rsync

  • rsync概述
  • 工作原理
  • rsync特性
  • 实验演示
  • ssh协议数据同步
  • 下行同步
  • 上行同步
  • rsync协议数据同步
  • 扩展1
  • 扩展2
  • rsync连接失败的解决方法


rsync概述

rsync是用于数据镜像备份的工具。支持增量备份的完全备份,支持本地复制,远程同步等,类似于scp,rsync在同步文件之前先登录目标主机进行用户身份认证,认证成功后才能进行数据同步。身份认证的方式有两种协议,ssh协议 和 rsync协议

工作原理

rsync 原理图 rsync传输原理_服务器

rsync特性

  1. 能更新整个目录树和文件系统
  2. 有选择性的保留符号链接、硬链接、文件属性、权限、设备以及时间等
  3. 对于安装来说,无任何权限要求
  4. 对于传输多文件,传输效率更高
  5. 能用ssh的端口或自定义端口作为传输入口端口

实验演示

ssh协议数据同步

实验环境:两台服务器
在两台服务器上分别创建目录(/filesrc,/filedst)
实现下行同步(下载)
格式: rsync -avz 服务器地址:/服务器目录/* /本地目录
-a:归档模式,递归并保留对象属性
-v:显示同步过程
-z:传输文件时进行压缩
上行同步(上传)
格式: rsync -avz /本地目录/* 服务器地址:/服务器目录
注:实验可以使用root进行实验,在生成环境中尽量使用普通用户,减少权限溢出。因此可以使用acl

# ueradd zhangsan
# passwd zhangsan
# setfacl -m u:zhangsan:rwx /filedst

实验测试:

下行同步

①首先连接两个服务器,分别创建/filesrc /filedst

[root@localhost filedst]# mkdir /filedst/ #src服务器

[root@localhost filesrc]# mkdir /filesrc/ #dst服务器

现在做下行同步实验,因此我们以dst为基准,去src上进行下载,所以在src上首先创建几个文件,给dst提供下载资源

rsync 原理图 rsync传输原理_rsync 原理图_02

②在src上创建文件

[root@localhost filesrc]# touch {1…5}.txt #创建5个分别以1.txt,2.txt的文件

rsync 原理图 rsync传输原理_html_03

③在dst上进行下行同步

[root@localhost filedst]# rsync -avz root@192.168.28.148:/filesrc/* /filedst/

rsync 原理图 rsync传输原理_linux_04


ok,dst服务器上已经成功从src下载完成!!

上行同步

我们还是以dst为基准服务器,向src上传文件。因此我们现在在dst上创建几个.html的文件。
①在dst服务器上,批量创建.html的文件
[root@localhost filedst]# touch {1…5}.html

rsync 原理图 rsync传输原理_运维_05


②接着开始进行上传

[root@localhost filedst]# rsync -avz /filedst/* root@192.168.28.148:/filesrc/

rsync 原理图 rsync传输原理_运维_06


③再去dst上查看

结果显示:已经上传过来

rsync 原理图 rsync传输原理_html_07


ok,上行同步实验也完成!!但我发现一个问题,就是每次同步时,都需要登录目标主机的密码。那我们是否可以不用密码进行访问?答案是肯定的,由于rsync利用ssh协议同步,所以我们可以在两个服务器上互相把自己的密钥发给对方,因此在需要登录目标主机时,服务器直接去验证密钥就ok了。(对称加密)

因此我们可以接着进行实验:
①首先我们使用上行同步,以src为基准,并将dst刚刚创建的文件全部删除。
[root@localhost filedst]# ls
1.html 1.txt 2.html 2.txt 3.html 3.txt 4.html 4.txt 5.html 5.txt
[root@localhost filedst]# rm -rf ./*
[root@localhost filedst]# ls
②接着我们在两台服务器上分别创建密钥,并发送给对方

[root@localhost filedst]# ssh-keygen -t rsa -b 2048  #自己生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:e+N6dqQ/2ZNcwTtU+SFvkCV8t5TVE4D9yINnUyYWWdM root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|            ++B=O|
|           . X.XE|
|            + & B|
|           . B O.|
|        S   o = o|
|         .  .  o.|
|        . oo + o.|
|         o+.+ =  |
|        .+.o.. . |
+----[SHA256]-----+

[root@localhost filedst]# ssh-copy-id root@192.168.28.148   #发送给对方
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.28.148's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.28.148'"
and check to make sure that only the key(s) you wanted were added.
[root@localhost filesrc]# ssh-keygen -t rsa -b 2048  #自己生成密钥
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:D9EMZ7sjIkVFfsJ49t4+wyWvXMo2NGpGGA8/R+kYFrY root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
|      .o+ o      |
|     . + *o.     |
|      o B.=o .   |
|     . oo=E.o    |
|    . . SB+=     |
|     . ..=*o* .  |
|         .o*.=.  |
|          ++*o.  |
|         o .*=   |
+----[SHA256]-----+
[root@localhost filesrc]# ssh-copy-id root@192.168.28.149  #发送给对方
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.28.149's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.28.149'"
and check to make sure that only the key(s) you wanted were added.

③开始正式测试:在src上进行上行同步

[root@localhost filesrc]# rsync -avz /filesrc/1.txt  root@192.168.28.149:/filedst/
sending incremental file list
1.txt

sent 84 bytes  received 35 bytes  79.33 bytes/sec
total size is 0  speedup is 0.00

由于dst上之前被删了,所以此时应该已经存在1.txt这个文件了。去dst上查看
[root@localhost filedst]# ls
1.txt
ok,实验成功!!

rsync协议数据同步

实验准备:两台服务器
①在两台服务器上分别创建/filesrc /filedst目录
②搭建rsync服务(仅在一台服务器上搭建即可)
1)进入主配置文件(vim /etc/rsyncd.conf,centos7以下需要自己创建)

[root@localhost xinetd.d]# vim /etc/rsyncd.conf
	     address = 192.168.28.148			#rsync服务绑定IP。
        port 873							#默认服务端口873。
        log file = /var/log/rsyncd.log		#日志文件位置。
        pid file = /var/run/rsyncd.pid		#进程号文件位置。
   [web]
        comment = web directory backup		#共享描述话语。
        path = /filesrc						#实际共享目录。
        read only = no						#是否仅允许读取
        dont compress = *.gz *.bz2			#哪些文件类型不进行压缩。
        auth users = user1					#登录用户名,非系统用户,需自行创建
        secrets file = /etc/rsync_users.db	#同上,存储账号密码的地方

注意:后面的注释最好不要加上,最好去掉!!!!
2)创建认证所需的账户密码文件(vim /etc/rsync_users.db)

[root@localhost xinetd.d]# vim /etc/rsync_users.db
	user1:123
[root@localhost xinetd.d]# chmod 600 /etc/rsync_users.db	#必须修改权限否则登录报错!!

3)启动rsync服务

[root@localhost xinetd.d]# rsync --daemon
[root@localhost xinetd.d]# ps aux | grep rsync  #查询是否存在
root       6564  0.0  0.0 114740   384 ?        Ss   14:58   0:00 rsync --daemon
root       6651  0.0  0.0 112720   984 pts/0    R+   15:01   0:00 grep --color=auto rsync
[root@localhost xinetd.d]# netstat -antp | grep 873  #查询服务端口是否开启
tcp        0      0 192.168.28.148:873      0.0.0.0:*               LISTEN      6653/rsync

4)设置映射用户对共享目录有权限r(一般都是nobody,因此可以不写acl)
[root@localhost xinetd.d]# setfacl -m u:nobody:rwx /filesrc/

5)接着开始测试:在另一台服务器上,进行下行同步。
在src服务器上的/filesrc目录下创建6.txt文件,而dst通过下行同步进行下载。

[root@localhost filesrc]# touch 6.txt 

[root@localhost filedst]# rsync -avz rsync://user1@192.168.28.148/web /filedst/
Password: 
receiving incremental file list
./
6.txt

sent 46 bytes  received 103 bytes  99.33 bytes/sec
total size is 0  speedup is 0.00
[root@localhost filedst]# ls
1.txt  6.txt

ok,实验测试成功!!!

扩展1

可以实现dst与src同步,尽管dst上存在或者缺少多余的文件,都会跟src进行同步,操作过程是增或者删都有可能

#首先src上存在6和7的文件,而dst存在1,2,6的文件

```bash
[root@localhost filesrc]# ls     
6.txt  7.txt

[root@localhost filedst]# ls
1.txt  2.txt  6.txt
[root@localhost filedst]# rsync -avz --delete rsync://user1@192.168.28.148/web /filedst/
Password: 
receiving incremental file list
deleting 2.txt
deleting 1.txt
./
7.txt

sent 46 bytes  received 127 bytes  69.20 bytes/sec
total size is 0  speedup is 0.00
[root@localhost filedst]# ls
6.txt  7.txt

实验结果显示,dst会同步src上的文件

扩展2

上行同步测试,还是以src为基准,但此时的上传就是dst上传文件到src上

  1. 先删掉src /filesrc下的文件
    [root@localhost filesrc]# ls
    6.txt 7.txt
    [root@localhost filesrc]# rm -rf *
    [root@localhost filesrc]# ls
  2. 再通过dst进行上传
[root@localhost filedst]# rsync -avz /filedst/*   rsync://user1@192.168.28.148/web
Password: 
sending incremental file list
6.txt
7.txt
rsync: chgrp "/.6.txt.r9BPuX" (in web) failed: Operation not permitted (1)
rsync: chgrp "/.7.txt.6261SF" (in web) failed: Operation not permitted (1)

sent 140 bytes  received 212 bytes  234.67 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]
在这里好像存在错误,因此解决办法
原因是有文件没有写的权限,导致备份权限不够,两种解决办法:
1、将服务端rsyncd.conf配置文件的uid和gid分别修改成root,重载下,/etc/rc.d/init.d/xinetd reload,再次执行同步,同步成功
2、将需要同步的文件夹及下属文件赋予777权限(chmod -R 777 xxx),再次执行同步,同步成功
注意:如果使用第一种办法,那么在执行完同步后,为了安全,记得将uid和gid修改回来,或修改成nobody
[root@localhost filesrc]# chmod  777 /filesrc/
[root@localhost filesrc]# ls
6.txt  7.txt
此时dst已经上传到src上了!!!ok,实验成功!!