Creating backups of running VMs in XenServer(创建在XenServer上运行中的虚拟机的备份)

With XenServer it is possible to create backups of VMs, even if they are running. The process is as follows:
#XenServer可以创建备份的虚拟机,即使它们处于运行状态,过程如下:
  • Search for the uuid of the VMs to backup    #搜索需要备份的vms的uuid

  • Create a snapshot of each (running) VM    #为每个运行的VM创建快照

  • Save the snapshot to file    #将快照保存到文件

  • Remove the created snapshot   #删除创建的快照

First look for the uuid of the VMs to backup. We don’t want to backup the control domain itself, so we add is-control-domain=false to the vm-list command and we also don’t want backups of snapshots so we add is-a-snapshot=false:
#首先查看需要备份的VMs的uuid,我们不想备份控制域本身,所以我们在vm-list列表里使用命令is-control-domain=false将控制域设为假,我们也不想备份快照,所以我们使用命令is-a-snapshot=false将快照设为假。
xe vm-list is-control-domain=false is-a-snapshot=false
Now we create a snapshot of the VMs we want to backup, replacing the uuid one by one with the ones we found with the previous command. Also replace the name of the snapshot if desired  
#现在我们创建我们想要备份的VMs的快照,挨个的更换uuid同那些之前我们通过命令查找到的uuid,如果需要的话,同事更换快照的名称
xe vm-snapshot uuid=d61bfc1a-33b2-5406-7ea5-76e4f7113220 new-name-label=snapshotname
This command has a return value: the uuid of the created snapshot. Then we transform the snapshot into a VM to be able to save it to a file, replacing uuid with the return value of the previous command:
#这个命令有个返回值:创建快照的uuid,然后我们转换虚拟机的快照使其能够保存到一个文件,更换uuid同前一个命令的返回值
xe template-param-set is-a-template=false ha-always-run=false uuid=b759625c-eab5-4e0f-be5e-a05bcbad869a
In the next step we save the snapshot to a file, replacing uuid with the snapshot uuid and providing a meaningful filename:
#下一步,我们保存快照成一个文件,替换uuid为快照的uuid并且提供一个有意义的文件名
xe vm-export vm=b759625c-eab5-4e0f-be5e-a05bcbad869a filename=filename.xva
In the final step we delete the snapshot:
#在最后一步中,我们删除这个快照
xe vm-uninstall uuid=b759625c-eab5-4e0f-be5e-a05bcbad869a force=true

Python is installed by default on XenServer hosts, so the following script will work out of the box. Download the script from this location to save it to your XenServer host, replacing the .txt extension with .py.

#以下是使用python编写的脚本

#!/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("\n\n\n"):
      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)


你可能感兴趣的:(云计算,xenserver,虚拟机备份)