rsync备份

修改网卡

  1. 1586825271551

    rsync备份工具

  1. 什么是备份?

    • 备份就是把文件复制一份存起来.

  2. 为什么要做备份?

    • 快速恢复,保证数据不丢失,减少损失

  3. 能不能不做备份?

    • 不重要的数据可以不备份

  4. 备份应该怎么做?

    • 完全备份

    • 增量备份

  5. 备份使用什么工具?

    • 本地备份:cp

    • 远程备份:scp或者rsync

  6. 备份工具rsync基本介绍

    rsync remote rsync 远程同步,可以实现 “不同主机” 之间的同步,同时支持增量和全量的同步方式`

  7. rsync备份工具应用场景?

    推Push(上传):将本地的数据推送到 备份服务器。 拉Pull(下载):将备份服务器的数据拉取到本地。

  8. rsync三大传输模式?

    • 本地传输

    • 远程传输

    • 守护进程

    1)本地传输方式:单个主机之间的数据拷贝,类似于cp,不过是增量备份。

    • rsync:命令 -avz:选项 /root/file.txt:备份文件 /tmp:备份位置。

    [root@web ~]# rsync -avz /root/file.txt /tmp/
    sending incremental file list
    file.txt

    sent 87 bytes received 35 bytes 244.00 bytes/sec
    total size is 0 speedup is 0.00

    2)远程传输方式:借助ssh协议进行传输,要知道对方的用户名和密码

    1. 将本地/root/file.txt文件推送到172.161.41服务器上的/opt目录,使用的是41服务器的root用户

      1586832000374

    2. 拉取服务器上面推送到41服务器/otp目录下的file.txt文件到本地的/mnt目录

      [root@web ~]# rsync -avz root@172.16.1.41:/opt/file.txt /mnt/
      root@172.16.1.41's password:
      receiving incremental file list
      file.txt

      sent 43 bytes received 87 bytes 8.97 bytes/sec
      total size is 0 speedup is 0.00
      [root@web ~]# ls /mnt/
      file.txt
    3. 推送目录

      • [root@backup ~]# rsync -avz /etc/ root@172.16.1.31:/opt/ [root@backup ~]# rsync -avz /etc root@172.16.1.31:/opt/

        注意: 1.添加 / 表示推送目录下的所有内容 2.不添加 / 表示推送该目录本身,以及目录下的内

      • 依托对端主机的用户身份权限

        1. 使用系统root用户(不安全)

        2. 使用系统的普通用户(会出现权限不够的情况)

    3)守护进程模式:不使用系统用户,使用一个虚拟的用户(不存在的用户)来实现推送。

    准备:172.16.1.41作为rsync服务端

    172.16.1.31作为rsync客户端

    1. 安装

      yum install rsync -y

    2. 配置vrsync文件

      [root@web2 ~]# vim /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 = false
      auth users = rsync_backup
      secrets file = /etc/rsync.passwd
      log file = /var/log/rsyncd.log
      #####################################
      [backup]
      comment = welcome to oldboyedu backup!
      path = /backup

      --- 配置详解 [root@backup ~]# vim /etc/rsyncd.conf 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.passwd # 定义rsync服务用户连接认证密码文件路径

      [backup] # 定义模块信息 comment = commit # 模块注释信息 path = /backup # 定义接收备份数据目录

    3. 根据配置文件解读,我们发现还需要做一些初始化的操作:

      1. 创建Rsync进程运行的用户 ( rsync )

        [root@web2 ~]# useradd -M -s /sbin/nologin rsync
        [root@web2 ~]# id rsync
        uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)
      2. 定义虚拟用户名rsync_bakcup的密码.

        [root@web2 ~]# echo "rsync_backup:123456" > /etc/rsync.passwd
        [root@web2 ~]# chmod 600 /etc/rsync.passwd
      3. 创建存储的数据的目录 /backup

        [root@web2 ~]# mkdir /backup
        [root@web2 ~]# chown -R rsync.rsync /backup/
    4. 启动

      [root@web2 ~]# systemctl start rsyncd [root@web2 ~]# systemctl enable rsyncd

    5. 测试(推送、拉取)

      1.使用172.16.1.31 推送 client.txt 文件至 备份服务器 41 的 backup模块

      [root@web ~]# touch client.txt
      [root@web ~]# rsync -avz client.txt rsync_backup@172.16.1.41::backup
      Password:
      sending incremental file list
      client.txt

      sent 93 bytes received 43 bytes 2.05 bytes/sec
      total size is 0 speedup is 0.00

      2.拉取 172.16.1.41 备份服务器 backup模块下的数据 到/opt

      [root@web ~]# rsync -avz rsync_backup@172.16.1.41::backup /opt/
      Password:
      receiving incremental file list
      ./
      123.txt
      client.txt

      sent 69 bytes received 198 bytes 6.94 bytes/sec
      total size is 0 speedup is 0.00
      [root@web ~]# ls /opt/
      123.txt client.txt
    6. 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     #让目标目录和源目录数据保持一致

      实战1:rsync的limit限速(1=1mb)

      [root@web ~]# rsync -avz --bwlimit=1 -P /etc rsync_backup@172.16.1.41::backup

      实战2: Rsync实现数据无差异同步 --delete

      1. 推: 客户端 推送 到 服务端,服务端必须与客户端保持一致。

        • 客户端操作

          1. 查看opt目录然后对目录进行推送到服务端使其服务端和客户端数据一致

        [root@web ~]# ls /opt/
        111.txt 123.txt 222.txt client.txt
        [root@web ~]# rsync -avz --delete /opt/ rsync_backup@172.16.1.41::backup
        Password:
        sending incremental file list
        deleting 555.txt
        deleting 444.txt
        deleting 333.txt
        ./
        111.txt
        123.txt
        222.txt
        client.txt

        sent 304 bytes received 136 bytes 18.72 bytes/sec
        total size is 0 speedup is 0.00
        • 服务端查看(操作前和操作后)

          [root@web2 ~]# ls /backup/
          333.txt 444.txt 555.txt
          [root@web2 ~]# ls /backup/
          111.txt 123.txt 222.txt client.txt
      2. 拉: 客户端 拉取 服务端数据,服务端有什么,客户端就有什么。 客户端必须与服务端完全一致。

        [root@web ~]# ls /opt/
        1..3txt 1txt 2txt 3txt
        [root@web ~]# rsync -avz --delete rsync_backup@172.16.1.41::backup /opt
        Password:
        receiving incremental file list
        deleting 3txt
        deleting 2txt
        deleting 1txt
        deleting 1..3txt
        ./
        111.txt
        123.txt
        222.txt
        client.txt

        sent 107 bytes received 312 bytes 17.10 bytes/sec
        total size is 0 speedup is 0.00
        [root@web ~]# ls /opt/
        111.txt 123.txt 222.txt client.txt

         

      实战3: 无需输入密码实现rsync推送和拉取

       

      方式一: --password-file
      [root@nfs opt]# echo "123456" > /etc/rsync.pass
      [root@nfs opt]# chmod 600 /etc/rsync.pass
      [root@nfs opt]# rsync -avz --delete rsync_backup@172.16.1.41::backup /opt/ --password-file=/etc/rsync.pass

       

        方式二: 设定环境变量方式
      [root@nfs opt]# export RSYNC_PASSWORD=123456 #只当前窗口有用,换一个窗口就没用了(放到脚本中使用,非常的方便)
      [root@nfs opt]# rsync -avz --delete rsync_backup@172.16.1.41::backup /opt/
    7.  

 

 

 

posted @ 2020-04-14 15:56  小迷茫c  阅读(123)  评论(0编辑  收藏  举报