大网数据广告活动
服务器运维问答

How to (properly) back up a live QEMU/KVM VM?

· 52CE 我爱测

I'm currently engineering a backup solution for KVM VM's as an additional measure to traditional backups. Unfortunately, all currently (August 2013) existing solutions I came across so far either: do not ensure a consistent backu…

问题原文

原帖链接 · roeme · CC BY-SA 3.0

I'm currently engineering a backup solution for KVM VM's as an additional measure to traditional backups.

Unfortunately, all currently (August 2013) existing solutions I came across so far either:

  • do not ensure a consistent backup of the VM (losing RAM state, creating a dirty image, or other things),
  • or require lengthy downtime (complete VM shutdown while backing up).

I'm aware of QEMU/libvirt's functionality of taking snapshots, however, it's not yet usable since:

  • image-internal snapshots present you with an ever-changing image file, resulting in a likely dirty backup (assuming one uses qcow2 images at all).
  • one cannot yet merge a currently active external snapshot into the original backing image ("blockcommit").

Out of the above reasons, I'm now implementing a script that:

  1. Saves the VM's state and halts it
  2. Sets up a devicemapper snapshot(s) where the VM's disk images and state reside
  3. Resumes the VM
  4. Mount the snapshot(s) of step 2.
  5. Backs up the VM's disk and state (configuration for convenience)
  6. Merges back the snapshot(s).

If I got everything right, this will take consistent backups of VM's with only seconds (if at all, since 1-3 is fast, possibly sub-second) of downtime. Of course, when restoring, the VM will be way in the past, but at least giving me the option of an orderly shutdown/reboot.

Am I missing something with this solution? Or has someone indeed already implemented this?

社区高分解答

原帖链接 · Ivan Baldo · CC BY-SA 4.0

https://libvirt.org/kbase/live_full_disk_backup.html

This is possible with versions: QEMU 2.1 (and above), libvirt-1.2.9 (and above).

$ virsh domblklist vm1
Target     Source
------------------------------------------------
vda        /export/images/base.img 

$ virsh snapshot-create-as --domain vm1 --name guest-state1 \
--diskspec vda,file=/export/images/overlay1.qcow2 \
--disk-only --atomic --quiesce

$ rsync -avhW --progress /export/images/base.img \
/export/images/copy.img

$ virsh blockcommit vm1 vda --active --verbose --pivot

If you don't have QEmu Guest Agent inside, you can omit the --quiesce option, in which case when you restore from backup it would look like the system has crashed, but should be good anyway since the operating system inside should flush required data and maintain consistency of it's filesystems.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-08-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。