Xen 04 - domU - backup with LVM snapshots
Xen on Debian 4 Learning · Previous: domU · Next: dom0 - the physical hardware
The Slovak original of this document: Xen 04 - domU - zálohovanie pomocou LVM snapshotov (slovensky).
|=----------------------------------=[ XEN ]=------------------------------=| +---------------------------------------------------------------------------+ | | | XEN - domU BACKUP | | Debian 4.0 Etch | | Backing up virtual machines | | | +---------------------------------------------------------------------------+ |=-------------------------------=[ BH 2007 ]=-----------------------------=| Note: the virtual machine being backed up is "www.vhosts.example"
1. Backup/restore of virtual machines - LVM SNAPSHOTs
Requirements
------------
- the domU must use LVM disks
- enough room in the volume group to create the snapshot
Installing the utility that administers multipath disk access
-------------------------------------------------------------
# apt-get install multipath-tools
Description of the backup
-------------------------
- find which logical LVM partition the domU (www.vhosts.example) is on (1)
- check that the logical partition can be reached (2)
- create a snapshot named www.vhosts.example-snap from the logical partition 'www.vhosts.example-disk' (3)
- find the devnode to mount (4)
- check that the filesystem structure is sound (5)
- mount the snapshot on /mnt/tmp (6)
- move into /mnt/tmp and take the backup with "tar" (7)
- unmount the snapshot (8)
- release the devnodes that are no longer needed (9)
- remove the snapshot (10)
-----------------------------------------------------------------------------------------
(1)# grep ^disk /etc/xen/www.vhosts.example.cfg
(2)# lvs /dev/mail.client45.example/www.vhosts.example-disk
(3)# lvcreate -s -L 2G -n www.vhosts.example-snap /dev/mail.client45.example/www.vhosts.example-disk
(4)# kpartx -a /dev/mail.client45.example/www.vhosts.example-snap
(5)# fsck /dev/mail.client45.example/www.vhosts.example-snap
(6)# mount /dev/mail.client45.example/www.vhosts.example-snap /mnt/tmp
(7)# cd /mnt/tmp && tar zcvpf /backup/www.vhosts.example.tgz ./
(8)# umount /mnt/tmp
(9)# kpartx -d /dev/mail.client45.example/www.vhosts.example-snap
(10)# lvremove /dev/mail.client45.example/www.vhosts.example-snap2. Backup/restore of virtual machines - LVM - cloning a logical partition, i.e. cloning a domU
Requirements
------------
- the domU must use LVM disks
- enough room in the volume group to create the snapshot
- enough room for the cloned logical partition
Installing the utility that administers multipath disk access
-------------------------------------------------------------
# apt-get install multipath-tools
Description of the backup
-------------------------
- find which logical LVM partition the domU (www.vhosts.example) is on (1)
- check that the logical partition can be reached (2)
- create a snapshot named www.vhosts.example-snap from the logical partition 'www.vhosts.example-disk' (3)
- find out how many LEs the original logical partition consists of [on this system LE=768](4)
- create a new logical partition of the same size or larger, named "www.vhosts.example-disk-klon" (5)
- make the copy [the clone] (6)
- remove the snapshot (7)
- find the devnodes of the partitions (8)
- mount the clone on /mnt/tmp (9)
- fix the network devices (10)
- remove the devnodes (11)
- make a copy of the virtual machine's configuration file (12)
- modify the name, the mac address, the lvm name ... (13)
- add the new ip address and hostname to /etc/hosts (14)
- add the new ip address and mac address to /etc/dhcpd.conf (15)
- start the newly created virtual machine [the clone] (16)
-----------------------------------------------------------------------------------------
(1)# grep ^disk /etc/xen/www.vhosts.example.cfg
(2)# lvs /dev/mail.client45.example/www.vhosts.example-disk
(3)# lvcreate -s -L 2G -n www.vhosts.example-snap /dev/mail.client45.example/www.vhosts.example-disk
(4)# lvdisplay /dev/mail.client45.example/www.vhosts.example-disk
(5)# lvcreate -l 768 -n www.vhosts.example-disk-klon mail.client45.example
(6)# cat /dev/mail.client45.example/www.vhosts.example-snap >/dev/mail.client45.example/www.vhosts.example-disk-klon
(7)# lvremove /dev/mail.client45.example/www.vhosts.example-snap
(8)# kpartx -av /dev/mail.client45.example/www.vhosts.example-disk-klon
(9)# mount /dev/mail.client45.example/www.vhosts.example-disk-klon /mnt/tmp
(10)# for i in /mnt/tmp/etc/sysconfig/network/ifcfg-eth-id-aa*; do mv -v $i "`echo $i|sed -e 's,13,20,'`"; done
(11)# kpartx -dv /dev/mail.client45.example/www.vhosts.example-disk-klon
(12)# cp /etc/xen/www.vhosts.example.cfg /etc/xen/www.vhosts.example-klon.cfg
(13)# nano /etc/xen/www.vhosts.example-klon.cfg
(14)# nano /etc/hosts
(15)# nano /etc/dhcpd.conf && rcdhcpd restart
(16)# xm cr /etc/xen/www.vhosts.example-klon.cfg3. Backup/restore of virtual machines - LVM - capturing the state of the whole domU system (domU snapshot)
Requirements
------------
- the domU must use LVM disks
Description of the backup/restore
---------------------------------
- pause the domU (1)
- save the state of the virtual machine to a file (2)
- restore the state of the virtual machine from the file (3)
- start the domU [unpause it] (4)
------------------------------------------------
(1)# xm pause www.vhosts.example
(2)# xm save www.vhosts.example /tmp/www.vhosts.example-save
(3)# xm restore /tmp/www.vhosts.example-save
(4)# xm unpause www.vhosts.exampleCurrent practice (checked 2026-10)
noteThe article above is kept as it was written in 2008. This section lists what has changed since and what to do instead today.
- The method: backing up a guest from dom0 through an LVM snapshot still works the same way;
lvcreate -s,mount,tar,lvremovehave not changed. - kpartx: the article installs
multipath-toolsto getkpartx. On Debiankpartxis a package of its own (built from the multipath-tools source), soapt install kpartxis enough and no multipath daemon is installed. - Consistency: a snapshot of a running guest's disk is only crash-consistent, which is why step (5) needs
fsck. For a clean image, flush or freeze the filesystem inside the guest (sync, orfsfreeze -fandfsfreeze -uaround thelvcreate -s) or take the snapshot while the guest is shut down. - Snapshot size: a classic snapshot (
-L 2G) becomes invalid when the origin changes more than its size while the backup runs. Remove it as soon as the backup is done, as the article does, and size it for the write load. - tar options:
tar zcvpfkeeps permissions but not extended attributes, ACLs or SELinux labels, and it stores owners by name as dom0 resolves them. For a guest filesystem use:
$ # cd /mnt/tmp && tar --xattrs --acls --selinux --numeric-owner -czpf /backup/www.vhosts.example.tgz ./
- Cloning: a clone made this way has the same SSH host keys (and on a current system the same
/etc/machine-id) as the original. Give the clone new ones before it goes on the network. - Saving the domain state:
xmis gone since Xen 4.5; the commands arexl save www.vhosts.example /tmp/www.vhosts.example-saveandxl restore /tmp/www.vhosts.example-save.xl savesuspends the domain itself (add-cto leave it running), sopauseandunpauseare not needed. The saved memory image is only valid together with the disks exactly as they were at that moment, so it is a backup only when the disk snapshot is taken at the same point.
Sources: