rsync使用详解

时间:2025-03-26 15:54:58

rsync服务

rsync备份的三种模式

  • 本地模式:类似使用cp命令, 在本地进行增量或者全量拷贝

  • 远程模式:将数据从一台linux服务器, 拷贝到另一台服务器

    将数据从另一台服务器拉取到另一台服务器

  • 进程模式:B/S架构 服务端到客户端

    将数据从服务端上传到客户端

    将数据从服务端下载到客户端

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 #让目标目录和源目录数据保持一致 --password-file=xxx #使用密码文件

  • 本地模式

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

    本地 rsync 参数选项 源文件/源目录 目标位置

使用cp命令复制 #:每次都是全量复制

  • - [root@backup ~]# cp /etc/passwd .  =====复制/etc/passwd
        [root@backup ~]# ll
        total 4
        -rw-r--r--. 1 root root 1022 Jun  7 14:08 passwd
        [root@backup ~]# cp /etc/passwd .
        cp: overwrite ‘./passwd’?   ============是否覆盖已复制的文件
    ​
    ​

使用rsync进行复制文件

  • #注意:rsync在操作目录的过程中,如果是目录后面加 / 则复制的是目录下的文件

    如果目录后面跟不加 / 则操作目录本身,其他命令后面加 / 与不加意义相同

  • ##删除原有passwd文件
    [root@backup ~]# rm passwd 
    rm: remove regular file ‘passwd’? y
    ​
    [root@backup ~]# rsync -avz /etc/passwd .
    sending incremental file list
    passwd
    ​
    sent 528 bytes  received 35 bytes  1,126.00 bytes/sec
    total size is 1,022  speedup is 1.82
    [root@backup ~]# ll
    total 4
    -rw-r--r--. 1 root root 1022 Jun  6 18:37 passwd
    ##rsync默认使用增量的方式拷贝  不会改变文件时间属性
    [root@backup ~]# rsync -avz /etc/passwd .
    sending incremental file list
    ​
    sent 45 bytes  received 12 bytes  114.00 bytes/sec
    total size is 1,022  speedup is 17.93
    [root@backup ~]# ll
    total 4
    -rw-r--r--. 1 root root 1022 Jun  6 18:37 passwd
    ​

远程访问模式

  • 语法结构 Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]

    下载 : rsync -avz 目标用户@ip:源文件/源目录 目标位置

    Push: rsync [OPTION...] SRC. .. [USER@]HOST:DEST

    上传: rsync -avz 文件/源目录 目标用户@主机名称:目标位置

    • pull :下载,更新

    • rsync :命令

    • USER :真实的系统用户, 管理员、普通用户

      远程拷贝数据时需要验证的用户

      如果为root用户 那拥有拷贝所有文件的权限

      如果为普通用户 只能拷贝普通用户有权限的文件

      默认以当前登录系统的用户为远程验证用户

    • @ :分隔符

    • HOST :ip地址、主机名称、域名(前提可以通信)

      主机名称表示自己的主机 是否ping的痛

      ping主机名称、域名是否可达

      /etc/hosts 文件作用:本地域名解析服务

    • SRC:目标主机的文件位置

    • DEST :下载到本地的那个目录

    ip 主机名
    目标主机 10.0.0.201 内网172.16.1.201 shuige
    本地主机 10.0.0.41 内网172.16.1.41 backup

    下载语法

    : rsync -avz 目标用户@ip:源文件/源目录 目标位置

在本地主机下载目标主机的 /etc/hosts 文件到当前目录

  • [root@backup ~]# rsync -avz   10.0.0.201:/etc/hosts .
      The authenticity of host '172.16.1.201 (172.16.1.201)' can't be established.
    ECDSA key fingerprint is SHA256:7jhjax7q1XPL+bcUmTB7DsHn5JhHNJjaPJwjr4b4e1c.
      ECDSA key fingerprint is MD5:7c:14:5f:d9:09:7f:3d:82:e3:b2:b9:a9:1b:cd:71:b5.
      Are you sure you want to continue connecting (yes/no)? yes  
      Warning: Permanently added '1.0.0.0.201' (ECDSA) to the list of known hosts.
      root@10.0.0.201's password:    ======输入10.0.0.201的密码
      receiving incremental file list
      hosts
      
      sent 43 bytes  received 156 bytes  30.62 bytes/sec
      total size is 175  speedup is 0.88
      [root@backup ~]# ll
      total 4
      -rw-r--r--. 1 root root 175 Jun  7 15:10 hosts
      

