Backup and recovery (d)

Backup and recovery (d)

8. Backup and Recovery

8.1 Backup and Recovery Overview

image.png-338.5kB

8.2 by virt-manager to manage snapshots

  • Create a snapshot
  • Snapshot recovery
  • Deleting a snapshot

8.2.1 Creating a snapshot

image.png-22.8kB

image.png-29.3kB

image.png-17.5kB

image.png-38kB

#查看运行的虚拟机列表
[root@KVM ~]# virsh list
 Id    名称                         状态
----------------------------------------------------
 2     centos6.5                      running
 4     centos6.5-2                    running

#查看指定名称的虚拟机的磁盘使用情况
[root@KVM ~]# virsh domblklist centos6.5
目标     源
------------------------------------------------
vda        /var/lib/libvirt/images/centos6.5.qcow2
hda        /ios/CentOS-6.5-x86_64-bin-DVD1.iso

#查看指定磁盘的快照情况
[root@KVM ~]# qemu-img snapshot -l /var/lib/libvirt/images/centos6.5.qcow2
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         snapshot1              216M 2019-01-16 13:39:57   02:04:44.485

8.2.2 snapshot recovery

image.png-25.1kB

image.png-42.7kB

image.png-27.7kB

8.2.3 To delete a snapshot

image.png-50.6kB

[root@KVM ~]# qemu-img snapshot -l /var/lib/libvirt/images/centos6.5.qcow2 
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         snapshot1              216M 2019-01-16 13:39:57   02:04:44.485
2         snapshot2              216M 2019-01-16 14:03:54   02:10:17.600
3         snapshot3              216M 2019-01-16 14:04:26   02:10:24.168
4         snapshot4              216M 2019-01-16 14:04:50   02:10:36.746

The KVM virt-manager to do is actually a snapshot checkpoint snapshot. While we all look the whole amount of backup, but not really, between these snapshots after a previous incremental backup of only a snapshot. Therefore, there is a relationship between them. I can see a tree relationship between snapshots by the following method

[root@KVM ~]# virsh snapshot-list centos6.5 --tree
snapshot1
  |
  +- snapshot2
      |
      +- snapshot3
          |
          +- snapshot4

image.png-58.9kB

8.3 virsh management through snapshots

#查看创建快照帮助
[root@KVM ~]# virsh help snapshot-create-as
  NAME
    snapshot-create-as - 使用一组参数生成快照

  SYNOPSIS
    snapshot-create-as <domain> [--name <string>] [--description <string>] [--print-xml] [--no-metadata] [--halt] [--disk-only] [--reuse-external] [--quiesce] [--atomic] [--live] [--memspec <string>] [[--diskspec] <string>]...

  DESCRIPTION
    使用一组参数生成快照(磁盘和 RAM)

  OPTIONS
    [--domain] <string>  domain name, id or uuid
    --name <string>  快照名称
    --description <string>  快照描述
    --print-xml      输出 XML 文档而不是生成 XML
    --no-metadata    提取快照但不生成元数据
    --halt           生成快照后停止域
    --disk-only      捕获磁盘状态而不是 vm 状态
    --reuse-external  重新使用任意现有外部文件
    --quiesce        静默虚拟机的文件系统
    --atomic         需要自动操作
    --live           提取实时快照
    --memspec <string>  内存属性:[file=]name[,snapshot=type]
    [--diskspec] <string>  磁盘属性: disk[,snapshot=type][,driver=type][,file=name]
#创建一个快照
[root@KVM ~]# virsh snapshot-create-as centos6.5 shot1
已生成域快照 shot1

[root@KVM ~]# virsh snapshot-list centos6.5
 名称               生成时间              状态
------------------------------------------------------------
 shot1                2019-01-16 14:10:57 +0800 running   #怎么到上边了
 snapshot1            2019-01-16 13:39:57 +0800 running
 snapshot2            2019-01-16 14:03:54 +0800 running
 snapshot3            2019-01-16 14:04:26 +0800 running

[root@KVM ~]# virsh snapshot-list --tree centos6.5    #以树格式显示快照
snapshot1
  |
  +- snapshot2
      |
      +- snapshot3
          |
          +- shot1          #得如此来查看快照上下级关系
#查看某个快照的详细信息
[root@KVM ~]# virsh snapshot-info centos6.5 --current
名称:       shot1
域:          centos6.5
当前:       是
状态:       running
位置:       内部
上级:       snapshot3
下级:       0
降序:       0
元数据:    是
#删除一个快照
[root@KVM ~]# virsh snapshot-list centos6.5
 名称               生成时间              状态
------------------------------------------------------------
 shot1                2019-01-16 14:10:57 +0800 running
 snapshot1            2019-01-16 13:39:57 +0800 running
 snapshot2            2019-01-16 14:03:54 +0800 running
 snapshot3            2019-01-16 14:04:26 +0800 running

[root@KVM ~]# virsh snapshot-delete centos6.5 snapshot2
已删除域快照 snapshot2

[root@KVM ~]# virsh snapshot-list centos6.5
 名称               生成时间              状态
------------------------------------------------------------
 shot1                2019-01-16 14:10:57 +0800 running
 snapshot1            2019-01-16 13:39:57 +0800 running
 snapshot3            2019-01-16 14:04:26 +0800 running
 
[root@KVM ~]# virsh snapshot-list --tree centos6.5
snapshot1
  |
  +- snapshot3
      |
      +- shot1
#恢复一个快照
#我们执行先删除虚拟机里的部分文件在测试恢复
[root@KVM ~]# virsh snapshot-revert centos6.5 shot1  #恢复快照shot1

8.4 KVM backup and recovery

image.png-257.1kB

KVM-based virtual disk file backup

Create a temporary external disk snapshot
virsh snapshot-create-as --domain crm guest-statel --diskspec vda,file=/

8.4.1 upgrade by cloud yum repository qemu-kvm-ev

Because the system comes with qemu-kvm is too old 1.5.3 version does not support a lot, so we need to upgrade your qemu-kvm Enterprise Edition

[root@KVM ~]# mkdir -p /export/images/   #创建存放外部磁盘快照目录

[root@KVM ~]# virsh snapshot-create-as --domain centos6.5 guest-statel --diskspec vda,file=/export/images/centos6.5-overlay1.qcow2 --disk-only --atomic
错误:不支持的操作:这个 QEMU 二进制文件不支持即时磁盘快照

[root@KVM ~]# rpm -qa | grep libvirt
libvirt-python-4.5.0-1.el7.x86_64
libvirt-daemon-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-lxc-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-iscsi-4.5.0-10.el7_6.3.x86_64
libvirt-gobject-1.0.0-1.el7.x86_64
libvirt-daemon-driver-interface-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-qemu-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-4.5.0-10.el7_6.3.x86_64
libvirt-client-4.5.0-10.el7_6.3.x86_64
libvirt-glib-1.0.0-1.el7.x86_64
libvirt-daemon-driver-nwfilter-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-config-nwfilter-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-disk-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-scsi-4.5.0-10.el7_6.3.x86_64
libvirt-libs-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-secret-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-config-network-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-rbd-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-mpath-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-kvm-4.5.0-10.el7_6.3.x86_64
libvirt-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-nodedev-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-core-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-gluster-4.5.0-10.el7_6.3.x86_64
libvirt-bash-completion-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-network-4.5.0-10.el7_6.3.x86_64
libvirt-daemon-driver-storage-logical-4.5.0-10.el7_6.3.x86_64
libvirt-gconfig-1.0.0-1.el7.x86_64

[root@KVM ~]# rpm -qa | grep kvm
qemu-kvm-1.5.3-160.el7.x86_64                   #版本太低
libvirt-daemon-kvm-4.5.0-10.el7_6.3.x86_64
qemu-kvm-common-1.5.3-160.el7.x86_64            #版本太低
#下载阿里云源
[root@KVM ~]# which wget
/usr/bin/wget
[root@KVM ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#以下省略若干。。。

#将本地yum源扔进备份目录
[root@KVM ~]# cd /etc/yum.repos.d/
[root@KVM yum.repos.d]# ls
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@KVM yum.repos.d]# mkdir bak
[root@KVM yum.repos.d]# mv C* bak/
[root@KVM yum.repos.d]# mv bak/*Base* .
[root@KVM yum.repos.d]# ls
bak  CentOS-Base.repo
#生成centos-release-qemu-ev企业版kvm的yum源文件
[root@KVM yum.repos.d]# yum -y install centos-release-qemu-ev
[root@KVM yum.repos.d]# rpm -qa centos-release-qemu-ev
centos-release-qemu-ev-1.0-3.el7.centos.noarch

[root@KVM yum.repos.d]# ls
bak  CentOS-Base.repo  CentOS-QEMU-EV.repo
[root@KVM yum.repos.d]# cat CentOS-QEMU-EV.repo
# CentOS-QEMU-EV.repo
#
# Please see http://wiki.centos.org/SpecialInterestGroup/Virtualization for more
# information

[centos-qemu-ev]
name=CentOS-$releasever - QEMU EV
baseurl=http://mirror.centos.org/centos/$releasever/virt/$basearch/kvm-common/    #源配置文件本行需要修改
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-Virtualization

[centos-qemu-ev-test]
name=CentOS-$releasever - QEMU EV Testing
baseurl=http://buildlogs.centos.org/centos/$releasever/virt/$basearch/kvm-common/
gpgcheck=0
enabled=0
#安装和升级企业版qemu-kvm-ev
[root@KVM yum.repos.d]# yum -y install qemu-kvm-ev
[root@KVM yum.repos.d]# rpm -qa qemu-kvm-ev
qemu-kvm-ev-2.12.0-18.el7_6.1.1.x86_64

#重启KVM宿主机
[root@KVM yum.repos.d]# reboot

8.4.2 by local custom yum source, upgrade qemu-kvm-ev

#查看本地定制化rpm包,通过createrepo命令设定
[root@KVM ~]# ls /root/rpm/
ipxe-roms-qemu-20170123-1.git4e85b27.el7_4.1.noarch.rpm  repodata
lz4-1.7.5-2.el7.x86_64.rpm                               seabios-bin-1.11.0-2.el7.noarch.rpm
qemu-img-ev-2.10.0-21.el7_5.3.1.x86_64.rpm               seavgabios-bin-1.11.0-2.el7.noarch.rpm
qemu-kvm-common-ev-2.10.0-21.el7_5.3.1.x86_64.rpm        spice-server-0.14.0-2.el7_5.3.x86_64.rpm
qemu-kvm-ev-2.10.0-21.el7_5.3.1.x86_64.rpm

#定制好以后,直接进行yum安装即可
[root@KVM ~]# yum -y install qemu-kvm-ev

#重启KVM宿主机
[root@KVM ~]# reboot

qemu-kvm-ev upgrade is complete, we continue to do external disk snapshot

[root@KVM ~]# virsh snapshot-create-as --domain centos6.5 guest-statel --diskspec vda,file=/export/images/centos6.5-overlay1.qcow2 --disk-only --atomic
已生成域快照 guest-statel

[root@KVM ~]# ls /export/images/
centos6.5-overlay1.qcow2

8.4.3 External Disk Snapshot View

[root@KVM ~]# virsh snapshot-list centos6.5
 名称               生成时间              状态
------------------------------------------------------------
 guest-statel         2019-01-16 23:05:39 +0800 shutoff
 shot1                2019-01-16 14:10:57 +0800 running
 snapshot1            2019-01-16 13:39:57 +0800 running
 snapshot3            2019-01-16 14:04:26 +0800 running

[root@KVM ~]# virsh snapshot-list --tree centos6.5
snapshot1                   #外部磁盘快照
  |
  +- snapshot3              #虚拟机快照
      |
      +- guest-statel       #虚拟机快照
      +- shot1              #虚拟机快照
#查看虚拟机磁盘信息
[root@KVM ~]# virsh domblklist centos6.5
目标     源
------------------------------------------------
vda        /export/images/centos6.5-overlay1.qcow2     #磁盘的指向变成了我们创建的新的磁盘文件
hda        /ios/CentOS-6.5-x86_64-bin-DVD1.iso

