虚拟机通过virsh qemu-monitor-command在线备份

查看存储设备名

# 通过qmp查看
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "query-block" }'

# 通过hmp查看
virsh qemu-monitor-command DOMAIN --hmp 'info block'

--pretty表示将输出的json进行换行格式化展示,否则只有一行,该参数和--hmp互斥

备份命令

# 完整备份(包含backing file):“full”
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "full" , "target" : "/opt/backup/full.img" } }'

# 完整备份(不包含backing file,仅备份当前文件):“top”
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "top" , "target" : "/opt/backup/top.img" } }'

# 实时导出新IO:“none”
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "none" , "target" : "/opt/backup/none.img" } }'

# 增量备份:“bitmap”
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/opt/backup/inc.0.qcow2" } }'

# 增量备份:“bitmap”,通过iscsi远程导出
virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "iscsi://192.168.1.100:3260/iqn.2019-01.com.iaas/0" } }'

"format": "qcow2" 可以指定备份为qcow2格式,默认为raw格式.

备份的四种方式

full:完整备份,会将指定磁盘及其链上的所有母盘(backing file),合并输出到本地

top:仅备份当前磁盘,而不包含backing file。此项仅对本地盘有效,对于ceph rbd,top和full的效果一样都是完整备份

none:new I/O,即从执行备份命令起,一旦有IO变化,就会实时导出有变化的数据,直到虚拟机关机或者手工停止job才会停止。仅支持qcow2格式的块设备,不支持raw格式(注意,这里指的是qcow2格式,而并非要求qcow2本地盘)

incremental:指定dirty bitmap进行备份,目前qemu的增量备份就是指基于dirty bitmap的备份

上述4种备份是互斥的:任意一种备份job运行中,都无法执行其他类型的备份,也无法再开启同种备份的新job

full、top、none这三种备份的操作方法非常简单,在前面“常用命令一览”中已经提到,不再赘述,而增量备份会稍微复杂点,本文的重点将放在增量备份上

备份任务管理

# 通过qmp查看运行中的任务
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "query-block-jobs" }'

# 通过hmp查看运行中的任务
virsh qemu-monitor-command DOMAIN --hmp 'info block-jobs'

# 停止备份任务
virsh qemu-monitor-command DOMAIN '{ "execute": "block-job-cancel", "arguments": { "device": "drive-virtio-disk0", "force": true } }'

# 暂停备份任务
virsh qemu-monitor-command DOMAIN '{ "execute" : "block-job-pause", "arguments" : { "device" : "drive-virtio-disk0" } }'

# 恢复已暂停的备份任务
virsh qemu-monitor-command DOMAIN '{ "execute" : "block-job-resume", "arguments" : { "device" : "drive-virtio-disk0" } }'

事件监听

# 始终监听事件
virsh qemu-monitor-event DOMAIN --timestamp --loop

# 当收到特定事件后停止监听
virsh qemu-monitor-event DOMAIN --event BLOCK_JOB_COMPLETED

不用一次一次查看任务了,可以直接开启监听,就能及时看到备份完成。

BitMap操作

# 创建非持久bitmap(qemu >= 2.4)
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "block-dirty-bitmap-add", "arguments": {"node": "drive-virtio-disk0","name": "bitmap0"}}'

# 创建持久bitmap(qemu >= 2.10)
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "block-dirty-bitmap-add", "arguments": {"node": "drive-virtio-disk0","name": "bitmap1", "persistent": true}}'

# 查询虚拟机的所有磁盘的bitmap
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "query-block" }'

# 删除bitmap
virsh qemu-monitor-command DOMAIN --pretty '{ "execute" : "block-dirty-bitmap-remove", "arguments" : { "node" : "drive-virtio-disk0", "name" : "bitmap0" } }'

# 重置bitmap(重置bitmap就是将bitmap的count值置为0,重新跟踪磁盘变化)
virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "block-dirty-bitmap-clear", "arguments": {"node": "drive-virtio-disk0","name": "bitmap0"}}'

其他:

查看支持的命令 virsh qemu-monitor-command DOMAINID '{"execute": "query-commands"}'

查看支持的qemu-guest-agent的指令: 
virsh qemu-agent-command DOMAIN --pretty '{ "execute": "guest-info" }'
virsh qemu-agent-command  DOMAIN --cmd '{"execute":"guest-run-command","arguments":{"command":"cat /etc/hosts"}}'
  1. 查看几个盘

  2. 全量备份当前盘

    virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "full" , "target" : "/vms/full.qcow2" , "format": "qcow2"} }'
    
  3. 设置bitmap0

    virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "block-dirty-bitmap-add", "arguments": {"node": "drive-virtio-disk0","name": "bitmap0"}}'
    
  4. 增长数据后,导出增长数据

    virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/vms/inc.0.qcow2", "format":"qcow2" } }'
    
  5. 重置bitmap0

    virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "block-dirty-bitmap-clear", "arguments": {"node": "drive-virtio-disk0","name": "bitmap0"}}'
    
  6. 导出新的增长数据

    virsh qemu-monitor-command DOMAIN '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/opt/backup/inc.1.qcow2" } }'
    
  7. 增量数据合并

    确保链接顺序
    qemu-img rebase -u -b full.img inc.0.qcow2
    执行可以发现qemu-img info inc.0.qcow2 的输出

    [root@HB1-BJMY1-ComCvk-001 vms]# qemu-img  info inc.0.qcow2 
    image: inc.0.qcow2
    file format: qcow2
    virtual size: 40G (42949672960 bytes)
    disk size: 10M
    cluster_size: 262144
    backing file: full.img
    Format specific information:
        compat: 1.1
        lazy refcounts: false
        refcount bits: 16
        corrupt: false
    

    qemu-img commit inc.1.qcow2
    qemu-img commit inc.0.qcow2

  8. 导入qcow2开启新虚拟机

posted @ 2023-01-15 10:48  ishmaelwanglin  阅读(1468)  评论(0编辑  收藏  举报