在10.0.0.201创建用户goudan

  • [root@backup ~]# useradd goudan
    [root@backup ~]# passwd goudan
    Changing password for user goudan.
    New password: 
    BAD PASSWORD: The password is a palindrome
    Retype new password: 
    passwd: all authentication tokens updated successfully.
    [root@backup ~]# id goudan
    uid=1000(goudan) gid=1000(goudan) groups=1000(goudan)
    ​

通过goudan用户下载10.0.0.201的/root/

  • [root@backup ~]# rsync -avz goudan@10.0.0.201:/root/
    goudan@10.0.0.201's password: 
    receiving incremental file list
    rsync: change_dir "/root" failed: Permission denied (13)==权限拒绝
    ​
    sent 8 bytes  received 70 bytes  31.20 bytes/sec
    total size is 0  speedup is 0.00
    rsync error: some files/attrs were not transferred (see previous errors) (code 23) at (1651) [Receiver=3.1.2]
    rsync: [Receiver] write error: Broken pipe (32)
    [root@backup ~]# ll
    total 4
    -rw-r--r--. 1 root root 175 Jun  7 15:10 hosts
    ​

goudan用户下载/etc/hosts文件

  • [root@backup ~]# rsync -avz goudan@10.0.0.201:/etc/hosts
    goudan@10.0.0.201's password: 
    receiving incremental file list
    -rw-r--r--            175 2022/06/07 15:10:55 hosts
    ​
    sent 20 bytes  received 44 bytes  42.67 bytes/sec
    total size is 175  speedup is 2.73
    [root@backup ~]# ll
    total 4
    -rw-r--r--. 1 root root 175 Jun  7 15:10 hosts
    ##本地域名解析
    [root@backup ~]# cat /etc/hosts
    127.0.0.1   localhost  localhost4 localhost4.localdomain4
    ::1         localhost  localhost6 localhost6.localdomain6
    10.0.0.201 shuige
    ​
    ​

    #注意:使用域名方式下载: 先配置hosts解析 然后使用shuige为主机进行远程下载

rsync拷贝目录

目标主机下载本地主机的/root/clock目录到当前

  • [root@shuige潘某 ~]#rsync -avz backup@10.0.0.41:/root/clock .
    The authenticity of host '10.0.0.41 (10.0.0.41)' can't be established.
    ECDSA key fingerprint is SHA256:IpVzZuk+kpeM7yJRFDo5bdw25/uaMNUJAEDhEQ5rr3Q.
    ECDSA key fingerprint is MD5:52:c1:0e:93:e2:d8:fb:c3:99:79:1e:fc:ca:79:cb:95.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added '10.0.0.41' (ECDSA) to the list of known hosts.
    root@10.0.0.41's password: 
    receiving incremental file list
    clock/
    clock/
    ​
    sent 47 bytes  received 123 bytes  113.33 bytes/sec
    total size is 0  speedup is 0.00
    ​

    #注意:后面加 / 拷贝的师目录下的文件、不包含目录

上传语法

: rsync -avz 文件/源目录 目标用户@主机名称:目标位置

#:将本地的数据上传到远程的服务器的某个·目录下

将10.0.0.41的·/etc/hosts文件上传到10.0.0.201的/opt目录下

  • ##上传hosts文件
    [root@backup ~]# rsync -avz /etc/hosts root@10.0.0.201:/opt
    root@10.0.0.201's password: 
    sending incremental file list
    hosts
    ​
    sent 157 bytes  received 35 bytes  128.00 bytes/sec
    total size is 176  speedup is 0.92
    ##切换用户查看
    [root@shuige潘某 ~]#ll /opt
    -rw-r--r--  1 root root 176 Jun  7 15:10 hosts
    
    

守护进程模式

  • 守护进程:服务时刻在后台运行称为守护进程

语法结构

Push: rsync [OPTION...] SRC... [USER@]HOST::DEST 推送: rsync -avz 本地文件 用户@主机::模块名称

Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

下载: rsync -avz 用户@主机:: 模块名称 下载到本地的目录

服务端10.0.0.41

第一个流程:下载安装服务 默认已经安装

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

第二个流程:配置服务

  • -##原本默认配置
    [root@backup ~]# cat /etc/ 
    ​
    # /etc/rsyncd: configuration file for rsync daemon mode
    ​
    # See  man page for more options.
    ​
    # configuration example:
    ​
    # uid = nobody
    ​
    # gid = nobody
    ​
    # use chroot = yes
    ​
    # max connections = 4
    ​
    # pid file = /var/run/
    ​
    # 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
  • 修改

  • uid = rsync                      # 运行进程的用户
    gid = rsync                      # 运行进程的用户组
    port = 873                       # 监听端口
    fake super = yes                 # 无需让rsync以root身份运行,允许接收文件的完整属性
    use chroot = no                  # 禁锢推送的数据至某个目录, 不允许跳出该目录
    max connections = 200            # 最大连接数
    timeout = 600                    # 超时时间
    ignore errors                    # 忽略错误信息
    read only = false                # 对备份数据可读写
    list = false                     # 不允许查看模块信息
    auth users = rsync_backup        # 定义虚拟用户,作为连接认证用户
    secrets file = /etc/ # 定义rsync服务用户连接认证密码文件路径
    ​
    [backup]                # 定义模块信息
    comment = commit        # 模块注释信息
    path = /backup          # 定义接收备份数据目录

第三个流程:根据配置文件创建必要的信息

  • 1、确认虚拟用户rsync是否存在 不存在则创建

    ##查看rsync虚拟用户是否存在
    [root@backup ~]# id rsync
    id: rsync: no such user
    ##创建rsync虚拟用户
    [root@backup ~]# useradd -s /sbin/nologin -M  rsync
    ##检查是否创建成功
    [root@backup ~]# id rsync
    uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)
    ​
  • 2、认证用户rsync_backup不需要创建

  • 3、密码文件 手动可以修改密码的名称和位置secrets file = /etc/

创建密码文件 #:密码文件的名称必须和配置文件中的名称相同

[root@backup ~]# vim /etc/
[root@backup ~]# cat /etc/ 
rsync_backup:123456
  • 4、给密码文件授权600权限

    [root@backup ~]# chmod 600 /etc/ 
    [root@backup ~]# ll /etc/ 
    -rw-------. 1 root root 20 Jun  7 16:22 /etc/
  • 5、创建backup目录 并修改属主、属组

    root@backup ~]# mkdir /backup
    [root@backup ~]# ll -d /backup/
    drwxr-xr-x. 2 root root 6 Jun  7 16:25 /backup/
    ##修改目录属主、属组
    [root@backup ~]# chown  /backup/
    [root@backup ~]# ll -d /backup/
    drwxr-xr-x. 2 rsync rsync 6 Jun  7 16:25 /backup/

第四个流程:启动服务

[root@backup ~]# systemctl start rsyncd
[root@backup ~]# systemctl enable rsyhncd
[root@backup ~]# netstat -tunlp
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      2601/rsync          
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1244/sshd           
tcp6       0      0 :::873                  :::*                    LISTEN  

第五个流程:测试服务

  • 客户端10.0.0.201

  • 上传/etc/hosts文件到服务端的backup模块------>/backup目录

    ##上传到backup模块
    [root@shuige潘某 ~]#rsync -avz /etc/hosts rsync_backup@172.16.1.41::backup
    Password: 
    sending incremental file list
    hosts
    ​
    sent 160 bytes  received 43 bytes  81.20 bytes/sec
    total size is 175  speedup is 0.86
    ##查看/backup
    [root@backup ~]# ll /backup
    total 4
    -rw-r--r--. 1 rsync rsync 175 Jun  7 15:10 hosts

上传到模块

在10.0.0.201上传以主机名和IP地址和时间命令的文件到backup模块

1、创建目录

[root@shuige潘某 ~]#mkdir `hostname`_`ifconfig|awk 'NR==2{print $2}'`_`date +%F-%H:%M`
[root@shuige潘某 ~]#ll
drwxr-xr-x  2 root   root     6 Jun  7 16:56 shuige_10.0.0.201_2022-06-07-16:56
​

2、使用rsync上传10.0.0.41的backup模块

##上传文件到10.0.0.41的backup模块
[root@shuige潘某 ~]#rsync -avz `hostname`_`ifconfig|awk 'NR==2{print $2}'`_`date +%F` rsync_backup@10.0.0.41::backup
Password: 
sending incremental file list
shuige_10.0.0.201_2022-06-07/
​
sent 86 bytes  received 28 bytes  25.33 bytes/sec
total size is 0  speedup is 0.00
##检查是否上传成功
[root@backup ~]# ll /backup/
total 4
-rw-r--r--. 1 rsync rsync 175 Jun  7 15:10 hosts
drwxr-xr-x. 2 rsync rsync   6 Jun  7 16:59 shuige_10.0.0.201_2022-06-07

