Linux 集群架构_Rsync企业级全网备份服务器_05

rsync 复制软件应用与实践

what 是什么?
why 为什么要用?
where

1、什么是rsync?
rsync,Rsync英文全称为Remote synchronization 缩写rsync

是开源、高速的、可实现本地以及远程,全量以及增量的数据复制(拷贝)工具。

2、全量复制和增量复制的区别
全量复制:

[root@backup ~]$ cp -a /etc/ /opt/
[root@backup ~]$ \cp -a /etc/ /opt
[root@backup ~]$ touch /etc/oldboy.txt
[root@backup ~]$ \cp -a /etc/ /opt/

增量复制: 
只复制oldboy.txt

3、rsync的作用
工作中需要定时/实时数据备份。不同机器、
不同机房之间的数据备份。都可以rsync完成。

4、rsync功能特性

  • 支持拷贝普通文件与特殊文件,如连接文件,设备文件等。
  • 支持排除指定文件或目录同步的功能,相当于打包命令tar的排除功能。
  • 可以做到保持原文件或目录的权限、时间、软硬连接、属主、组等所有属性均不改变。
  • 可实现增量复制,既只复制发生变化的数据,因此数据传输效率很高。
  • 可以使用rcp,rsh,ssh等方式来配合进行隧道加密传输文件(rsync本身不对数据加密)。
  • 可以通过socket(进程方式)传输文件和数据(服务端和客户端)*。
  • 支持匿名或认证(无需系统用户)进程模式传输,安全的进行数据备份及镜像。

5、增量复制原理
使用quick check算法 ,只对增量的部分复制,根据大小属性的变化进行复制
2.x比对差异后复制,3.x一边比对一边复制。

6、rsync三种工作模式介绍
1)本地(local)
rsync命令
a、把数据从一个地方复内容制到另一个地方(仅在一台机器增量),相当于cp
b、通过加参数实现删除文件和清空文件的功能,相当于rm命令。
c、查看属性信息功能,相当于ls。

实践:
基础不牢,地动山摇。
听话出活,跑通在变通。

a.拷贝实践

[root@backup ~]$ ls /opt
etc
[root@backup ~]$ rsync /etc/hosts /opt
[root@backup ~]$ ls /opt
etc hosts
[root@backup ~]$ \cp /etc/hosts /opt

保持属性:

[root@backup ~]$ rsync -zrtopg /etc/hosts /opt/
[root@backup ~]$ ls -l /etc/hosts /opt/hosts
-rw-r--r--. 1 root root 330 2月 21 11:35 /etc/hosts
-rw-r--r-- 1 root root 330 2月 21 11:35 /opt/hosts
[root@backup ~]$ ls -lhi /etc/hosts /opt/hosts
16825558 -rw-r--r--. 1 root root 330 2月 21 11:35 /etc/hosts
375777 -rw-r--r-- 1 root root 330 2月 21 11:35 /opt/hosts

 

b.删除实践
删除文件内容:

[root@backup ~]$ touch /null.txt #空文件
[root@backup ~]$ cat /opt/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.5 lb01
172.16.1.6 lb02
172.16.1.7 web01
172.16.1.8 web02
172.16.1.9 web03
172.16.1.31 nfs01
172.16.1.41 backup
172.16.1.51 db01 db01.etiantian.org
172.16.1.61 m01
[root@backup ~]$ rsync --delete /null.txt /opt/hosts #让前面null.txt和后面hosts一样
rsync: --delete does not work without --recursive (-r) or --dirs (-d).
rsync error: syntax or usage error (code 1) at main.c(1567) [client=3.1.2]
[root@backup ~]$ rsync -r --delete /null.txt /opt/hosts 
-bash: rs: 未找到命令
[root@backup ~]$ rsync -r --delete /null.txt /opt/hosts 
[root@backup ~]$ cat /opt/hosts
删除所有文件:
[roo t@backup ~]$ mkdir /null
[root@backup ~]$ rsync -r --delete /null /opt/
[root@backup ~]$ ls /opt
hosts null
[root@backup ~]$ rsync -r --delete /null/ /opt/ #让后面opt前面null目录内容保持一致 
[root@backup ~]$ ls /opt

 

c.查看属性

[root@backup ~]$ rsync /etc/hosts
-rw-r--r-- 330 2020/02/21 11:35:59 hosts

 

2)远程shell模式
借助类似ssh隧道传输数据,适合不同的机器之间复制。
异地拷贝,相当于scp,区别scp是远程全量拷贝

