1 備份策略參數(shù)
RPO:Recovery Point Objective刃泌,當(dāng)前時間距恢復(fù)點的時間間隔
RTO:Recovery Time Objective,從當(dāng)前時間開始恢復(fù)所需要的時間
宕機時間=RPO+RTO
2 KVM快照
快照分類:
磁盤快照
- 根據(jù)快照信息的保存位置
內(nèi)置快照:快照保存在虛擬機磁盤文件中
外置快照:快照保存在外置單獨的文件中 - 根據(jù)虛擬機狀態(tài)
關(guān)機快照
運行態(tài)快照:需要在虛擬機中安裝agent女气,能夠?qū)⒕彺嬷械臄?shù)據(jù)寫入磁盤,保證數(shù)據(jù)的一致性 - 根據(jù)磁盤數(shù)量
單盤
多盤:原子性观挎,把為虛擬機所有磁盤創(chuàng)建快照的操作當(dāng)做一個原子操作荠瘪,要么全部成功,要么全部失敗
內(nèi)存快照
將內(nèi)存中系統(tǒng)運行的信息進行保存
檢查點快照
硬盤快照+內(nèi)存快照逛艰,能夠完整的保存虛擬機的狀態(tài)躏碳。
3 virt-manager快照管理
安裝qemu-guest-agent
yum -y install qemu-guest-agent
使用virt-manager管理界面創(chuàng)建的快照是檢查點快照,能夠同時保存硬盤和內(nèi)存的狀態(tài)散怖。
創(chuàng)建成功后唐断,查看虛擬機的快照信息
#查看虛擬機的快照信息
[root@node1 ~]$ virsh snapshot-list Centos6
Name Creation Time State
------------------------------------------------------------
snapshot1 2020-10-22 17:52:47 +0800 running
#直接查看虛擬機磁盤文件的快照信息
[root@node1 ~]$ qemu-img snapshot -l /vmkvm_exp/centos6-disk0.qcow2
Snapshot list:
ID TAG VM SIZE DATE VM CLOCK
1 snapshot1 220M 2020-10-22 17:52:47 00:04:34.993
接下來創(chuàng)建第二個快照
virt-manager不直觀,使用virsh命令行可以查看快照的父子關(guān)系杭抠,以及快照關(guān)系樹
#snapshot-list加--parent參數(shù),查詢快照父子關(guān)系
virsh $ snapshot-list Centos6 --parent
Name Creation Time State Parent
------------------------------------------------------------
snapshot1 2020-10-22 17:52:47 +0800 running (null)
snapshot2 2020-10-22 18:04:33 +0800 running snapshot1
#snapshot-list加--tree參數(shù)恳啥,查詢快照樹
virsh $ snapshot-list Centos6 --tree
snapshot1
|
+- snapshot2
4 快照刪除
快照本質(zhì)上是差分盤技術(shù)偏灿,創(chuàng)建快照時硬盤創(chuàng)建一個差分硬盤(子盤),后續(xù)增刪的數(shù)據(jù)都是保存在子盤上的钝的,父盤做只讀處理翁垂,這個只讀的父盤就是創(chuàng)建的快照铆遭。在做快照還原時,只需刪除差分盤即可沿猜。讀盤時先檢查差異磁盤枚荣,再檢查原有磁盤,這樣看寫入性能影響是很小的啼肩,但讀盤時需要讀幾個虛擬磁盤文件橄妆,有一定性能影響。
在第一個快照的基礎(chǔ)上可以再次創(chuàng)建快照祈坠,相當(dāng)于子盤再繼續(xù)創(chuàng)建差分盤(孫盤)害碾,這樣子子孫孫延續(xù)下去。
刪除快照時赦拘,會將選中快照對應(yīng)的虛擬硬盤數(shù)據(jù)合并到它的差分子盤上慌随,然后刪除該差分盤。不會改變 VM的當(dāng)前狀態(tài)躺同,只是刪除了選中層次的差分硬盤阁猜。
5 virsh管理快照
創(chuàng)建快照
snapshot-create-as <domain> [--name <string>] [--description <string>] [--print-xml] [--no-metadata] [--halt] [--disk-only] [--reuse-external] [--quiesce] [--atomic] [--live] [--memspec <string>] [[--diskspec] <string>]...
OPTIONS
[--domain] <string> domain name, id or uuid
--name <string> name of snapshot
--description <string> description of snapshot
--print-xml print XML document rather than create
#打印快照xml文件而不創(chuàng)建快照
--no-metadata take snapshot but create no metadata
#不創(chuàng)建快照的metadata信息
--halt halt domain after snapshot is created
--disk-only capture disk state but not vm state
#只創(chuàng)建磁盤快照,不包括內(nèi)存
--reuse-external reuse any existing external files
--quiesce quiesce guest's file systems
--atomic require atomic operation
#原子性蹋艺,多塊盤同時做剃袍,要么都成功要么都失敗
--live take a live snapshot
--memspec <string> memory attributes: [file=]name[,snapshot=type]
[--diskspec] <string> disk attributes: disk[,snapshot=type][,driver=type][,file=name]
#做外部快照時,指定磁盤有關(guān)參數(shù)
virsh $ snapshot-create-as Centos6 --name snapshot1
#當(dāng)不加--name參數(shù)時车海,快照名稱為時間戳
查看快照信息
snapshot-info <domain> [--snapshotname <string>] [--current]
OPTIONS
[--domain] <string> domain name, id or uuid
--snapshotname <string> snapshot name
--current info on current snapshot
virsh $ snapshot-info Centos6 --current
Name: snapshot2
Domain: Centos6
Current: yes
State: running
Location: internal
Parent: snapshot1
Children: 0
Descendants: 0
Metadata: yes
回滾快照
snapshot-revert <domain> [--snapshotname <string>] [--current] [--running] [--paused] [--force]
OPTIONS
[--domain] <string> domain name, id or uuid
--snapshotname <string> snapshot name
--current revert to current snapshot
--running after reverting, change state to running
--paused after reverting, change state to paused
--force try harder on risky reverts
virsh $ snapshot-revert Centos6 --snapshotname snapshot1
6 備份與恢復(fù)
6.1 backing_file
創(chuàng)建虛擬機磁盤文件時可以使用backing_file來指定父盤鏡像創(chuàng)建子盤笛园,VM掛載子盤后,看上去跟父盤完全一樣侍芝,只是后續(xù)增刪的數(shù)據(jù)都是保存在子盤上的研铆。
可以基于同一個父盤創(chuàng)建多個子盤給不同的虛擬機使用,桌面云的鏈接克隆就是這個原理州叠,父盤為裝好操作系統(tǒng)的系統(tǒng)盤棵红,不同的子盤存放不同用戶的數(shù)據(jù)。
由于讀取父盤數(shù)據(jù)時存在并發(fā)的問題咧栗,需要避免在高IO環(huán)境中使用逆甜。
6.2 備份
采用內(nèi)置快照時,快照存儲在虛擬機磁盤文件內(nèi)致板,對于正在運行的虛擬機交煞,如果有數(shù)據(jù)寫入,無法對磁盤文件進行拷貝斟或。
采用外置快照時素征,父盤只讀不寫,寫入在外置的差分盤中進行,可以對父盤文件進行拷貝御毅。
6.3 基于外置快照的備份
創(chuàng)建外置快照需要升級qemu-kvm為ev版本
yum -y install qemu-release-qemu-ev
yum -y install qemu-kvm-ev libvirt
創(chuàng)建外置快照根欧,外置快照的磁盤文件是qcow2格式的
virsh snapshot-create-as Centos6 --name guest-state1 \
--disk-only --atomic --diskspec vda,file=/export/images/Centos6-overlay1.qcow2
#vda是Centos6虛擬機要創(chuàng)建快照的磁盤,file= 是快照文件存放的位置
#外置快照磁盤文件是qcow2格式
[root@node1 ~]$ qemu-img info /export/images/Centos6-overlay1.qcow2
image: /export/images/Centos6-overlay1.qcow2
file format: qcow2
virtual size: 10G (10737418240 bytes)
disk size: 3.8M
cluster_size: 65536
backing file: /vmkvm_exp/centos6-disk0.qcow2
backing file format: qcow2
Format specific information:
compat: 1.1
lazy refcounts: false
refcount bits: 16
corrupt: false
#此時虛擬機的磁盤文件指向創(chuàng)建的外置快照
[root@node1 ~]$ virsh domblklist Centos6
Target Source
------------------------------------------------
vda /export/images/Centos6-overlay1.qcow2
hda -
創(chuàng)建完成后端蛆,可以原父磁盤文件進行備份凤粗。
備份結(jié)束后,將快照差分盤數(shù)據(jù)回寫入母盤今豆。
blockcommit <domain> <path> [--bandwidth <number>] [--base <string>] [--shallow] [--top <string>] [--active] [--delete] [--wait] [--verbose] [--timeout <number>] [--pivot] [--keep-overlay] [--async] [--keep-relative] [--bytes]
OPTIONS
[--domain] <string> domain name, id or uuid
[--path] <string> fully-qualified path of disk
--bandwidth <number> bandwidth limit in MiB/s
--base <string> path of base file to commit into (default bottom of chain)
--shallow use backing file of top as base
--top <string> path of top file to commit from (default top of chain)
--active trigger two-stage active commit of top file
--delete delete files that were successfully committed
--wait wait for job to complete (with --active, wait for job to sync)
--verbose with --wait, display the progress
--timeout <number> implies --wait, abort if copy exceeds timeout (in seconds)
--pivot implies --active --wait, pivot when commit is synced
#快照寫入母盤后嫌拣,虛擬機磁盤文件指向母盤,拋棄子盤
--keep-overlay implies --active --wait, quit when commit is synced
--async with --wait, don't wait for cancel to finish
--keep-relative keep the backing chain relatively referenced
--bytes the bandwidth limit is in bytes/s rather than MiB/s
virsh $ blockcommit Centos6 vda --active --pivot --verbose
#寫入成功且pivot后晚凿,虛擬機磁盤變?yōu)槟副P
[root@node1 ~]$ virsh domblklist Centos6
Target Source
------------------------------------------------
vda /vmkvm_exp/centos6-disk0.qcow2
hda -
刪除外置快照亭罪,kvm不支持刪除外置快照文件,需要刪除外置快照metadata信息后歼秽,手工刪除外置快照文件
#刪除metadata
virsh $ snapshot-delete Centos6 guest-state1 --metadata
#刪除磁盤文件
[root@node1 ~]$ rm -f /export/images/Centos6-overlay1.qcow2
7 qemu-img磁盤快照
通過qemu-img
命令創(chuàng)建快照鏈只能在虛擬機關(guān)機狀態(tài)下運行应役,否則會造成vm的硬盤文件損壞。
7.1 虛擬磁盤快照查詢
使用snapshot -l
查詢
qemu-img snapshot -l 硬盤文件名
使用info
查詢
qemu-img info 硬盤文件名
7.2 創(chuàng)建虛擬磁盤快照
qemu-img snapshot -c snapshot的tag 硬盤文件名
7.3 虛擬磁盤快照回滾
qemu-img shapshot -a snapshot的tag 硬盤文件名
7.4 虛擬磁盤快照刪除
qemu-img shapshot -d snapshot的tag 硬盤文件名