拷贝10.0.0.201目录下所有内容不包含目录

##拷贝10.0.0.201/root/目录下所有
[root@shuige潘某 ~]#rsync -avz /root/ rsync_backup@10.0.0.41::backup
Password: 
sending incremental file list
./
.
.bash_history
.viminfo





.ssh/
.ssh/known_hosts
​
sent 5,525 bytes  received 206 bytes  1,637.43 bytes/sec
total size is 20,925  speedup is 3.65
##查看是否成功
[root@backup ~]# ll /backup/
total 24
-rw-r--r--. 1 rsync rsync  16 Jun  6 21:18 
-rw-r--r--. 1 rsync rsync   7 May 15 10:42 
-rw-r--r--. 1 rsync rsync 320 Jun  3 13:36 
-rw-r--r--. 1 rsync rsync  16 Jun  6 21:18 
-rw-r--r--. 1 rsync rsync 175 Jun  7 15:10 hosts
-rw-r--r--. 1 rsync rsync  16 Jun  6 21:18 
drwxr-xr-x. 2 rsync rsync   6 Jun  7 16:59 shuige_10.0.0.201_2022-06-07

模块上下载

语法结构

Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]

下载: rsync -avz 用户@主机:: 模块名称 下载到本地的目录

下载模块中的hosts文件到当前

[root@shuige潘某 ~]#rsync -avz rsync_backup@10.0.0.41::backup/
Password: 
@ERROR: auth failed on module backup
rsync error: error starting client-server protocol (code 5) at (1649) [Receiver=3.1.2]
[root@shuige潘某 ~]#rsync -avz rsync_backup@10.0.0.41::backup/
Password: 
receiving incremental file list
-rw-r--r--             16 2022/06/06 21:18:43 
​
sent 24 bytes  received 54 bytes  17.33 bytes/sec
total size is 16  speedup is 0.21
[root@shuige潘某 ~]#ll
-rw-r--r--. 2 root root  16 Jun  6 21:18 

下载目录

[root@shuige潘某 ~]#rsync -avz rsync_backup@10.0.0.41::backup/shuige_10.0.0.201_2022-06-07 .
Password: 
receiving incremental file list
shuige_10.0.0.201_2022-06-07/
​
sent 32 bytes  received 86 bytes  33.71 bytes/sec
total size is 0  speedup is 0.00
[root@shuige潘某 ~]#ll
total 20
-rw-r--r--. 2 root root  16 Jun  6 21:18 
-rw-r--r--. 1 root root   7 May 15 10:42 
-rw-r--r--  1 root root 320 Jun  3 13:36 
-rw-r--r--. 2 root root  16 Jun  6 21:18 
-rw-r--r--  1 root root  16 Jun  6 21:18 
drwxr-xr-x  2 root root   6 Jun  7 16:59 shuige_10.0.0.201_2022-06-07

#注意:只有模块 则默认模块下的所有目录和文件

##注意:

  • 远程访问模式,不区分客户端和服务端,在任意的服务器上都可以进行下载和上传数据

    • 在10.0.0.201执行

  • rsync -avz root@172.16.1.41:/etc/hosts . rsync -avz /etc/hosts root@172.16.1.41

    • 在10.0.0.41执行

  • rsync -avz /etc/hosts root@172.16.1.200:/root/ rsync -avz root@172.16.1.200:/etc/hosts .

  • 守护进程模式

    • 服务端 启动服务

    • 服务端有自己的验证用户 rsync_backup

    • 一模块的方式进行数据传输

    • 在客户端可以将数据上传到模块对应的目录

      • 在客户端将模块对应的目录下载到客户端

#:守护进程:服务端不能主动下载客户端的数据 (可以使用远程访问模式)

rsync其他参数案例

进程守护模式每次传输都输入密码。 解决方案

  • [root@shuige潘某 ~]#rsync -avz rsync_backup@10.0.0.41::backup/shuige_10.0.0.201_2022-06-07 . Password: -------默认手动交互模式

第一种办法:在客户端创建密码文件 密码内容123456

在执行命令时、指定密码文件 。不需要手动交互

[root@shuige潘某 ~]#echo 123456 >/etc/
[root@shuige潘某 ~]#cat /etc/ 
123456
##授权密码文件
[root@shuige潘某 ~]#chmod 600 /etc/ 
[root@shuige潘某 ~]#ll /etc/ 
-rw------- 1 root root 7 Jun  7 17:35 /etc/
##推送使用指定密码文件
[root@shuige潘某 ~]#rsync -avz /etc/passwd rsync_backup@10.0.0.41::backup --password-file=/etc/ 
sending incremental file list
passwd
​
sent 639 bytes  received 43 bytes  1,364.00 bytes/sec
total size is 1,325  speedup is 1.94