拉门 推门 思考:
pull,拉:从远端拉取到本地
rsync [OPTION...] [USER@]HOST:SRC... [DEST]
rsync命令 参数选项 [认证用户]@[主机地址]:[源路径] 本地路径


push,推:从本地推送到远端
rsync [OPTION...] SRC... [USER@]HOST:[DEST]
rsync命令 参数选项 本地路径 [认证用户]@[主机地址]:[目标路径]

push实践:

[root@nfs01 ~]$ rsync -avz /etc/hosts root@172.16.1.41:/opt/
The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
ECDSA key fingerprint is SHA256:YJEodYoXLkuQFvsfmYCER9bdwi+BDLN3b92UUeF6BOE.
ECDSA key fingerprint is MD5:12:b3:95:b8:80:ad:15:ae:8b:58:5c:38:3d:74:ad:f7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.41' (ECDSA) to the list of known hosts.
root@172.16.1.41's password: 
sending incremental file list
hosts

sent 219 bytes received 35 bytes 33.87 bytes/sec
total size is 330 speedup is 1.30

检查:

[root@backup ~]$ cd /opt
[root@backup opt]$ ls
hosts
1 rsync -avz /etc/hosts root@172.16.1.41:/opt/
2 rsync -avz /etc/hosts -e 'ssh -p 22' root@172.16.1.41:/opt/
3 上述命令是等价的。 -e指定通道 ssh ssh服务连接客户端 -p 22指定端口
[root@nfs01 ~]$ rsync -avz /etc/hosts -e 'ssh -p 22' root@172.16.1.41:/opt/
root@172.16.1.41's password: 
sending incremental file list

sent 44 bytes received 12 bytes 12.44 bytes/sec
total size is 330 speedup is 5.89

 

拉的命令:

rsync -avz root@172.16.1.41:/opt/hosts /opt
rsync -avz -e 'ssh -p 22' root@172.16.1.41:/opt/hosts /opt
上述命令是等价的。 -e指定通道 ssh ssh服务连接客户端 -p 22指定端口

 

推拉:
a、参照物,执行命令的机器
b、root@172.16.1.41 使用的用户和主机,就用主机和用户的密码。

强调一个重点:适合rsync
null和null/ 区别,null是目录和目录下的内容,
null/只是目录下的内容,不含本身。


3)rsync守护进程模式
首先要搭建rsync服务端(),然后才能在客户端实现要用守护进程推拉数据
企业运维的重要模式,重点讲。
客户端语法暂时不讲。
rsync服务模式:*****开启后台进程,接受别人访问。

 

7、rsync命令参数

rsync命令参数:
-v, --verbose 显示输出过程
-z, --compress 压缩
-a, --archive 多参数集合(-rtopgDl)
-r, --recursive 递归
-t, --times 保持修改时间属性
-o, --owner 保持属主不变
-p, --perms 保持权限不变
-g, --group 保持用户组不变
-l, --links 保持拷贝软连接
-q, --quiet 安静的拷贝
--bwlimit=KBPS 限制I/O带宽,KBytes per second
--delete 删除 
--exclude 排除
--exclude-from 从文件中排除

企业常用参数组合: -avz 或者 -vzrtopg

8、rsync守护进程模式应用实践

以下操作都是在backup服务器:
1)安装

[root@backup ~]$ rsync --version
rsync version 3.1.2 protocol version 31
[root@backup ~]$ yum install rsync (不需要)

2)配置文件/etc/rsyncd.conf
备份

cp /etc/rsyncd.conf{,.ori}
cat>/etc/rsyncd.conf<<EOF
#rsync_config______________start
#created by oldboy
#site:http://www.oldboyedu.com
fake super = yes #不用root用户也可以存储文件的完整属性。
uid = rsync gid = rsync use chroot = no max connections = 200 timeout = 600 pid file = /var/run/rsyncd.pid lock file = /var/run/rsync.lock log file = /var/log/rsyncd.log ignore errors read only = false list = false hosts allow = 172.16.1.0/24 hosts deny = 0.0.0.0/32 auth users = rsync_backup secrets file = /etc/rsync.password [backup] comment = welcome to oldboyedu backup! path = /backup/ EOF
[root@backup ~]$ cat /etc/rsyncd.conf -n
 1    #rsync_config______________start
 2    #created by oldboy
 3    #site:http://www.oldboyedu.com
      fake super = yes   #不用root用户也可以存储文件的完整属性。