#查看新磁盘文件的xml配置信息
[root@KVM ~]# qemu-img info /export/images/centos6.5-overlay1.qcow2
image: /export/images/centos6.5-overlay1.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 196K
cluster_size: 65536
backing file: /var/lib/libvirt/images/centos6.5.qcow2    #原始磁盘文件成了备份文件
backing file format: qcow2
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

When we use external disk snapshot disk hot backup of the virtual machine is actually generate a link to the original disk disk difference (incremental) snapshot file soft connection. The virtual machine disk to point to a soft link disk file newly generated. This way, then, the original file on the disk is no longer in the occupied state. We can be a variety of cold backup.

8.4.4 of the original virtual disk files cold backup
#查看原始磁盘文件
[root@KVM ~]# qemu-img info /var/lib/libvirt/images/centos6.5.qcow2
image: /var/lib/libvirt/images/centos6.5.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 8.6G
cluster_size: 65536
Snapshot list:
ID        TAG                 VM SIZE                DATE       VM CLOCK
1         snapshot1              216M 2019-01-16 13:39:57   02:04:44.485
3         snapshot3              216M 2019-01-16 14:04:26   02:10:24.168
4         shot1                  216M 2019-01-16 14:10:57   02:16:31.850
Format specific information:
    compat: 1.1
    lazy refcounts: true
    refcount bits: 16
    corrupt: false

We did not find any point at which the information and documents related to the new disk. So the original disk file has not been occupied. We can cold backup

#进行虚拟磁盘文件的冷备份
[root@KVM ~]# cd /tmp/

[root@KVM tmp]# tar -zcvf 2019-01-16-centos6.5.tar.gz /var/lib/libvirt/images/centos6.5.qcow2 

[root@KVM tmp]# du -sh 2019-01-16-centos6.5.tar.gz
1.0G    2019-01-16-centos6.5.tar.gz

8.4.5 External disk data snapshot file write back

Whether to create a virtual machine in centos6.5 several files, and then view after switching virtual disk files, create files disappear

#查看虚拟机的虚拟磁盘信息
[root@KVM tmp]# virsh domblklist centos6.5
目标     源
------------------------------------------------
vda        /export/images/centos6.5-overlay1.qcow2
hda        /ios/CentOS-6.5-x86_64-bin-DVD1.iso

#外部磁盘快照的回写
[root@KVM tmp]# virsh blockcommit centos6.5 vda --active --verbose --pivot
Block commit: [100 %]
成功进行数据挖掘

[root@KVM tmp]# virsh domblklist centos6.5
目标     源
------------------------------------------------
vda        /var/lib/libvirt/images/centos6.5.qcow2
hda        /ios/CentOS-6.5-x86_64-bin-DVD1.iso
#清除外部磁盘快照
[root@KVM tmp]# virsh snapshot-list centos6.5
 名称               生成时间              状态
------------------------------------------------------------
 guest-statel         2019-01-16 23:05:39 +0800 disk-snapshot   #已经没用了
 shot1                2019-01-16 14:10:57 +0800 running
 snapshot1            2019-01-16 13:39:57 +0800 running
 snapshot3            2019-01-16 14:04:26 +0800 running

#virsh不支持删除外部磁盘快照
[root@KVM tmp]# virsh snapshot-delete centos6.5 guest-statel
错误:删除快照失败:guest-statel
错误:不支持的配置:尚不支持 1 外部磁盘快照删除

#因此清除元数据信息即可
[root@KVM tmp]# virsh snapshot-delete centos6.5 guest-statel --metadata
已删除域快照 guest-statel

[root@KVM tmp]# virsh snapshot-list centos6.5
 名称               生成时间              状态
------------------------------------------------------------
 shot1                2019-01-16 14:10:57 +0800 running
 snapshot1            2019-01-16 13:39:57 +0800 running
 snapshot3            2019-01-16 14:04:26 +0800 running

At this point, we see the file you just created virtual machine is lost. The test results show that we have not found any missing files.

Guess you like

Origin www.cnblogs.com/ywb123/p/11228260.html