第二种方式:通过rsync的内置变量RSYNC_PASSWORD 给内置变量赋值

  • 在执行rsync密码验证过程中, rsync会先读取内置变量RSYNC_PASSWORD里面是否有密码,如果有则验证。如果没有则提示用户输入密码

#:如果指定了密码文件,则会读取密码文件中的密码

1、给RSYNC_PASSWORD内置变量赋值密码123456

[root@shuige潘某 ~]#export RSYNC_PASSWORD=123456

2、执行rsync命令过程中会验证变量复制

[root@shuige潘某 ~]#rsync -avz /etc/crontab rsync_backup@10.0.0.41::backup
sending incremental file list
crontab
​
sent 352 bytes  received 43 bytes  263.33 bytes/sec
total size is 451  speedup is 1.14

案例:打包/etc/hosts和/etc/passwd然后以时间命名 上传到10.0.0.41的backup模块

[root@shuige潘某 ~]#sh  
tar: Removing leading `/' from member names
sending incremental file list
2022-06-07
​
sent 861 bytes  received 43 bytes  1,808.00 bytes/sec
total size is 760  speedup is 0.84
[root@shuige潘某 ~]#cat  
tar zcf /root/`date +%F`  /etc/passwd  /etc/hosts
export RSYNC_PASSWORD=123456
rsync -avz /root/`date +%F`  rsync_backup@10.0.0.41::backup 
##或者
[root@shuige潘某 ~]#sh  
tar: Removing leading `/' from member names
[root@shuige潘某 ~]#cat  
tar zcf /root/`date +%F`  /etc/passwd  /etc/hosts
rsync -acz /root/`date +%F` rsync_backup@10.0.0.41::backup --password-file=/etc/

rsync参数及案例

rsync参数delete

  • delete :将客户端和服务端的数据变得完全相同

1、以本地的目录为准 本地有什么 远端服务器有什么

  • 查看backup目录下的文件

[root@backup ~]# ll /backup/
total 8
-rw-r--r-- 1 rsync rsync   0 Jun  7 11:39 
-rw-r--r-- 1 rsync rsync 742 Jun  7 12:12 
-rw-r--r-- 1 rsync rsync   0 Jun  8 09:20 
-rw-r--r-- 1 rsync rsync 158 Jun  7  2013 hosts
drwxr-xr-x 2 rsync rsync   6 Jun  7 11:34 oldboyedu_10.0.0.200
  • 查看客户端date目录下的文件

    [root@oldboyedu ~]# ll /data
    total 0
    -rw-r--r-- 1 root root 0 Jun  8 09:31 
    -rw-r--r-- 1 root root 0 Jun  8 09:31 
  • 使用 --delete 进行数据同步

    [root@oldboyedu ~]# rsync -avz --delete  /data/ rsync_backup@172.16.1.41::backup
    sending incremental file list
    deleting oldboyedu_10.0.0.200/
    deleting hosts
    deleting 
    deleting 
    deleting 
    ./
    
    

2、以服务端为准 服务器的/backup目录有什么 本地的/data就有什么

  • 查看服务器的backup目录

    [root@backup ~]# rm -f /backup/
    [root@backup ~]# touch /backup/oldboy{1..3}.log
    [root@backup ~]# ll /backup/
    total 0
    -rw-r--r-- 1 rsync rsync 0 Jun  8 09:31 
    -rw-r--r-- 1 root  root  0 Jun  8 09:43 
    -rw-r--r-- 1 root  root  0 Jun  8 09:43 
    -rw-r--r-- 1 root  root  0 Jun  8 09:43 
  • 查看客户端date目录下

    [root@oldboyedu ~]# ll /data
    total 0
    -rw-r--r-- 1 root root 0 Jun  8 09:31 
    -rw-r--r-- 1 root root 0 Jun  8 09:31 
  • 进行数据同步,以服务端为准

    [root@oldboyedu ~]# rsync -avz --delete rsync_backup@172.16.1.41::backup /data/
    receiving incremental file list
    deleting 
    ./
    
    
    
    ​
    sent 88 bytes  received 251 bytes  678.00 bytes/sec
    total size is 0  speedup is 0.00
    [root@oldboyedu ~]# ll /data
    total 0
    -rw-r--r-- 1 root root 0 Jun  8 09:31 
    -rw-r--r-- 1 root root 0 Jun  8  2022 
    -rw-r--r-- 1 root root 0 Jun  8  2022 
    -rw-r--r-- 1 root root 0 Jun  8  2022 

