Rsync远程同步

1.Rsync基本概述

1.什么是备份?

备份就是把文件在复制一份存放起来,(简单说就是给原文件增加一个副本)

2.为什么要做备份?

数据非常重要(手机备忘录信息 相片)
保证数据不丢失
便于快速的恢复

3.能不能不做备份?

可以,对于不是特别重要的数据可以不考虑
a公司:什么都不用备份,配置文件都存储在ansible中
b公司:数据库数据、脚本、核心节点的文件重要ansible

4.备份应该怎么做?

完全备份:全备,效率低下、占用空间
增量备份:增备,效率 较高,节省空间
在这里插入图片描述

5.备份通常使用什么工具?

本地备份:cp
远程备份:scp rsync
在这里插入图片描述

6.什么是Rsync?

rsync简称远程同步,可以实现不同主机之间的同步,同时支持增量和全量的备份。
rsync:官方地址:传送门
rsync监听的端口:873
rsync运行模式:C/S 客户端与服务端直接的关系
在这里插入图片描述

2.Rsync应用场景

前面我们了解过备份的方式,下面我们需要了解Rsync的数据同步模式
1.推:所有主机推送本地数据至Rsync备份服务器,会导致数据同步缓慢(适合少量数据备份)
在这里插入图片描述
在这里插入图片描述

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

在这里插入图片描述

3.大量服务器备份场景

在这里插入图片描述

4.异地备份实现思路

在这里插入图片描述

3.Rsync传输模式

Rsync使用三种主要的数据传输方式
本地方式 :和cp差不多
远程方式 :可以在机器之间互相传输数据 (推送和拉取) —》依赖于ssh协议,系统root的密码
守护进程: 将rsync运行为一个服务,对外提供。 (暴漏端口)

1.安装Rsync命令

yum  -y  install  rsync  

2.检查是否安装Rsync命令

rpm -qa  rsync  

3.rsync有几种用法

本地传输:

       Local:  rsync [OPTION...] SRC... [DEST]
        Local:                                    本地
        rsync                                       命令
         [OPTION...]                         选项 
         SRC...                                  源
         [DEST]                                  目标
  总结:使用rsync命令 将什么文件 拷贝到  什么路径下    
    命令       选项                   源              	     目标
  rsync         -avz             /etc/hosts                /tmp         #增量
  cp           		-rp            /etc/hosts              /tmp       #全量
  [root@backup ~]# rsync -avz  /etc/hosts /tmp
sending incremental file list
hosts
sent 136 bytes  received 35 bytes  342.00 bytes/sec
total size is 158  speedup is 0.92
再次拷贝一遍
[root@backup ~]# rsync -avz  /etc/hosts /tmp
sending incremental file list
sent 38 bytes  received 12 bytes  100.00 bytes/sec
total size is 158  speedup is 3.16
把/etc/hosts文件追加内容 再次拷贝文件
[root@backup ~]# echo  "##"  >>/etc/hosts
[root@backup ~]# rsync -avz  /etc/hosts /tmp
sending incremental file list
hosts
sent 144 bytes  received 35 bytes  358.00 bytes/sec
total size is 161  speedup is 0.90

远程传输: A 和 B 两太机器传输 跨主机的传输方式

       Access via remote shell:
         下载Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
          上传Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
   推送:
 10.0.0.41 客户端 (推送)      10.0.0.31 服务端  (接收)   
 上传Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
 rsync                    命令
 [OPTION...]         选项
 SRC...                  源文件
 [USER@]HOST: [DEST]   拷贝到哪个主机的哪个目录  使用他的什么用户身份
