3. Rsync备份服务-上
1、Rsync基本概述
rsync是一款开源的备份工具,可以在不同主机之间进行同步,可实现全量备份与增量备份,因此非常适合用于架构集中式备份或异地备份等应用。
rsync官方地址:传送门
rsync监听端口:873
rsync运行模式:C/S
既然需要备份,那么我们应该了解备份的方式:
完全备份
增量备份
假设客户端上有 file1 file2 file3 文件,服务端上有 file1 文件,现要将客户端上的数据备份到服务端
hostnamectl set-hostname nfs
替换IP地址:
sed -i 's#55#72#g' /etc/sysconfig/network-scripts/ifcfg-eth[01]
1012 mount -t auto /dev/cdrom /mnt/cdrom/ 1013 ls /mnt/cdrom/ 1014 yum install tree -y 1015 yum clean all 1016 yum makecache 1017 yum update 1018 ls /etc/yum.repos.d/ 1019 cat /etc/yum.repos.d/CentOS-Media.repo 1020 cd /etc/yum.repos.d/ 1023 cat CentOS-Base.repo.bak 1024 rm CentOS-Base.repo 1027 yum clean all 1028 yum update 1029 yum install tree -y 1030 yum install rsync -y 1031 rsync -avz ./CentOS-Media.repo root@10.95.29.71:/ 1032 scp ./CentOS-Media.repo root@10.95.29.71:/ 1033 history [root@manager /etc/yum.repos.d]$ rsync -avz ./CentOS-Media.repo root@10.95.29.71:/ root@10.95.29.71's password: sending incremental file list CentOS-Media.repo sent 447 bytes received 35 bytes 192.80 bytes/sec total size is 628 speedup is 1.30
rsync 使用三种主要的数据传输方式
本地方式
远程方式
守护进程
Local:rsync [
rsync 命令
OPTION 选项
DEST 目标文件
上传
[root@manager ~]$ rsync -avz ./CentOS-Media.repo root@10.95.29.71:/
下载
使用 10.95.29.71 的root 身份拷贝 /CentOS-Media.repo 到本地当前目录下
[root@manager /]$ rsync -avz root@10.95.29.71:/CentOS-Media.repo ./ root@10.95.29.71's password: receiving incremental file list CentOS-Media.repo
sent 43 bytes received 447 bytes 326.67 bytes/sec total size is 628 speedup is 1.28 [root@manager /]$ ls app bin boot CentOS-Media.repo data dev etc exam home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var [root@manager /]$
rsync 命令
avz 选项
root 系统用户
Host IP地址
:SRC 源路径
DEST 目录位置
[root@manager ~]$ touch backup_$(date +%F) [root@manager ~]$ ll total 12 -rw-------. 1 root root 1548 Oct 8 12:28 anaconda-ks.cfg -rw-r--r--. 1 root root 0 Dec 23 15:57 backup_2019-12-23 -rw-r--r--. 1 root root 0 Nov 19 10:37 df.txt -rw-r--r--. 1 root root 4218 Dec 2 19:33 police2.md5
上传本地的 nfs文件到10.95.29.71服务器上root目录下,使用root用户
[root@nfs ~]$ rsync -avz nfs root@10.95.29.71:/root The authenticity of host '10.95.29.71 (10.95.29.71)' can't be established. ECDSA key fingerprint is SHA256:wVvLrxsJzJme+qbAooYXczrhDndQ6SvsvMj4DMF99YQ. ECDSA key fingerprint is MD5:53:eb:81:3e:0c:ad:8a:66:a7:59:41:6e:9e:f2:c4:f9. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.95.29.71' (ECDSA) to the list of known hosts. root@10.95.29.71's password: sending incremental file list nfs sent 81 bytes received 35 bytes 17.85 bytes/sec total size is 0 speedup is 0.00 [root@nfs ~]$ ls anaconda-ks.cfg df.txt nfs
[root@backup ~]$ ls
anaconda-ks.cfg backup_2019-12-23 df.txt nfs
下载10.95.2.71服务器上的backup文件到当前目录
[root@nfs ~]$ rsync -avz root@10.95.29.71:/root/backup ./
root@10.95.29.71's password:
receiving incremental file list
backup
sent 43 bytes received 85 bytes 85.33 bytes/sec
total size is 0 speedup is 0.00
[root@nfs ~]$ ls
anaconda-ks.cfg backup df.txt nfs
拉取10.95.1=29.71服务器 /root整个目录
[root@nfs ~]$ rsync -avz root@10.95.29.71:/root ./
root@10.95.29.71's password:
receiving incremental file list
root/
root/.bash_history
root/.bash_logout
root/.bash_profile
root/.bashrc
root/.cshrc
root/.tcshrc
root/.viminfo
root/anaconda-ks.cfg
root/backup
root/backup_2019-12-23
root/df.txt
root/nfs
sent 256 bytes received 5,083 bytes 2,135.60 bytes/sec
total size is 13,118 speedup is 2.46
[root@nfs ~]$ ls
anaconda-ks.cfg backup df.txt nfs root
[root@nfs ~]$ ls root/
anaconda-ks.cfg backup backup_2019-12-23 df.txt nfs
[root@nfs ~]$ ll -a root/
total 40
dr-xr-x---. 2 root root 215 Dec 23 11:30 .
dr-xr-x---. 4 root root 214 Dec 23 11:40 ..
-rw-------. 1 root root 1548 Oct 8 12:28 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Dec 23 11:30 backup
-rw-r--r--. 1 root root 0 Dec 23 11:17 backup_2019-12-23
-rw-------. 1 root root 10301 Dec 23 11:16 .bash_history
-rw-r--r--. 1 root root 18 Dec 28 2013 .bash_logout
-rw-r--r--. 1 root root 176 Dec 28 2013 .bash_profile
-rw-r--r--. 1 root root 176 Dec 28 2013 .bashrc
-rw-r--r--. 1 root root 100 Dec 28 2013 .cshrc
-rw-r--r--. 1 root root 0 Nov 19 10:37 df.txt
-rw-r--r--. 1 root root 0 Dec 23 11:26 nfs
-rw-r--r--. 1 root root 129 Dec 28 2013 .tcshrc
-rw-------. 1 root root 670 Dec 1 08:55 .viminfo
拉取10.95.1=29.71服务器 /root目录下的所有文件
[root@nfs ~]$ rsync -avz root@10.95.29.71:/root/ ./
root@10.95.29.71's password:
receiving incremental file list
./
.bash_history
.viminfo
backup_2019-12-23
sent 180 bytes received 1,101 bytes 512.40 bytes/sec
total size is 13,118 speedup is 10.24
[root@nfs ~]$ ls
anaconda-ks.cfg backup backup_2019-12-23 df.txt nfs root
rsync借助SSH协议同步数据存在的缺陷
1、使用系统用户(不安全)
2、使用普通用户(会导致权限不足情况)
1、为什么要做备份?
保证数据不丢失
便于快速的恢复
2、备份怎么做?
windows: 外部存储设备复制,网盘备份
3、备份的策略
全量备份:效率低下,占用空间
增量备份:提高备份效率,节省空间
4、备份的方式?
上传,将电脑里的内容传输到云盘 push 推
下载,从云盘下载内容到本地 pull 拉
推:所有主机推送本地数据到Rsync备份服务器,会导致数据同步缓慢(适合少量数量备份)

拉:rsync 备份服务器端拉取所有主机上的数据,会导致备份服务器开销大

大量服务器备份场景

异地备份实现思路

[root@nfs ~]$ man rsync
NAME rsync - a fast, versatile, remote (and local) file-copying tool SYNOPSIS Local: rsync [OPTION...] SRC... [DEST] Access via remote shell: Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST] Push: rsync [OPTION...] SRC... [USER@]HOST:DEST 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 Usages with just one SRC arg and no DEST arg will list the source files instead of copying.
测试软件来源于哪个yum包
[root@nfs ~]$ yum provides rsync Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * c7-media: rsync-3.1.2-4.el7.x86_64 : A program for synchronizing files over a network Repo : c7-media rsync-3.1.2-4.el7.x86_64 : A program for synchronizing files over a network Repo : @c7-media rpm -qc rsync 获取软件的配置文件 [root@nfs ~]$ rpm -qc rsync /etc/rsyncd.conf /etc/sysconfig/rsyncd [root@nfs ~]$
配置 /etc/rsync.conf
[root@nfs ~]$ cat /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 # exclude = lost+found/ # transfer logging = yes # timeout = 900 # ignore nonreadable = yes # dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # [ftp] # path = /home/ftp # comment = ftp export area
vim 中dG 清空文件内容
ps -aux 查看进程
服务器端操作 rsync-server
1、安装 rsync
yum install rsync -y
2、配置
[root@backup ~]$ rpm -qc rsync
/etc/rsyncd.conf
[root@backup ~]$ cat /etc/rsyncd.conf uid = rsync gid = rsync port = 873 #fake super = yes use chroot = no max connections = 200 timeout = 600 ignore errors read only = false list = true auth users = rsync_backup secrets file = /etc/rsync.passwd log file = /var/log/rsyncd.log ###################################### [backup] comment = welcome to exam backup! path = /backup
3、创建rsync 进程运行的普通用户
[root@backup ~]$ useradd -M -s /sbin/nologin rsync
4、创建虚拟用户与密码文件,主要用来提供客户端连接时间使用的身份
[root@backup ~]$ echo "rsync_backup:1" > /etc/rsync.passwd [root@backup ~]$ cat /etc/rsync.passwd rsync_backup:1
[root@backup ~]$ chmod 600 /etc/rsync.passwd
5、创建存放备份数据的目录
[root@backup ~]$ mkdir /backup [root@backup ~]$ chown -R rsync.rsync /backup/
6、启动
[root@backup ~]$ systemctl start rsyncd [root@backup ~]$ systemctl enable rsyncd Created symlink from /etc/systemd/system/multi-user.target.wants/rsyncd.service to /usr/lib/systemd/system/rsyncd.service. [root@backup ~]$ systemctl status rsyncd ● rsyncd.service - fast remote file copy program daemon Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; enabled; vendor preset: disabled) Active: active (running) since Mon 2019-12-23 12:42:37 EST; 1min 0s ago Main PID: 15605 (rsync) CGroup: /system.slice/rsyncd.service └─15605 /usr/bin/rsync --daemon --no-detach
Dec 23 12:42:37 backup systemd[1]: Started fast remote file copy program daemon.
Dec 23 12:42:37 backup systemd[1]: Starting fast remote file copy program daemon...
Dec 23 12:42:37 backup rsyncd[15605]: params.c:Parameter() - Ignoring badly formed line in config file: ignore errors
Dec 23 12:42:37 backup rsyncd[15605]: rsyncd version 3.1.2 starting, listening on port 873
7、测试
[root@backup ~]$ netstat -lnt Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::873 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN
重启服务
[root@backup ~]$ systemctl restart rsyncd
客户端操作 nfs-server
1、准备数据
[root@nfs ~]$ mkdir backup_$(date +%F) [root@nfs ~]$ ls backup_2019-12-23
2、测试推送、拉取
[root@nfs ~]# rsync -avz backup_2019-12-25 rsync_backup@172.26.73.196::backup
Password:
sending incremental file list
sent 96 bytes received 25 bytes 80.67 bytes/sec
total size is 8 speedup is 0.07
[root@nfs ~]# rsync -avz rsync_backup@172.26.73.196::backup ./
Password:
receiving incremental file list
./
backup_2019-12-25/
backup_2019-12-25/1225.txt
sent 54 bytes received 213 bytes 178.00 bytes/sec
total size is 13 speedup is 0.05
[root@nfs ~]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 25 18:06 backup_
drwxr-xr-x 2 root root 4096 Dec 25 19:00 backup_2019-12-25
[root@nfs ~]# ll backup_2019-12-25/
total 8
-rw-r--r-- 1 root root 5 Dec 25 19:00 1225.txt
-rw-r--r-- 1 root root 8 Dec 25 18:09 txt.txt

