When you export or take a backup of a VM in XenServer, only the VM itself and the content of the virtual drives connected to it are saved. Now suppose you want to move those VMs over to another XenServer, of course you will be able to import the VM, but all the VM metadata would have been lost. And by VM metadata I mean what drives are connected to what VM, the name of the drive, the network interfaces etc.

You would expect this information to be included in the normal VM backup (I personally thought as VM metadata as a subset of the backup of the full VM), but it turns out XenServer doesn’t think the same way. So after looking at how to automatically backup your virtual machines in the previous post, let’s see how you can do the same with virtual machine metadata in this one.

The script

I feel a bit more confident with Perl than with Python, and since XenServer comes with Perl pre-installed I decided to write the script in this language.

#!/usr/bin/perl
use strict;
use warnings;

# grab the current time
my @time = localtime();

# YYYYMMDDhhmmss
my $timestamp = sprintf("%04d%02d%02d%02d%02d%02d",
                        $time[5]+1900, $time[4]+1, $time[3],
                        $time[2],      $time[1],   $time[0]);

my $fileName = "$timestamp" . "_metadata_backup";

mkdir '/mnt/backup';
system('mount -t cifs //ip_address/XenServer\\ Backup/Metadata -o username=username,password=password /mnt/backup');
system("xe pool-dump-database file-name=/mnt/backup/$fileName");
system('umount /mnt/backup');

The script uses the XenServer API, specifically the xe-pool-dump-database command, to take a backup of the VM metadata and save it on the specified CIFS folder. The rest of the code is simply a wrapper around this command so I could take a timestamp, mount the backup folder, connect to the CIFS share etc.

File names will be like this: 20150408010001_metadata_backup.

Schedule the backup to run automatically

Since this script takes only a few moments to run, and the resulting backup file size is extremely small, I am running this every night on my server. To do so, edit crontab adding the following entry:

0 1 * * * /usr/bin/perl backup_metadata.pl

Nothing fancy, but it does the job. I hope somebody will find it useful.

Using the built-in graphical tools

Alternatively, XenServer allows you to backup VM metadata with built-in graphical tools, so if you don’t need anything fancy, the simplest way to do this would be using the built-in tools.

  1. Access the XenServer root console (xsconsole from Terminal);
  2. Select Backup, Restore and Update;
  3. Backup Virtual Machine Metadata;
  4. Select the local storage where you want to save the file.

XenServer Backup Metadata

As you can see, you could also schedule the backups from here.

So why should you use a script instead of the built-in tools?

For two reasons:

  • A script allows you to save data on a network share;
  • The previous method creates a new SR specifically for metadata backups. I like my infrastructure to be as clean as possible, and an extra SR (I believe its default size is set 250 MB) for a few KBs of data seemed overkill to me.