#文件  将41服务器的/etc/hosts文件,推送到31服务器的/tmp目录,使用的是31服务器的root密码
 [root@backup ~]# rsync -avz /etc/hosts  root@172.16.1.31:/tmp
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:DGeuJ9SOABLouTzNwcAya8tjXEZiH5pvW2ldwyrHZ+w.
ECDSA key fingerprint is MD5:1f:dd:10:52:fe:80:d2:c8:9d:91:db:26:74:eb:0c:15.
Are you sure you want to continue connecting (yes/no)? yes   
Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.
root@172.16.1.31's password: 
sending incremental file list
hosts
sent 143 bytes  received 35 bytes  18.74 bytes/sec
total size is 159  speedup is 0.89
再到10.0.0.31主机上查看拷贝过来的文件
[root@nfs ~]# ll  /tmp/
total 4
-rw-r--r--. 1 root root 159 Mar 17 16:38 hosts
drwx------. 2 root root   6 Mar 18 00:04 vmware-root_830-2999133150
#目录  将41服务器的/opt目录以及目录下的所有文件都推送到31服务器的/tmp目录 使用的是31服务器的root用户身份
[root@backup ~]# rsync -avz  /opt   root@172.16.1.31:/tmp
[root@backup ~]# ls  /opt/
[root@backup ~]# touch   /opt/1.txt
[root@backup ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Mar 17 16:55 1.txt
[root@backup ~]# ll  /opt/
total 0
-rw-r--r--. 1 root root 0 Mar 17 16:55 1.txt
[root@backup ~]# rsync -avz  /opt   root@172.16.1.31:/tmp
root@172.16.1.31's password: 
sending incremental file list
opt/
opt/1.txt
sent 115 bytes  received 39 bytes  61.60 bytes/sec
total size is 0  speedup is 0.00
在31服务器上查看41服务器推送过来的目录 
[root@nfs ~]# ll  /tmp
total 4
-rw-r--r--. 1 root root 159 Mar 17 16:38 hosts
drwxr-xr-x. 2 root root  19 Mar 17 16:55 opt
drwx------. 2 root root   6 Mar 18 00:04 vmware-root_830-299913315

拉取:
10.0.0.41 客户端 (推送) 10.0.0.31 服务端 (接收)
文件:拉取31服务器的/tmp/3.txt文件,至41服务器的本地的/opt目录

下载Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
 rsync                         命令
 [OPTION...]                  选项
 [USER@]HOST:SRC...   
[root@backup ~]# rsync  -avz root@172.16.1.31:/tmp/3.txt  /opt
root@172.16.1.31's password: 
receiving incremental file list
3.txt
sent 43 bytes  received 83 bytes  50.40 bytes/sec
total size is 0  speedup is 0.00
[root@backup ~]# ll /opt/
total 0
-rw-r--r--. 1 root root 0 Mar 17 16:55 1.txt
-rw-r--r--. 1 root root 0 Mar 18 08:14 3.txt

目录:拉取31服务器的/tmp目录以及目录下的内容,至41服务器本地的/opt目录下

[root@backup ~]# rsync -avz root@172.16.1.31:/tmp /opt
root@172.16.1.31's password: 
receiving incremental file list
tmp/
tmp/3.txt
tmp/hosts
tmp/.ICE-unix/
tmp/.Test-unix/
tmp/.X11-unix/
tmp/.XIM-unix/
tmp/.font-unix/
tmp/opt/
tmp/opt/1.txt
tmp/vmware-root_830-2999133150/
sent 121 bytes  received 536 bytes  438.00 bytes/sec
total size is 159  speedup is 0.24
[root@backup ~]# ll  /opt/
total 0
-rw-r--r--. 1 root root   0 Mar 17 16:55 1.txt
-rw-r--r--. 1 root root   0 Mar 18 08:14 3.txt
drwxr-xr-x. 9 root root 164 Mar 18 08:32 tmp

注意:在推送或拉取目录时,要注意 / 斜线根问题
1.root: 权限太大,所有人都知道root的密码
2.oldboy:权限太小,出现权限拒绝的原因。

守护进程:(不需要使用系统的真实用户,可以自行diy一个用户,来实现数据的推送和拉取的,变成一个服务监听某个端口上面,客户端加上IP和端口访问这个服务) 要想把一个程序变成一个服务,只需要三步:1.安装2.配置(让软件按照我们所设定的预期去运行)3启动 4测试(客户端使用拉取或推送的方式进行测试)

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

服务端是10.0.0.41 内网地址:172.16.1.41

1.安装

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

2.配置 (文件)

[root@backup ~]# rpm -qc rsync 
/etc/rsyncd.conf
/etc/sysconfig/rsyncd
---------------------------------------------------------  
[root@backup ~]# cat  /etc/rsyncd.conf  -------------------源文件 rsync守护进程文件
# /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
-----------------------------------------------------------------------------------------------
[root@backup ~]# cat  /etc/rsyncd.conf 
uid = rsync                                         #rsync进程是使用rsync用户身份运行
gid = rsync
port = 873 										#默认监听的端口是873
fack super = yes								#不用root身份也可以存储完整的数据以及数据属性
use chroot = no								#固定在某一个目录中
max connections = 200					#最大连接数
timeout = 600									#连接超时时间
ignore errors									#忽略错误
read only = false								#可以使用读和写操作
list = false									
auth users = rsync_backup				#自行定义假的用户(不存在于系统中)
secrets file = /etc/rsync.passwd		#存储假用户的密码 username:passwd
log file = /var/log/rsyncd.log			#日志
########################################
[backup]												# 模块名称
comment = welcome to backup server    #描述的意思
path = /backup 										 #模块名称对应的具体目录

3.根据配置文件,初始化操作

	3.1)创建rsync用户(主要是rsync进程运行时需要使用的身份)
	[root@backup ~]# useradd rsync -r -M
	[root@backup ~]# tail -1 /etc/passwd
    rsync:x:998:996::/home/rsync:/bin/bash

	3.2)创建密码文件/etc/rsync.passwd  密码文件中要填写 username:passwd  用户要与配置文件中定义的保持
	[root@backup ~]# echo "rsync_backup:1" > /etc/rsync.passwd
    [root@backup ~]# cat /etc/rsync.passwd 
     rsync_backup:1
     -------------------------------密码文件调为600权限
     [root@backup ~]# ll /etc/rsync.passwd 
  -rw-r--r--. 1 root root 15 Mar 31 17:53 /etc/rsync.passwd
    [root@backup ~]# chmod 600 /etc/rsync.passwd 

	3.3)创建存储数据的目录:/backup
       [root@backup ~]# mkdir  /backup

