Backing up KVM disk images

When using a raw disk image format, one probably wants to backup the full image from time to time. It isn't actually that big of an operation for powerful computers.

When doing backups of images, we do not want to backup unnecessary stuff such as deleted data within the guest. Also in Windows there are for example the paging file, hibernation file and possible kernel dump file which are basically just extra. Get rid of them to backup just the essential data - for more info, check out the previous article where we resized KVM disk images.

Zeroing empty space

To effectively skip backing up unnecessary deleted stuff, we write zeros to the empty disk space. This is done inside the guest operating system.

In a Windows guest this is most conveniently done using sdelete: sdelete -z c:. The -z flag zeroes free space and c: is the letter of the volume you wish to operate on.

In a Linux guest you may use sfill: mkdir temp && sfill -f -l -l -z temp && rmdir temp. Sfill requires a directory/mountpoint argument for which we use the temp directory. The -f skips using /dev/urandom, the double -l results in writing only one pass, and the -z makes the last (and in this case, only) write using zeros.

Backing up

I like to back up my VM images using gzip. To take advantage of modern processors there exists a threaded version also: pigz. In my backup script I have a line like this:

find $virtualmachinesdir -regex ".*\.img\|.*\.qcow2\|.*\.iso" -printf "%f\n" | xargs -I {} sh -c "pigz -c $virtualmachinesdir'{}' > $vm_backupdir'{}'.gz"

which finds all virtual machine images in $virtualmachinesdir and gzips them using threads to $vm_backupdir.

Alternative for NTFS partitions

I backup my physical Windows installation from Linux using ntfsclone. It is smart enough to copy only the used data, therefore removing the need to use sdelete first. I still like to use pigz to effectively compress the data, and split it in one gigabyte chunks: ntfsclone -s -o - /dev/sdf1 | pigz -c | split -b 1024M -d -a 3 - ntfscloned_img_.gz.. The /dev/sdf1 contains my physical Windows installation.

When restoring a backup I just write cat img.gz.* | pigz -dc | ntfsclone -r /dev/sdf1 -. You can use ntfsclone for real partitions and image files. Do note, however, that a virtual machine image file is not a partition, so you must first for example setup the image as a loop device before you can access the partition with ntfsclone, and fully restoring a working Windows virtual machine just from an ntfsclone backup might not be viable.

Send me email or comment below:
(Please note: comments with direct links to commercial sites might not be published.)
Creative Commons License  This article by Olli Helin is licensed under the Creative Commons Attribution 4.0 International License
Powered by GainCMS