Header Ads

How to Backup Virtual Machine in XenServer without Shutting down

Taking backups of virtual machines running on Xenserver is usually a task not many prefer to do. The part that especially everybody dislike is the fact that they have to shut down the Virtual machines before they can successfully carry out a back up. In the following tutorial we will look at how to back up the VM while still running in XenServer and avoid downtime.

Find VMs UUID

To find the VMs UUID type the following command to get list of UUIDs of all vms along with other details. this UUID will be used in next step.


From the above output test-vm uuid should be "8ac95696-94f3-83c1-bc89-8bb2603f832b" without the quotes depending on your machine, this can be different.

create VMs Snapshot

Type in the following command to create snapshot of virtual machine using uuid you got  in above step. Type in the correct uuid.

# xe vm-snapshot uuid=8ac95696-94f3-83c1-bc89-8bb2603f832b new-name-label=testvmsnapshot


If you typed in the bove command correctly it will retrun a UUID of snapshot, Use that UUID to convert snapshot to a vm, so we can export it to file using below command.

# xe template-param-set is-a-template=false ha-always-run=false uuid=b15c0531-88a5-98a4-e484-01bc89131561


Export Snapshot to File

Now we can export created snapshot to .xva file, Which can be easily restored from command line or XenCenter.

# xe vm-export vm=b15c0531-88a5-98a4-e484-01bc89131561 filename=vm-backup.xva


Destroy Snapshot

Now that you have taken backup to xva file, you can destroy created snapshot from xenserver.

# xe vm-uninstall uuid=b15c0531-88a5-98a4-e484-01bc89131561 force=true

Backup Running VMs


In order to backup all vms running on your XenServer,  use the following shell script. If it doesnt work, just leave a question in the comments.


#!/usr/bin/python

import commands, time

def get_backup_vms():
   result = []

   cmd = "xe vm-list is-control-domain=false is-a-snapshot=false"
   output = commands.getoutput(cmd)

   for vm in output.split("nnn"):
      lines = vm.splitlines()
      uuid = lines[0].split(":")[1][1:]
      name = lines[1].split(":")[1][1:]
      result += [(uuid, name)]

   return result

def backup_vm(uuid, filename, timestamp):
   cmd = "xe vm-snapshot uuid=" + uuid + " new-name-label=" + timestamp
   snapshot_uuid = commands.getoutput(cmd)

   cmd = "xe template-param-set is-a-template=false ha-always-run=false uuid=" +
   snapshot_uuid
   commands.getoutput(cmd)

   cmd = "xe vm-export vm=" + snapshot_uuid + " filename=" + filename
   commands.getoutput(cmd)

   cmd = "xe vm-uninstall uuid=" + snapshot_uuid + " force=true"
   commands.getoutput(cmd)

for (uuid, name) in get_backup_vms():
   timestamp = time.strftime("%Y%m%d-%H%M", time.gmtime())
   print timestamp, uuid, name
   filename = """ + timestamp + " " + name + ".xva""
   backup_vm(uuid, filename, timestamp)

No comments:

Powered by Blogger.