4 uid = rsync   # 管理备份目录的用户 5 gid = rsync   # 管理备份目录的用户组 6 use chroot = no  # 安全功能,数据是否锁定到备份目录 7 max connections = 200 # 并发连接,同时多少客户端访问 8 timeout = 600   # 超时时间 9 pid file = /var/run/rsyncd.pid # 进程号所在文件 10 lock file = /var/run/rsync.lock # 锁文件 11 log file = /var/log/rsyncd.log # 日志文件,查看报错等 12 ignore errors      # 忽略错误 13 read only = false    # 可写 14 list = false       # 不允许列表 15 hosts allow = 172.16.1.0/24     # 那些主机可以访问 16 hosts deny = 0.0.0.0/32       # 那些主机不允许可以访问 17 auth users = rsync_backup      # 远程虚拟连接用户 18 secrets file = /etc/rsync.password # 存放密码的文件,格式:用户名:密码 权限 必须600 19 [backup]      # [模块名]远程访问使用模块名访问 20 comment = welcome to oldboyedu backup! # 说明注释 21 path = /backup/ # 服务端用户备份的目录,用户和组,rsync.rsync [root@backup ~]$

要了解知识的源头:

1 man rsync 查命令的参数
2 man rsyncd.conf 查配置参数
3 https://www.samba.org/ftp/rsync/rsync.html

 

学会获取知识的源头。

创建用户和备份目录

[root@backup ~]$ useradd rsync
[root@backup ~]$ id rsync
uid=1002(rsync) gid=1002(rsync) 组=1002(rsync)
[root@backup ~]$ mkdir -p /backup
[root@backup ~]$ chown -R rsync.rsync /backup/
[root@backup ~]$ ls -ld /backup
drwxr-xr-x 2 rsync rsync 6 2月 21 21:54 /backup

 

3)启动和查找

rsync --daemon(c6及以前)
systemctl start rsyncd
systemctl status rsyncd
systemctl enable rsyncd
[root@backup ~]$ systemctl start rsyncd
[root@backup ~]$ systemctl status rsyncd
rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2020-02-21 20:59:42 CST; 33s ago
Main PID: 2132 (rsync)
CGroup: /system.slice/rsyncd.service
└─2132 /usr/bin/rsync --daemon --no-detach

2月 21 20:59:42 backup systemd[1]: Started fast remote file copy program daemon.
[root@backup ~]$ systemctl enable rsyncd
Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service.

 

ps -ef | grep sync 查看进程命令

[root@backup ~]$ ps -ef | grep sync
root 2025 1923 0 20:18 pts/0 00:00:00 /usr/bin/python /usr/bin/yum install rsync
root 2132 1 0 20:59 ? 00:00:00 /usr/bin/rsync --daemon --no-detach
root 2176 1923 0 21:04 pts/0 00:00:00 grep --color=auto sync
[root@backup ~]$ ps -ef | grep sync | grep -v grep
root 2025 1923 0 20:18 pts/0 00:00:00 /usr/bin/python /usr/bin/yum install rsync
root 2132 1 0 20:59 ? 00:00:00 /usr/bin/rsync --daemon --no-detach

 

检测端口命令:

[root@backup ~]$ netstat -lntup | grep 873
tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 2132/rsync 
tcp6 0 0 :::873 :::* LISTEN 2132/rsync 
[root@backup ~]$ lsof -i :873
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rsync 2132 root 3u IPv4 28949 0t0 TCP *:rsync (LISTEN)
rsync 2132 root 5u IPv6 28950 0t0 TCP *:rsync (LISTEN)

面试题:如何查看某端口对应什么服务,答案就上面两个

 

4)配置密码文件

[root@backup ~]$ ls /etc/rsync.password
ls: 无法访问/etc/rsync.password: 没有那个文件或目录
[root@backup ~]$ echo "rsync_backup:oldboy" > /etc/rsync.password
[root@backup ~]$ chmod 600 /etc/rsync.password
[root@backup ~]$ cat /etc/rsync.password
rsync_backup:oldboy
[root@backup ~]$ ll /etc/rsync.password
-rw------- 1 root root 20 2月 21 21:18 /etc/rsync.password

 

rsync服务端配置完成。


(2)以下操作都是在客户端服务器
以下方法2选1
方法1:认证密码文件

