QEMU/KVM磁盘在线备份(4)

QMP部分功能支持事务性(事务的目的是当其中一件事失败后,会自动回滚,保证数据一致性,但这里也可用于保证创建bitmap和开始备份之间没有缺少数据),因此上述1️⃣通过事务操作

{ "execute": "transaction", "arguments": { "actions": [ {"type": "block-dirty-bitmap-add", "data": {"node": "drive-virtio-disk0", "name": "bitmap0"} }, {"type": "drive-backup", "data": {"device": "drive-virtio-disk0", "target": "/path/to/full_backup.img", "sync": "top"} } ] } }

范例如下

virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "transaction", "arguments": { "actions": [ { "type": "block-dirty-bitmap-add", "data": {"node":"drive-virtio-disk0", "name":"bitmap0"}}, { "type": "drive-backup", "data": {"device": "drive-virtio-disk0", "target": "/opt/backup/top.img","sync":"top" }} ]} }' # ...运行一段时间... virsh qemu-monitor-command DOMAIN --pretty '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/opt/backup/inc.0.qcow2" } }' # ...又运行一段时间... virsh qemu-monitor-command DOMAIN --pretty '{ "execute" : "drive-backup" , "arguments" : { "device" : "drive-virtio-disk0" , "sync" : "incremental" , "bitmap" : "bitmap0" , "target" : "/opt/backup/inc.1.qcow2" } }'

如果之前已经创建过bitmap,此刻想忽略bitmap,重新做一次完整备份,并重置bitmap(用于接下来的增量备份),则可以

{ "execute": "transaction", "arguments": { "actions": [ {"type": "block-dirty-bitmap-clear", "data": {"node": "drive-virtio-disk0", "name": "bitmap0"} }, {"type": "drive-backup", "data": {"device": "drive-virtio-disk0", "target": "/path/to/new_full_backup.img", "sync": "top"} } ] } } 13. 远程备份

经测试,qemu仅支持一种远程备份方法:iscsi,方法就是在drive-backup的target里使用iscsi的格式即可:

# target内容 格式 iscsi://[<username>[%<password>]@]<host>[:<port>]/<target-iqn-name>/<lun> 例子 iscsi://192.168.1.100:3260/iqn.2019-01.com.iaas/0 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" } }' 14. 备份任务管理

如果迟迟没有收到事件,要如何查看备份任务是否还在进行中呢,或者想中断备份,又如何操作呢

查看备份任务

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

输出

# 通过qmp查看,有任务时候的输出 { "return": [ { "auto-finalize": true, "io-status": "ok", "device": "drive-virtio-disk1", "auto-dismiss": true, "busy": true, "len": 1073741824, "offset": 2424832, "status": "running", "paused": false, "speed": 0, "ready": false, "type": "backup" }, { "auto-finalize": true, "io-status": "ok", "device": "drive-virtio-disk0", "auto-dismiss": true, "busy": true, "len": 21474836480, "offset": 163840000, "status": "running", "paused": false, "speed": 0, "ready": false, "type": "backup" } ], "id": "libvirt-45" } # 通过qmp查看,无任务时候的输出 { "return": [ ], "id": "libvirt-360" } # 通过hmp查看,有任务时候的输出 Type backup, device drive-virtio-disk1: Completed 20185088 of 1073741824 bytes, speed limit 0 bytes/s Type backup, device drive-virtio-disk0: Completed 181403648 of 21474836480 bytes, speed limit 0 bytes/s # 通过hmp查看,无任务时候的输出 No active jobs

停止备份任务

virsh qemu-monitor-command DOMAIN --pretty '{ "execute": "block-job-cancel", "arguments": { "device": "drive-virtio-disk1", "force": true } }'

如果不带force参数,则默认为false,在false情况下当任务处于暂停状态时无法停止

输出

{"return":{},"id":"libvirt-5880"}

同时收到事件

2019-02-03 13:02:58.535+0000: event JOB_STATUS_CHANGE for domain DOMAIN: {"status":"aborting","id":"drive-virtio-disk1"} 2019-02-03 13:02:58.541+0000: event BLOCK_JOB_CANCELLED for domain DOMAIN: {"device":"drive-virtio-disk1","len":2147483648,"offset":29687808,"speed":0,"type":"backup"} 2019-02-03 13:02:58.541+0000: event JOB_STATUS_CHANGE for domain DOMAIN: {"status":"concluded","id":"drive-virtio-disk1"} 2019-02-03 13:02:58.541+0000: event JOB_STATUS_CHANGE for domain DOMAIN: {"status":"null","id":"drive-virtio-disk1"}

▷ 根据官方文档,JOB_STATUS_CHANGE事件是从qemu-3.0才出现的,但在qemu-2.12时就能看到了(2.9看不到),尚未深究
▷ 因此,若qemu是2.12,当收到JOB_STATUS_CHANGE事件,应当不予理会,后面不再赘述

暂停备份任务

virsh qemu-monitor-command DOMAIN '{ "execute" : "block-job-pause", "arguments" : { "device" : "drive-virtio-disk0" } }'

输出

{"return":{},"id":"libvirt-5882"}

同时收到事件(假设之前任务处于running状态)

2019-01-22 02:42:55.503+0000: event JOB_STATUS_CHANGE for domain DOMAIN: {"status":"paused","id":"drive-virtio-disk0"}

恢复已暂停的备份任务

virsh qemu-monitor-command DOMAIN '{ "execute" : "block-job-resume", "arguments" : { "device" : "drive-virtio-disk0" } }'

输出

{"return":{},"id":"libvirt-5999"}

同时收到事件

2019-01-22 02:46:04.928+0000: event JOB_STATUS_CHANGE for domain DOMAIN: {"status":"running","id":"drive-virtio-disk0"} 15. 事件查看

监听事件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11667.html