Rsync的Limit限速

#企业案例: 某DBA使用rsync拉取备份数据时,由于文件过大导致内部交换机带宽被沾满,导致用户的请求无法响应

[root@nfs01 ~]# export RSYNC_PASSWORD=zls
[root@nfs01 ~]# rsync -avz --bwlimit=1 rsync_backup@172.16.1.41::backup/ /data/

Rsync备份案例:

客户端: 10.0.0.200 客户端: 10.0.0.7 服务端: 10.0.0.41

1.客户端提前准备存放的备份的目录,目录规则下:/backup/oldboyedu_172.16.1.200_2018-08-08 当前时间

2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/oldboyedu_10.0.0.200_2022-06-08-11-41-13

3.客户端最后将备份的数据进行推送至备份服务器

4.客户端每天凌晨1点定时执行该脚

5.避免浪费磁盘空间 客户端查找7天前的文件然后删除

1.客户端提前准备存放的备份的目录,目录规则如下:/backup/oldboyedu_172.16.1.200_2018-08-08 当前时间 1)创建/backup目录

 [root@oldboyedu ~]# mkdir /backup

2)在/backup目录下创建以主机名称+ip地址+时间命名目录

[root@oldboyedu ~]# hostname
oldboyedu
[root@oldboyedu ~]# ifconfig eth0|awk 'NR==2{print $2}'
10.0.0.200
[root@oldboyedu ~]# echo `date +%F`
2022-06-08
[root@oldboyedu ~]# echo `date +%F-%H-%M-%S`
2022-06-08-11-33-07
[root@oldboyedu ~]# echo /backup/`hostname`
/backup/oldboyedu
[root@oldboyedu ~]# echo /backup/`hostname`_`ifconfig eth0|awk 'NR==2{print $2}'`
/backup/oldboyedu_10.0.0.200
[root@oldboyedu ~]# echo /backup/`hostname`_`ifconfig eth0|awk 'NR==2{print $2}'`_`date +%F-%H-%M-%S`
/backup/oldboyedu_10.0.0.200_2022-06-08-11-35-09

通过变量赋值的方式进行拼接为目录名称

[root@oldboyedu ~]# HOST=`hostname`
[root@oldboyedu ~]# ifconfig eth0|awk 'NR==2{print $2}'
10.0.0.200
[root@oldboyedu ~]# IP=`ifconfig eth0|awk 'NR==2{print $2}'`
[root@oldboyedu ~]# echo $IP
10.0.0.200
[root@oldboyedu ~]# echo `date +%F-%H-%M-%S`
2022-06-08-11-37-00
[root@oldboyedu ~]# TIME=`date +%F-%H-%M-%S`        # 赋值一次 后面调用多次TIME值 不会发生变化
[root@oldboyedu ~]# echo $TIME
2022-06-08-11-37-08
[root@oldboyedu ~]# echo $HOST
oldboyedu
[root@oldboyedu ~]# echo $HOST_$IP_$TIME
2022-06-08-11-37-08
[root@oldboyedu ~]# echo ${HOST}_${IP}_$TIME
oldboyedu_10.0.0.200_2022-06-08-11-37-08

在当前创建

[root@oldboyedu ~]# mkdir ${HOST}_${IP}_$TIME
[root@oldboyedu ~]# ll
total 0
drwxr-xr-x 2 root root 6 Jun  8 11:43 oldboyedu_10.0.0.200_2022-06-08-11-41-13
/backup目录下创建
[root@oldboyedu ~]# mkdir /backup/${HOST}_${IP}_$TIME
[root@oldboyedu ~]# ll /backup/
total 0
drwxr-xr-x 2 root root 6 Jun  8 11:43 oldboyedu_10.0.0.200_2022-06-08-11-41-13

2.客户端在本地打包备份(系统配置文件、应用配置等)拷贝至/backup/oldboyedu_10.0.0.200_2022-06-08-11-41-13