[root@nfs01 ~]$ echo "oldboy" > /etc/rsync.password
[root@nfs01 ~]$ chmod 600 /etc/rsync.password:
[root@nfs01 ~]$ cat /etc/rsync.password
oldboy
[root@nfs01 ~]$ ll /etc/rsync.password
-rw------- 1 root root 7 2月 21 21:25 /etc/rsync.password

方法2:

[root@nfs01 ~]$ echo ' export RSYNC_PASSWORD=oldboy' >> /etc/bashrc
[root@nfs01 ~]$ tail -1 /etc/bashrc
export RSYNC_PASSWORD=oldboy
[root@nfs01 ~]$ . /etc/bashrc
[root@nfs01 ~]$ echo $RSYNC_PASSWORD
oldboy

rsync客户端nfs01配置完成


(3)守护进程模式,客户端rsync的命令语法:

配置服务器端守护进程,实现数据传输:

1、服务器端守护进程。2、客户端执行命令

拉门 推门 思考:
pull,拉:从远端拉取到本地
语法1:(常用)
rsync [OPTION...] [USER@]HOST::SRC... [DEST]
rsync命令 参数选项 [虚拟用户]@[主机地址]::[模块名] 本地路径

语法2:
rsync [OPTION...] rsync://[USER@]HOST/SRC... [DEST]
rsync命令 参数选项 rsync://[虚拟用户]@[主机地址]/[模块名] 本地路径


push,推:从本地推动远端
语法1:
rsync [OPTION...] [DEST] [USER@]HOST::SRC...
rsync命令 参数选项 本地路径 [认证用户]@[主机地址]:[目标路径]

语法2:
rsync [OPTION...] [DEST] rsync://[USER@]HOST::SRC...
rsync命令 参数选项 本地路径 rsync://[认证用户]@[主机地址]:[目标路径]


(4)测试成果

错误1:

[root@nfs01 ~]$ rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
@ERROR: invalid uid rsync

解答:

[root@backup ~]$ useradd rsync
[root@backup ~]$ id rsync
uid=1002(rsync) gid=1002(rsync) 组=1002(rsync)
[root@backup ~]$ mkdir -p /backup
[root@backup ~]$ chown -R rsync.rsync /backup/
[root@backup ~]$ ls -ld /backup
drwxr-xr-x 2 rsync rsync 6 2月 21 21:54 /backup

 

错误2:

[root@nfs01 ~]$ rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts
rsync: chgrp ".hosts.dHrnTj" (in backup) failed: Operation not permitted (1)

sent 223 bytes received 124 bytes 694.00 bytes/sec
total size is 330 speedup is 0.95
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1178) [sender=3.1.2]

解答:增加如下参数到/etc/rsyncd.conf
fake super = yes #不用root用户也可以存储文件的完整属性。
This allows the full attributes of a file to be stored without having to have the daemon actually running as root.

[root@backup ~]$ grep fake /etc/rsyncd.conf
fake super = yes

改完配置,要重启服务

[root@backup ~]$ systemctl restart rsyncd

再测试,成功

[root@nfs01 ~]$ rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password
sending incremental file list
hosts

sent 89 bytes received 49 bytes 276.00 bytes/sec
total size is 330 speedup is 2.39

[root@nfs01 ~]$ rsync -avz /etc rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.password

服务端检查;

[root@backup ~]$ ls /backup
hosts
[root@backup ~]$ ls /backup
etc hosts

 

如客户端按照环境变量的方式配置,则可以忽略--password-file=/etc/rsync.password参数

[root@nfs01 ~]$ rsync -avz /etc rsync_backup@172.16.1.41::backup
sending incremental file list

sent 51,688 bytes received 646 bytes 34,889.33 bytes/sec
total size is 31,635,536 speedup is 604.49
[root@nfs01 ~]$ touch /etc/oldboy.txt
[root@nfs01 ~]$ toursync -avz /etc rsync_backup@172.16.1.41::backup
sending incremental file list
etc/
etc/oldboy.txt

sent 51,748 bytes received 660 bytes 104,816.00 bytes/sec
total size is 31,635,536 speedup is 603.64

[root@nfs01 ~]$ rsync -avz /etc rsync://rsync_backup@172.16.1.41/backup
sending incremental file list

sent 51,717 bytes received 646 bytes 34,908.67 bytes/sec
total size is 31,635,536 speedup is 604.16
posted @ 2020-02-19 23:02  丁海龙  阅读(159)  评论(0)    收藏  举报