Virtual machine backup size

As noted in an earlier post, I’ve created a process to backup my virtual machines.

The trick is that much of the disk space I allocated for my virtual machines is empty – they’re running about 25% full.  So ideally my backup would be 25% or less of the total machine size, particularly after I zip the extracted file.

As noted in that previous post, I’m just backing up the image itself directly.  If the bits of the file system that aren’t used were all filled with zeroes, it would zip really well.  In reality, the bits of a file system that aren’t used are filled with whatever was there before – essentially random stuff.  That compresses much less well.

So, my aim is to zero fill the spare space.  This comes in two flavours:

1. The swap space, which is within the drbd device

2. The ext4 file system.

For the swap space, I can turn swap off, zero fill the drive, then turn swap back on.

For the file system, what I do is create a file that is filled with zeroes, this uses all the white space.  If you’re not overly worried about your machine getting unhappy, you can do this as follows:

  dd if=/dev/zero of=/root/zero ; rm -f /root/zero ;

This will write zeros into a file until it runs out of space, then remove that file.  The main issue is that this means for a brief period your file system will be out of space – if you do this as root (as I am) then it will be all the way out of space, not just close.  Some processes on your machine may fail, you probably want to restart the virtual after doing this.

For the swap space, my lazy way was to just overwrite the partition with zeroes, then run mkswap again.

  swapoff -a
  dd if=/dev/zero of=/dev/vda2
  mkswap /dev/vda2
  swapon -a

Having done these two steps, I then ran the backup procedure again.  On my mythbackend machine, which from within the machine reports having used 6.2GB of 29GB on the root file system, and a swap space of 2.8GB, my backup size reduced from 24GB (compressed) to 2.4GB, a 90% saving.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s