[root@oldboyedu ~]# tar zcvf /backup/${HOST}_${IP}_$TIME/  /etc/hosts /etc/passwd 
​	tar: Removing leading `/' from member names
​	/etc/hosts
​	/etc/passwd
​	[root@oldboyedu ~]# ll /backup/oldboyedu_10.0.0.200_2022-06-08-11-41-13/
​	total 4
​	-rw-r--r-- 1 root root 747 Jun  8 11:47 

[root@oldboyedu ~]# tar zcvf /backup/${HOST}_${IP}_$TIME/  /etc/hosts /etc/passwd 
tar: Removing leading `/' from member names
/etc/hosts
/etc/passwd
[root@oldboyedu ~]# ll /backup/oldboyedu_10.0.0.200_2022-06-08-11-41-13/
total 4
-rw-r--r-- 1 root root 747 Jun  8 11:47 
[root@oldboyedu ~]# ll /backup/${HOST}_${IP}_$TIME/
total 4
-rw-r--r-- 1 root root 747 Jun  8 11:47 

3.客户端最后将备份的数据进行推送至备份服务器

[root@oldboyedu ~]# rsync -avz /backup/${HOST}_${IP}_$TIME rsync_backup@172.16.1.41::backup
sending incremental file list
oldboyedu_10.0.0.200_2022-06-08-11-41-13/
oldboyedu_10.0.0.200_2022-06-08-11-41-13/

4.客户端每天凌晨1点定时执行该脚本 将命令写入到文本中 以正常命令执行:

[root@oldboyedu ~]# mkdir /server/scripts -p
[root@oldboyedu ~]# cd /server/scripts/
[root@oldboyedu scripts]# cat 
​
•   #客户端提前准备存放的备份的目录,目录规则如下:/backup/oldboyedu_172.16.1.200_2018-08-08 当前时间
•   mkdir -p /backup/oldboyedu_172.16.1.200_2022-06-08
​
•   #客户端提前准备存放的备份的目录,目录规则如下:/backup/oldboyedu_172.16.1.200_2018-08-08 当前时间
•   mkdir -p /backup/oldboyedu_172.16.1.200_2022-06-08
​
#2.客户端在本地打包备份放入上面创建好的目录下
cd /etc/
tar zcf /backup/oldboyedu_172.16.1.200_2022-06-08/ hosts passwd
​
#3.客户端最后将备份的数据进行推送至备份服务器
export RSYNC_PASSWORD=123456
rsync -avz /backup/oldboyedu_172.16.1.200_2022-06-08 rsync_backup@172.16.1.41::backup

-------------------基于上面的命令进行脚本优化---------------------------------------------- [root@oldboyedu scripts]# cat

    #定义变量
•   HOST=`hostname`
•   IP=`ifconfig eth0|awk 'NR==2{print $2}'`
•   TIME=`date +%F-%H-%M-%S`
•   DIR=/backup/${HOST}_${IP}_$TIME
​
#客户端创建以主机名称+IP地址+时间命名
mkdir -p  $DIR
​
#2.客户端在本地打包备份放入上面创建好的目录下
cd /etc/
tar zcf $DIR/ hosts passwd
​
#3.客户端最后将备份的数据进行推送至备份服务器
export RSYNC_PASSWORD=123456
rsync -avz $DIR  rsync_backup@172.16.1.41::backup

写入定时任务每天凌晨1点执行备份任务(写前先按照每分钟进行测试 没问题后修改成正常的时间)

[root@oldboyedu ~]# crontab -l
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
00 01 * * * sh /server/scripts/ &>/dev/null

5.避免浪费磁盘空间 客户端查找7天前的文件然后删除

    [root@oldboyedu scripts]# cat  
•   #定义变量
•   HOST=`hostname`
•   IP=`ifconfig eth0|awk 'NR==2{print $2}'`
•   TIME=`date +%F-%H-%M-%S`
•   DIR=/backup/${HOST}_${IP}_$TIME
​
#客户端创建以主机名称+IP地址+时间命名
mkdir -p  $DIR
​
#2.客户端在本地打包备份放入上面创建好的目录下
cd /etc/
tar zcf $DIR/ hosts passwd
​
#3.客户端最后将备份的数据进行推送至备份服务器
export RSYNC_PASSWORD=123456
rsync -avz $DIR  rsync_backup@172.16.1.41::backup
​
#4.查找7天前的文件并且删除
find /backup/ -mtime +7|xargs rm &>/dev/null

服务端

1、服务端需要每天校验客户端推送过来的数据是否完整 通过MD5校验的方式 检测在传输过程中没有被人篡改!

1)推送前对当前的文件提前做指纹

 [root@oldboyedu ~]# cat 
 1111