指定排除某个文件
[root@backup backup]# ll total 4 drwxr-xr-x 2 rsync rsync 4096 Dec 25 19:00 backup_2019-12-25 [root@backup backup]# ll backup_2019-12-25/ total 4 -rw-r--r-- 1 rsync rsync 5 Dec 25 19:00 1225.txt [root@backup backup]#
指定排除文件的列表,写入一个文件中
[root@nfs ~]# rsync -avzP --exclude-from=pc backup_2019-12-25 rsync_backup@172.26.73.196::backup Password: sending incremental file list backup_2019-12-25/ sent 75 bytes received 28 bytes 68.67 bytes/sec total size is 0 speedup is 0.00 [root@nfs ~]# cat pc txt.txt 1225.txt [root@nfs ~]#
rsync 命令对应选项
-a 归档模式传输,等于-tropgDL
-v 详细模式输出,打印速率,文件数量等
-z 传输时进行压缩以提高效率
-r 递归传输目录及目录,即目录下的所有目录都同样传输
-t 保持文件时间信息
-o 保持文件属主信息
-p 保持文件权限
-g 保持文件改组信息
-l 保留软件连接
-P 显示同步的过程以传输时的进度等信息
-D 保持设备文件信息
-L 保留软件连接指向的目标文件
-e 使用的信道协议,指定替代rsh 的shell程序
--exclude=PATTERN 指定排除不需要传输的文件模式
--exclude-from=file 文件名所在的目录文件
--bwlimit=100 限速传输
--partial 断点续传
--delete 让目标目录和源目录数据保持一致
[root@backup backup]# mkdir /backup/{2019-11-11,2019-12-12,2-10-13-13} -p [root@backup backup]# ll total 16 drwxr-xr-x 2 root root 4096 Dec 25 19:21 2019-11-11 drwxr-xr-x 2 root root 4096 Dec 25 19:21 2019-12-12 drwxr-xr-x 2 root root 4096 Dec 25 19:21 2-10-13-13 drwxr-xr-x 2 rsync rsync 4096 Dec 25 19:15 backup_2019-12-25
[root@nfs ~]# dd if=/dev/zero of=/opt/1.disk bs=1M count=1000
1000+0 records in
1000+0 records out
1048576000 bytes (1.0 GB) copied, 4.02303 s, 261 MB/s
[root@nfs ~]# ps aux|grep rsync root 1547 0.0 0.0 112704 972 pts/0 S+ 19:24 0:00 grep --color=auto rsync [root@nfs ~]# ps aux|grep rsync root 1549 0.0 0.0 112704 968 pts/0 S+ 19:25 0:00 grep --color=auto rsync [root@nfs ~]# kill -9 1550
--delete
客户端推送数据到服务器,加上--delete 参数
[root@nfs ~]$ rsync -avz --delete backup_2019-12-23 rsync_backup@10.95.29.71::backup/
注意:客户端强制让服务端与其一致,如果有差异以客户端为准
客户端拉取服务端的数据,加上--delete 参数
[root@nfs ~]# rsync -avz --delete rsync_backup@10.05.29.71::bakup
注意:服务端强制让客户端的目录与其一致,如果有差异以服务端为准
--bwlimit 限速,不允许添加单位,默认MB
[root@nfs ~]# rsync -avzP --bwlimit 5 /opt/1.disk rsync_backup@172.26.73.196::backup Password: sending incremental file list 1.disk

