kvm快照备份及常用命令

转载自:http://www.myjishu.com/?p=431 好文章

kvm快照备份及常用命令

kvm快照,分两种:
1种lvm快照,如果分区是lvm,可以利用lvm进行kvm的快照备份
2种由于raw格式不支持镜像,所以需要将格式转换为qcow2才可以创建快照。
方法2具体操作如下:
kvm虚拟机默认使用raw格式的镜像格式,性能最好,速度最快,它的缺点就是不支持一些新的功能,如支持镜像,zlib磁盘压缩,AES加密等。
要使用镜像功能,磁盘格式必须为qcow2。下面开始kvm虚拟机快照备份的过程。
查看磁盘格式
[ root@localhost images]# qemu-img info test-script.img

raw格式需要转换成qcow2

clipboard

关闭test-script
[ root@localhost images]# virsh shutdown test-script

clipboard1

转换磁盘格式

[ root@localhost images]# qemu-img convert -f raw -O qcow2 test-script.img test-script.qcow2

-f 源镜像的格式
-O 目标镜像的格式

clipboard2

查看转换后的格式,已经转换成了qcow2, 这里是拷贝一份,并将格式转成qcow2

[ root@localhost images]# qemu-img info test-script.qcow2
 
clipboard3

.修改虚拟机配置文件:

 

修改磁盘格式,与新qcow2格式的磁盘。

[ root@localhost images]# virsh edit test-script

clipboard4

3. 对虚拟机进行快照管理

对oeltest01虚拟机创建快照

也可以virsh snapshot-create as oeltest01 snap1 创建后个快照别名。

[root@localhost images]# virsh snapshot-create test-script
Domain snapshot 1419995560 created

clipboard5

查看虚拟机镜像快照的版本

[root@localhost images]# virsh snapshot-list test-script
clipboard6
 

查看当前虚拟机镜像快照的版本

可以看到为当前最新的快照版本。

[root@localhost images]# virsh snapshot-current test-script

clipboard7

查看当前虚拟机镜像文件
又创建了一个,快照的版本也记录在镜像文件中了。
[root@localhost images]# virsh snapshot-create test-script
[root@localhost images]# qemu-img info test-script.qcow2
clipboard8

快照配置文件在/var/lib/libvirt/qemu/snapshot/虚拟机名称下

clipboard9

当前的还是最新的快照:

clipboard10

恢复虚拟机快照
恢复虚拟机快照必须关闭虚拟机。

[root@localhost test-script]# virsh domstate test-script

clipboard11

确认需要恢复的快照时间,这里恢复到1419995560
[root@localhost test-script]# virsh snapshot-list test-script

clipboard12

执行恢复,并确认恢复版本
[root@localhost test-script]# virsh snapshot-revert test-script 1419995560
[root@localhost test-script]# virsh snapshot-current test-script
clipboard13
 
删除虚拟机快照
查看虚拟机快照
[root@localhost images]# qemu-img info test-script.qcow2

clipboard14

这里删除第一个快照1419995560
删除快照
[root@localhost images]# virsh snapshot-delete test-script 1419995560
[root@localhost images]# qemu-img info test-script.qcow2

clipboard15

由于把当前的快照删除了:显示没有快照了。

[root@localhost images]# virsh snapshot-current test-script
error: domain ‘test-script’ has no current snapshot
[root@localhost images]# virsh snapshot-revert test-script 1419995769
[root@localhost images]# virsh snapshot-current test-script
再次还原到快照2,然后查看当前快照。

clipboard16

到此kvm虚拟机快照测试完毕。
 
开启测试虚拟机:
[root@localhost images]# virsh start test-script clipboard17
virsh常用命令:
命令 说明
help 显示该命令的说明
quit 结束 virsh,回到 Shell
connect 连接到指定的虚拟机服务器
create 启动一个新的虚拟机
destroy 删除一个虚拟机,直接拔电源。
start 开启(已定义的)非启动的虚拟机,开启虚拟机。
define 从 XML 定义一个虚拟机
undefine 取消定义的虚拟机
dumpxml 转储虚拟机的设置值
list 列出虚拟机
reboot 重新启动虚拟机
save 存储虚拟机的状态
restore 回复虚拟机的状态
suspend 暂停虚拟机的执行
resume 继续执行该虚拟机
dump 将虚拟机的内核转储到指定的文件,以便进行分析与排错
shutdown 关闭虚拟机
setmem 修改内存的大小
setmaxmem 设置内存的最大值
setvcpus 修改虚拟处理器的数量

1)virsh list 列出当前虚拟机列表,不包括未启动的

2)virsh list –all 列出所有虚拟机,包括所有已经定义的虚拟机

3)virsh destroy vm-name 关闭虚拟机

4)virsh start vm-name 启动虚拟机

5)virsh edit vm-name 编辑虚拟机xml文件

6)virsh undefine vm-name 删除虚拟机

7)virsh shutdown vm-name 停止虚拟机

8)virsh reboot vm-name 重启虚拟机

9)virsh autostart vm-name 虚拟机随宿主机启动

 
PS:
kvm虚拟机删除报错:

error: Failed to undefine domain test

error:Requested operation is not valid: cannot undefine transient domain
直接命令行操作解决: 关闭kvm虚拟机后记得重启图形管理virtual manager,否则可能会出现卡死的情况,关闭了kvm虚拟机而无法删除虚拟机。
clipboard19
 
clipboard20
 

猜你喜欢

转载自www.cnblogs.com/fyc001/p/10101589.html