4.启动

[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 ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:873             0.0.0.0:*               LISTEN      1807/rsync          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1298/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1422/master         
tcp6       0      0 :::873                  :::*                    LISTEN      1807/rsync          
tcp6       0      0 :::22                   :::*                    LISTEN      1298/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1422/master  

5.测试(客户端使用推送或拉取的方式进行)

10.0.0.31 模拟客户端进行推送测试: Push: rsync [OPTION…] SRC… [USER@]HOST::DEST
测试过程中出现的错误提示:客户端的rsyncd服务没开启
[root@nfs ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.31::backup
rsync: failed to connect to 172.16.1.31 (172.16.1.31): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(125) [sender=3.1.2]
解决方法:开启客户端的rsyncd服务
[root@nfs ~]# systemctl start rsyncd.service
[root@nfs ~]# systemctl status rsyncd.service
● rsyncd.service - fast remote file copy program daemon
Loaded: loaded (/usr/lib/systemd/system/rsyncd.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2022-03-31 18:24:43 CST; 2s ago
Main PID: 1808 (rsync)
CGroup: /system.slice/rsyncd.service
└─1808 /usr/bin/rsync --daemon --no-detach

Mar 31 18:24:43 nfs systemd[1]: Started fast remote file copy program daemon.
Mar 31 18:24:43 nfs rsyncd[1808]: rsyncd version 3.1.2 starting, listening on port 873
错误提示:
[root@nfs ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
Password: Permission denied (13) 权限拒绝
sending incremental file list
hosts
rsync: mkstemp “.hosts.QSthVZ” (in backup) failed: Permission denied (13)

sent 140 bytes received 121 bytes 104.40 bytes/sec
total size is 158 speedup is 0.61
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
解决方法:把模块/backup目录的root改成rsync rsync用户身份
[root@backup ~]# chown -R rsync.rsync /backup/
错误提示:服务端rsync对目录操作权限不足
[root@nfs ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
rsync: ERROR: cannot stat destination “.” (in backup): Permission denied (13)
rsync error: errors selecting input/output files, dirs (code 3) at main.c(635) [Receiver=3.1.2]
解决方法:修改对应目录权限755
[root@backup ~]# chmod 755 /backup/
[root@backup ~]# ls -ld /backup/
drwxr-xr-x. 2 rsync rsync 6 Mar 31 18:40 /backup/
出现错误提示:这个可能是配置文件里面没有加fake super = yes 在以前的版本里面不需要,现在版本需要
rsync: chgrp “.hosts.doNRPT” (in backup) failed: Operation not permitted (1)
解决方法:
配置文件fake super = yes 问题导致的

在10.0.0.31上推送目录/etc/hosts 到10.0.0.41服务器上backu模块
[root@nfs ~]# rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
hosts

sent 140 bytes received 43 bytes 52.29 bytes/sec
total size is 158 speedup is 0.86
在10.0.0.41服务器上查看模块backup目录推送过来的数据
[root@backup ~]# ll /backup/
total 4
-rw-r–r–. 1 rsync rsync 158 Jun 7 2013 hosts


rsync命令对应选项
-a #归档模式传输,等于-tropgD1
-v # 详细模式传输,打印速率,文件数量等
-z # 传输时进行压缩以提高效率
-r # 递归传输目录及子目录,及目录下得所有目录都同样传输
-t # 保持文件时间信息
-o #保持文件属主信息
-p #保持文件权限
-g #保持文件属组信息
-l #保持软链接
-p #显示同步的过程及传输时的进度等信息
-D #保持设备文件信息
-L #保留软链接指向的目标文件
-e #使用的信道协议,指定替代rsh的shell程序
–exclude=PATTERN #指定排除不需要传输的文件模式
–exclude-from=file #文件名所在的目录文件
–bwlimit=100 #限速传输
–delete #让目标目录和源目录数据保持一致
1.–delete
推送:客户端100文件 ,服务端200文件 ,服务端最后就剩下客户端的100文件,高度和客户端一致
在客户端10.0.0.31服务器的/mnt目录下创建10个文件推送到服务端10.0.0.41的backup模块目录
[root@nfs ~]# touch /mnt/file-{1…10}
[root@nfs ~]# ls /mnt/
file-1 file-10 file-2 file-3 file-4 file-5 file-6 file-7 file-8 file-9
[root@nfs ~]# rsync -avz /mnt/ rsync_backup@10.0.0.41::backup
Password:
sending incremental file list
./
file-1
file-10
file-2
file-3
file-4
file-5
file-6
file-7
file-8
file-9

sent 550 bytes received 217 bytes 219.14 bytes/sec
total size is 0 speedup is 0.00
在服务端10.0.0.41服务器上查看backup目录有十个文件
[root@backup ~]# ll /backup/
total 4
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-1
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-10
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-2
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-3
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-4
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-5
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-6
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-7
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-8
-rw-r–r–. 1 rsync rsync 0 Apr 1 00:14 file-9
-rw-r–r–. 1 rsync rsync 158 Jun 7 2013 hosts
使用–delete参数选项高度与客户端10.0.0.31服务器上的/mnt目录保持一致
[root@nfs ~]# rsync -avz --delete /mnt/ rsync_backup@10.0.0.41::backup
拉取:客户端100文件,服务端200文件,客户端最终剩下200个服务端的文件,与服务端保持一致
在客户端10.0.0.31上拷贝/etc/目录所有的到/mnt目录下 使用–delete参数选项高度与服务端保持一致
[root@nfs ~]# cp /etc/* /mnt/
[root@nfs ~]# rsync -avz --delete rsync_backup@10.0.0.41::backup /mnt/
2.–bwlimit 限速 按照MB限制
案例:先来个推送的模式
客户端10.0.0.31服务器上模拟一个文件500M的文件
[root@nfs ~]# dd if=/dev/zero of=/opt/bigdata bs=1M cont=500
dd: unrecognized operand ‘cont=500’
Try ‘dd --help’ for more information.
[root@nfs ~]# dd if=/dev/zero of=/opt/bigdata bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 2.64447 s, 198 MB/s
[root@nfs ~]# du -sh /opt/bigdata
500M /opt/bigdata
[root@nfs ~]# rsync -avzP /opt/bigdata rsync_backup@172.16.1.41::backup P:加上大P是显示传输速率
Password:
sending incremental file list
bigdata
410,058,752 78% 195.61MB/s 0:00:00
开启限速功能 加上–bwlimit限制传输速率为10M/S
[root@nfs ~]# rsync -avzP --bwlimit 10 /opt/bigdata rsync_backup@172.16.1.41::backup
Password:
sending incremental file list
bigdata
152,174,592 29% 9.87MB/s 0:00:36
3.客户端输入密码,无法做到非交互,客户端非交互传输数据至服务端
(3.1):RSYNC_PASSWORD
[root@nfs ~]# export RSYNC_PASSWORD=1 这个只能在当前的bash窗口中使用,在打开一个bash窗口不生效 免密
[root@nfs ~]# rsync -avzP --bwlimit 10 rsync_backup@172.16.1.41::backup
(3.2)–password-file=/path/password (文件中仅可以存储客户端的密码,多一点都不行) 只需在客户端创建该文件即可
[root@nfs ~]# echo “1” > /etc/rsync.pass
[root@nfs ~]# rsync -avzP --bwlimit 10 /opt/bigdata rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.passERROR: password file must not be other-accessible
rsync error: syntax or usage error (code 1) at authenticate.c(196) [sender=3.1.2]
[root@nfs ~]# chmod 600 /etc/rsync.pass
[root@nfs ~]# rsync -avzP --bwlimit 10 /opt/bigdata rsync_backup@172.16.1.41::backup --password-file=/etc/rsync.passsending incremental file list
bigdata
152,174,592 29% 9.86MB/s 0:00:36
4.rsync和rsync_backup的区别:

4.已知3台服务器主机名分别为web01 backup nfs 主机信息见下表

Column 1Column 2
角色外网IP(NAT)
WEBeth0:10.0.0.7
NFSeth0:10.0.0.31
Rsynceth0:172.16.1.41

4.1客户端需求

1.客户端提前准备存放的备份的目录,目录规则如下:/backup/nfs_172.16.1.31_2022-04-01
2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/nfs_172.16.1.31_2022-04-01
3.客户端最后将备份的数据进行推送至备份服务器
4.客户端服务器本地保留最近7天的数据,避免浪费磁盘空间
5.客户端每天凌晨1点定时执行该脚本
[root@nfs ~]# hostname 获取主机名命令
nfs
[root@nfs ~]# ifconfig eth1|awk 'NR2 {print $2}’ 取IP地址命令
172.16.1.31
[root@nfs ~]# date +%F 取时间的命令
2022-04-01
(1)创建对应的目录
[root@nfs ~]# echo KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR
2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
nfs_172.16.1.31_2022-04-01
[root@nfs ~]# mkdir /backup/KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
在这里插入图片描述
(1)创建对应的目录
mkdir /backup/KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR
2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
(2)拷贝重要文件至该目录
cp /etc/hosts /backup/KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
(3)推送到指定的仓库
rsync -avz /backup/KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR
2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F) rsync_backup@172.16.1.41::backup
(4)创建对应的文件
[root@nfs ~]# mkdir -p /server/scripts
[root@nfs ~]# cat /server/scripts/client_push_server.sh
#1.创建对应的目录
mkdir KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
#2.拷贝重要文件至该目录
cp /etc/hosts KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR
2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
#3.推送到指定的仓库
export RSYNC_PASSWORD=1
rsync -avz KaTeX parse error: Expected group after '_' at position 11: (hostname)_̲(ifconfig eth1|awk 'NR2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F) rsync_backup@172.16.1.41::backup
[root@nfs ~]# sh /server/scripts/client_push_server.sh
mkdir: cannot create directory ‘nfs_172.16.1.31_2022-04-01’: File exists
sending incremental file list
nfs_172.16.1.31_2022-04-01/
nfs_172.16.1.31_2022-04-01/hosts
sent 200 bytes received 47 bytes 494.00 bytes/sec
total size is 158 speedup is 0.64
在服务端的10.0.0.41上查看/backup模块目录
[root@backup ~]# ll /backup/
drwxr-xr-x. 2 rsync rsync 19 Apr 1 14:40 nfs_172.16.1.31_2022-04-01
重新书写了变量
[root@nfs ~]# cat /server/scripts/client_push_server.sh
#定义变量
Path=/backup
Host= ( h o s t n a m e ) A d d r = (hostname) Addr= (hostname)Addr=(ifconfig eth1|awk 'NR
2 {print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲')_(date +%F)
Date= ( d a t e + D e s t = (date +%F) Dest= (date+Dest={Host}_KaTeX parse error: Expected group after '_' at position 7: {Addr}_̲{Date}
#1.创建对应的目录
mkdir -P P a t h / Path/ Path/Dest
#2.拷贝重要文件至该目录
cp /etc/hosts P a t h / Path/ Path/Dest
#3.推送到指定的仓库
export RSYNC_PASSWORD=1
rsync -avz P a t h / Path/ Path/Dest rsync_backup@172.16.1.41::backup
检查脚本[root@nfs ~]# sh -x /server/scripts/client_push_server.sh

  • Path=/backup
    ++ hostname
  • Host=nfs
    ++ ifconfig eth1
    ++ awk ‘NR==2 {print $2}’
  • Ip=172.16.1.31
    ++ date +%F
  • Date=2022-04-01
  • Dest=nfs_172.16.1.31_2022-04-01
  • mkdir -p /backup/nfs_172.16.1.31_2022-04-01
  • cp /etc/hosts /backup/nfs_172.16.1.31_2022-04-01
  • export RSYNC_PASSWORD=1
  • RSYNC_PASSWORD=1
  • rsync -avz /backup/nfs_172.16.1.31_2022-04-01 rsync_backup@172.16.1.41::backup
    sending incremental file list
    nfs_172.16.1.31_2022-04-01/
    nfs_172.16.1.31_2022-04-01/hosts
    sent 200 bytes received 47 bytes 494.00 bytes/sec
    total size is 158 speedup is 0.64
    批量产生数据30天,(会改变掉当前时间,做完测试记得修改回来)
    [root@nfs ~]# for i in {1…30};do date -s 2020/10/$i;sh /server/scripts/client_push_server.sh ;done
    检查backup目录出来30天的数据
    [root@nfs ~]# ll /backup/ -h
    total 0
    drwxr-xr-x. 2 root root 19 Oct 1 00:00 nfs_172.16.1.31_2020-10-01
    drwxr-xr-x. 2 root root 19 Oct 2 00:00 nfs_172.16.1.31_2020-10-02
    drwxr-xr-x. 2 root root 19 Oct 3 00:00 nfs_172.16.1.31_2020-10-03
    drwxr-xr-x. 2 root root 19 Oct 4 00:00 nfs_172.16.1.31_2020-10-04
    drwxr-xr-x. 2 root root 19 Oct 5 00:00 nfs_172.16.1.31_2020-10-05
    drwxr-xr-x. 2 root root 19 Oct 6 00:00 nfs_172.16.1.31_2020-10-06
    drwxr-xr-x. 2 root root 19 Oct 7 00:00 nfs_172.16.1.31_2020-10-07
    drwxr-xr-x. 2 root root 19 Oct 8 00:00 nfs_172.16.1.31_2020-10-08
    drwxr-xr-x. 2 root root 19 Oct 9 00:00 nfs_172.16.1.31_2020-10-09
    drwxr-xr-x. 2 root root 19 Oct 10 00:00 nfs_172.16.1.31_2020-10-10
    drwxr-xr-x. 2 root root 19 Oct 11 00:00 nfs_172.16.1.31_2020-10-11
    drwxr-xr-x. 2 root root 19 Oct 12 00:00 nfs_172.16.1.31_2020-10-12
    drwxr-xr-x. 2 root root 19 Oct 13 00:00 nfs_172.16.1.31_2020-10-13
    drwxr-xr-x. 2 root root 19 Oct 14 00:00 nfs_172.16.1.31_2020-10-14
    drwxr-xr-x. 2 root root 19 Oct 15 00:00 nfs_172.16.1.31_2020-10-15
    drwxr-xr-x. 2 root root 19 Oct 16 00:00 nfs_172.16.1.31_2020-10-16
    drwxr-xr-x. 2 root root 19 Oct 17 00:00 nfs_172.16.1.31_2020-10-17
    drwxr-xr-x. 2 root root 19 Oct 18 00:00 nfs_172.16.1.31_2020-10-18
    drwxr-xr-x. 2 root root 19 Oct 19 00:00 nfs_172.16.1.31_2020-10-19
    drwxr-xr-x. 2 root root 19 Oct 20 00:00 nfs_172.16.1.31_2020-10-20
    drwxr-xr-x. 2 root root 19 Oct 21 00:00 nfs_172.16.1.31_2020-10-21
    drwxr-xr-x. 2 root root 19 Oct 22 00:00 nfs_172.16.1.31_2020-10-22
    drwxr-xr-x. 2 root root 19 Oct 23 00:00 nfs_172.16.1.31_2020-10-23
    drwxr-xr-x. 2 root root 19 Oct 24 00:00 nfs_172.16.1.31_2020-10-24
    drwxr-xr-x. 2 root root 19 Oct 25 00:00 nfs_172.16.1.31_2020-10-25
    drwxr-xr-x. 2 root root 19 Oct 26 00:00 nfs_172.16.1.31_2020-10-26
    drwxr-xr-x. 2 root root 19 Oct 27 00:00 nfs_172.16.1.31_2020-10-27
    drwxr-xr-x. 2 root root 19 Oct 28 00:00 nfs_172.16.1.31_2020-10-28
    drwxr-xr-x. 2 root root 19 Oct 29 00:00 nfs_172.16.1.31_2020-10-29
    drwxr-xr-x. 2 root root 19 Oct 30 00:00 nfs_172.16.1.31_2020-10-30
    删除7天之前的文件,用find命令把7天之前的文件找出来删除
    [root@nfs ~]# find /backup/ -type d -mtime +7
    /backup/nfs_172.16.1.31_2020-10-01
    /backup/nfs_172.16.1.31_2020-10-02
    /backup/nfs_172.16.1.31_2020-10-03
    /backup/nfs_172.16.1.31_2020-10-04
    /backup/nfs_172.16.1.31_2020-10-05
    /backup/nfs_172.16.1.31_2020-10-06
    /backup/nfs_172.16.1.31_2020-10-07
    /backup/nfs_172.16.1.31_2020-10-08
    /backup/nfs_172.16.1.31_2020-10-09
    /backup/nfs_172.16.1.31_2020-10-10
    /backup/nfs_172.16.1.31_2020-10-11
    /backup/nfs_172.16.1.31_2020-10-12
    /backup/nfs_172.16.1.31_2020-10-13
    /backup/nfs_172.16.1.31_2020-10-14
    /backup/nfs_172.16.1.31_2020-10-15
    /backup/nfs_172.16.1.31_2020-10-16
    /backup/nfs_172.16.1.31_2020-10-17
    /backup/nfs_172.16.1.31_2020-10-18
    /backup/nfs_172.16.1.31_2020-10-19
    /backup/nfs_172.16.1.31_2020-10-20
    /backup/nfs_172.16.1.31_2020-10-21
    /backup/nfs_172.16.1.31_2020-10-22
    find命令结合xargs命令删除的命令
    [root@nfs ~]# find /backup/ -type d -mtime +7 |xargs rm -rf
    [root@nfs ~]# ll /backup/
    total 0
    drwxr-xr-x. 2 root root 19 Oct 23 00:00 nfs_172.16.1.31_2020-10-23
    drwxr-xr-x. 2 root root 19 Oct 24 00:00 nfs_172.16.1.31_2020-10-24
    drwxr-xr-x. 2 root root 19 Oct 25 00:00 nfs_172.16.1.31_2020-10-25
    drwxr-xr-x. 2 root root 19 Oct 26 00:00 nfs_172.16.1.31_2020-10-26
    drwxr-xr-x. 2 root root 19 Oct 27 00:00 nfs_172.16.1.31_2020-10-27
    drwxr-xr-x. 2 root root 19 Oct 28 00:00 nfs_172.16.1.31_2020-10-28
    drwxr-xr-x. 2 root root 19 Oct 29 00:00 nfs_172.16.1.31_2020-10-29
    drwxr-xr-x. 2 root root 19 Oct 30 00:00 nfs_172.16.1.31_2020-10-30
    定时推送数据
    crontab -e
    00 01 * * * /bin/bash /server/scripts/client_push_data_server.sh & >/dev/null

4.2服务端需求

1.服务端部署rsync,用于接收客户端推送过来的备份数据
2.服务端需要每天检验客户端推送过来的数据是否完整

md5------》文件----------------》校验码(随机的校验码唯一的)
md5sum  -c  校验码(唯一)
				ok 
[root@nfs ~]# md5sum anaconda-ks.cfg     
3abe7c822b939cfa7b47620f476f747f  anaconda-ks.cfg
[root@nfs ~]# md5sum /root/anaconda-ks.cfg 
3abe7c822b939cfa7b47620f476f747f  /root/anaconda-ks.cfg
[root@nfs ~]# md5sum anaconda-ks.cfg > flag.txt
[root@nfs ~]# cat flag.txt 
3abe7c822b939cfa7b47620f476f747f  anaconda-ks.cfg
[root@nfs ~]# md5sum -c flag.txt 
anaconda-ks.cfg: OK
如果文件 anaconda-ks.cfg删除一个内容在校验就出现的错误
[root@nfs ~]# md5sum -c flag.txt 
anaconda-ks.cfg: FAILED
md5sum: WARNING: 1 computed checksum did NOT match
[root@nfs ~]# cat /server/scripts/client_push_server.sh 
#定义变量
Path=/backup
Host=$(hostname)
Ip=$(ifconfig eth1|awk 'NR==2 {print $2}')
Date=$(date +%F)
Dest=${Host}_${Ip}_${Date}
#1.创建对应的目录
mkdir  -p  $Path/$Dest 
#2.将文件打包 存储对应的目录
cd / && \
tar zcf  $Path/$Dest/sys.tar.gz  etc/hosts etc/fstab   && \
tar zcf  $Path/$Dest/other.tar.gz   server/scripts/  var/spool/cron
#3.添加一个标记 md5
md5sum $Path/$Dest/sys.tar.gz  $Path/$Dest/other.tar.gz >$Path/$Dest/flag_$Date
#4.推送到指定的仓库
export RSYNC_PASSWORD=1
rsync -avz   $Path/$Dest   rsync_backup@172.16.1.41::backup  
#5.保留最近7天的数据:
find $Path/ -type d -mtime +7 |xargs rm -rf  
在服务端10.0.0.41服务器上的backup模块目录用md5sum检查一下文件的准确性
[root@backup ~]# cd /backup/
[root@backup backup]# ls 
nfs_172.16.1.31_2022-04-02
[root@backup backup]# cd nfs_172.16.1.31_2022-04-02/
[root@backup nfs_172.16.1.31_2022-04-02]# ll 
total 16
-rw-r--r--. 1 rsync rsync 162 Apr  2 10:12 flag_2022-04-02
-rw-r--r--. 1 rsync rsync 162 Apr  2 10:08 flag_nfs_172.16.1.31_2022-04-02
-rw-r--r--. 1 rsync rsync 657 Apr  2 10:12 other.tar.gz
-rw-r--r--. 1 rsync rsync 482 Apr  2 10:12 sys.tar.gz
[root@backup nfs_172.16.1.31_2022-04-02]# md5sum -c flag_2022-04-02 
/backup/nfs_172.16.1.31_2022-04-02/sys.tar.gz: OK
/backup/nfs_172.16.1.31_2022-04-02/other.tar.gz: OK

#5.保留最近7天的数据:
find $Path/ -type d -mtime +7 |xargs rm -rf
3.服务端需要每天检验的结果通知给管理员
4.服务端进保留6个月的备份数据,其余的全部删除
注意:所有服务器的备份目录必须都为/backup

1.将Linux服务器配置成邮箱:
[root@backup ~]# yum -y install mailx
2.借助qq邮箱发送,需要添加如下配置:
set from=1105816259@qq.com
set smtp=smtps://smtp.qq.com:465
set smtp-auth-user=1105816259@qq.com
set smtp-auth-password=tairntqslvfzjicj
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
3.测试邮件的发送:收件人 1105816259@qq.com
mail -s “测试一下是否成功” 1105816259@qq.com < /etc/hosts
4.服务端的脚本
[root@backup ~]# cat /server/scripts/server_data.sh
#定义变量
Path=/backup
Date=$(date +%F)
#1.服务端校验

md5sum -c $(find KaTeX parse error: Expected group after '_' at position 26: …e f -name "flag_̲(date +%F)") > $Path/result.txt

#2.将文件的结果通过邮箱的发送进行发送给管理人员
#2.1将服务器配置成邮箱
#2.2通过mail命令发送邮件给管理员
mail -s "Rsync Backup D e t e " 1105816259 @ q q . c o m < Dete" 1105816259@qq.com < Dete"1105816259@qq.com<Path/result.txt

#3.保留最近180天的数据
find $Path/ -type d -mtime +180 |xargs rm -rf
5.做一个整体的测试
客户端:
ntpdate ntp1.aliyun.com
rm -rf /backup/
服务端:
ntpdate ntp1.aliyun.com
rm -rf /backup/*
6.执行脚本
(1)先执行客户端脚本
[root@nfs ~]# sh /server/scripts/client_push_server.sh
sending incremental file list
nfs_172.16.1.31_2022-04-06/
nfs_172.16.1.31_2022-04-06/flag_2022-04-06
nfs_172.16.1.31_2022-04-06/other.tar.gz
nfs_172.16.1.31_2022-04-06/sys.tar.gz

sent 1,544 bytes received 85 bytes 3,258.00 bytes/sec
total size is 1,312 speedup is 0.81
(2)在执行服务端脚本
[root@backup ~]# sh /server/scripts/server_data.sh
[root@backup ~]# Error in certificate: Peer’s certificate issuer has been marked as not trusted by the.
7.添加定时任务
(1)客户端每分中执行
(2)服务端每两分钟执行
8.如何扩展一台节点:
远程拷贝172.16.1.31服务器的/server脚本到172.16.1.7服务器的根目录下
[root@web ~]# rsync -avz 172.16.1.31:/server /
The authenticity of host ‘172.16.1.31 (172.16.1.31)’ can’t be established.
ECDSA key fingerprint is SHA256:DGeuJ9SOABLouTzNwcAya8tjXEZiH5pvW2ldwyrHZ+w.
ECDSA key fingerprint is MD5:1f:dd:10:52:fe:80:d2:c8:9d:91:db:26:74:eb:0c:15.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘172.16.1.31’ (ECDSA) to the list of known hosts.
root@172.16.1.31’s password:
receiving incremental file list
server/
server/scripts/
server/scripts/client_push_server.sh

sent 55 bytes received 645 bytes 127.27 bytes/sec
total size is 652 speedup is 0.93
拷贝完检查172.16.1.31服务器上的脚本文件
[root@web ~]# ll /server/
total 0
drwxr-xr-x. 2 root root 35 Apr 2 10:33 scripts
[root@web ~]# ll /server/scripts/
total 4
-rw-r–r–. 1 root root 652 Apr 2 10:20 client_push_server.sh
编写定时任务
crontab -e ------------------编辑定时任务
crontab -l ------------------查看定时任务列表

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值