指定一个密码文件,密码文件中写入对应的密码即可 “1”
[root@nfs ~]# echo "1" >/etc/rsync.pass [root@nfs ~]# chmod 600 /etc/rsync.pass [root@nfs ~]# cat /etc/rsync.pass 1
如果不授权600 权限,会提示如下错误
ERROR:passwork file must not be other-accessible
使用变量方式【适合编写定时指定推送脚本使用】
[root@nfs ~]# export RSYNC_PASSWORD=1 [root@nfs ~]# rsync -avzP /opt/1.disk rsync_backup@172.26.73.196::backup

小结
Rsync 基本概念
跨平台 linux---linux linux ----windows
备份方式
全量 增量
备份的方式
推送 push
拉取 pull
1、本地(客户端)推送云盘(备份)
2、云盘拉取本地数据(定时)
3、大规模机器的场景
4、异地备份方式 学校--推送--云盘 ----回家---拉取--数据
Rsync 传输模式
本地传输 类似于cp
远程传输 需要借助ssh协议,同时需要使用系统用户,不安全
守护进程 使用是虚拟用户,使用rsync协议,更加的安全
Rsync 守护进程
1、安装
2、配置
3、启动
4、检查
5、测试
Rsync 相关参数
-avz -az 常用
--bwlimit 限速
--delete 强制保持一致
-P 显示传输速率
-exclude 排除单个文件
--exclude-from=file 将排除的内容写入文件中
浙公网安备 33010602011771号