2)将指纹写入到一个文件中 [root@oldboyedu ~]# md5sum > [root@oldboyedu ~]# cat 1f18348f32c9a4694f16426798937ae2 [root@oldboyedu ~]# ll total 8 -rw-r--r-- 1 root root 43 Jun 8 15:29 -rw-r--r-- 1 root root 5 Jun 8 15:28 3)将文件和校验文件 一起发给服务端 [root@oldboyedu ~]# rsync -avz rsync_backup@172.16.1.41::backup sending incremental file list

4)在服务端进行校验数据的完整性

md5sum -c

---------------------------------------修改客户端脚本增加MD5校验结果 [root@oldboyedu scripts]# cat

#定义变量
HOST=`hostname`
IP=`ifconfig eth0|awk 'NR==2{print $2}'`
TIME=`date +%F-%H-%M-%S
​
•   DIR=/backup/${HOST}_${IP}_$TIME
​
•   DIR=/backup/${HOST}_${IP}_$TIME
​
md5sum -c 
​
---------------------------------------修改客户端脚本增加MD5校验结果
[root@oldboyedu scripts]# cat 
#定义变量
HOST=`hostname`
IP=`ifconfig eth0|awk 'NR==2{print $2}'`
TIME=`date +%F-%H-%M-%S
​
•   DIR=/backup/${HOST}_${IP}_$TIME
​
#客户端创建以主机名称+IP地址+时间命名
mkdir -p  $DIR
​
#2.客户端在本地打包备份放入上面创建好的目录下
cd /etc/
tar zcf $DIR/ hosts passwd
​
#3.对打包后的文件做MD5校验值 存入文件中
md5sum $DIR/ > $DIR/
​
#3.客户端最后将备份的数据进行推送至备份服务器
export RSYNC_PASSWORD=123456
rsync -avz $DIR  rsync_backup@172.16.1.41::backup
​
#4.查找7天前的文件并且删除
find /backup/ -mtime +7|xargs rm &>/dev/null
​
​


服务端检测校验结果:

[root@backup ~]# md5sum -c /backup/oldboyedu_10.0.0.200_2022-06-08-16-25-09/ 
/backup/oldboyedu_10.0.0.200_2022-06-08-16-25-09/: OK

3.服务端需要每天校验的结果通知给管理员 需要用到邮件配置

/etc/ 文件下方直接输入

/etc/ 邮件服务

[root@backup /]# vim /etc/
set from=3549723899@
set smtp=smtps://:465
set smtp-auth-user=3549723899@
set smtp-auth-password=ktmtahzknkuxdadg#客户端授权码
set smtp-auth=login
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/

命令行测试邮件是否可以正常发送: [root@backup ~]# mail -s '测试结果' 3549723899@,1660084401@ < /backup/

4.服务端仅保留6个月的备份数据,其余的全部删除180天前的删除

 find /backup/ -mtime +180|xargs rm &>/dev/null

最终客户端脚本:

[root@oldboyedu scripts]# cat  
#定义变量
HOST=`hostname`
IP=`ifconfig eth0|awk 'NR==2{print $2}'`
TIME=`date +%F`
DIR=/backup/${HOST}_${IP}_$TIME
​
#客户端创建以主机名称+IP地址+时间命名
mkdir -p  $DIR
​
#2.客户端在本地打包备份放入上面创建好的目录下
cd /etc/
tar zcf $DIR/ hosts passwd
​
#3.对打包后的文件做MD5校验值 存入文件中
md5sum $DIR/ > $DIR/
​
#3.客户端最后将备份的数据进行推送至备份服务器
export RSYNC_PASSWORD=123456
rsync -avz $DIR  rsync_backup@172.16.1.41::backup
​
#4.查找7天前的文件并且删除
find /backup/ -mtime +7|xargs rm &>/dev/null

最终的服务端脚本:

[root@backup scripts]# cat  
​
# 校验数据的完整性
​
TIME=`date +%F`
md5sum -c /backup/*$TIME/ > /backup/${TIME}.log
​
#2.将校验结果发送给管理员
mail -s "数据备份通知" 593528156@ < /backup/${TIME}.log &>/dev/null
​
#3.服务器数据保留180天
find /backup/ -mtime +180|xargs rm &>/dev/null
​
服务端写入定时任务:
crontab -e
00 07 * * * sh /server/scripts/

快速部署扩展一台客户端: 1.创建目录 /server/scripts mkdir -p /server/scripts 2.将脚本拷贝到目录下 从7上执行下载 rsync -avz root@172.16.1.200:/server/scripts/ /server/scripts/ 3.写入定时任务 crontable -e 00 01 * * * sh /server/scripts/