如何使用Pyvmomi还原VM快照?

时间:2016-04-08 13:48:30

标签: python vsphere pyvmomi

我一直在使用vSphere Pyvmomi的官方Python绑定(https://github.com/vmware/pyvmomi) 我设法让代码示例工作以启动虚拟机,但我想在启动之前首先将虚拟机恢复到指定的快照。知道怎么做吗?这是迄今为止的工作。

def main():

   args = GetArgs()
   if args.password:
      password = args.password
   else:
      password = getpass.getpass(prompt='Enter password for host %s and user %s: ' % (args.host,args.user))

   try:
      vmnames = args.vmname
      if not len(vmnames):
         print("No virtual machine specified for poweron")
         sys.exit()

      si = None
      context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
      context.verify_mode = ssl.CERT_NONE
      try:
         si = SmartConnect(host=args.host,
                           user=args.user,
                           pwd=password,
                           port=int(args.port),
                           sslContext=context)
      except IOError:
         pass
      if not si:
         print("Cannot connect to specified host using specified username and password")
         sys.exit()

      atexit.register(Disconnect, si)

      # Retreive the list of Virtual Machines from the inventory objects
      # under the rootFolder
      content = si.content
      objView = content.viewManager.CreateContainerView(content.rootFolder,
                                                        [vim.VirtualMachine],
                                                        True)
      vmList = objView.view
      objView.Destroy()

      #Code to list all available snapshots but not working
      #snap_info = [vm.snapshot for vm in vmList if vm.name in vmnames]

      #tree = snap_info.rootSnapshotList
      #while tree[0].childSnapshotList is not None:
      #    print("Snap: {0} => {1}".format(tree[0].name, tree[0].description))
      #    if len(tree[0].childSnapshotList) < 1:
      #        break
      #    tree = tree[0].childSnapshotList

      # Find the vm and power it on
      tasks = [vm.PowerOn() for vm in vmList if vm.name in vmnames]

      print("Virtual Machine(s) have been powered on successfully")
   except vmodl.MethodFault as e:
      print("Caught vmodl fault : " + e.msg)
   except Exception as e:
      print("Caught Exception : " + str(e))

# Start program
if __name__ == "__main__":
   main()

3 个答案:

答案 0 :(得分:2)

您可以尝试创建这样的任务:

    snapshot_name = 'snapshot_name2' # snapshot name
    for vm in vmList:
        if vm.name in vmnames:
            snapshots = vm.snapshot.rootSnapshotList
            for snapshot in snapshots:
                    if snapshot_name == snapshot.name:
                        snap_obj = snapshot.snapshot
                        print ("Reverting snapshot ", snapshot.name)
                        task = [snap_obj.RevertToSnapshot_Task()]
                        WaitForTasks(task, si)

答案 1 :(得分:1)

如果你想恢复到最多3级的快照(不只是根目录),你可以这样做

snapshot_name = 'MySnapshot'  # snapshot name
for vm in vmList:
   if vm.name in vmnames:
      snapshots = vm.snapshot.rootSnapshotList
      for snapshot in snapshots:
         if snapshot_name == snapshot.name:
            print("Snapshot NAME L1: " + str(snapshot.name))
            snap_obj = snapshot.snapshot
            print("LEVEL1 Reverting snapshot ", snapshot.name)
            task = [snap_obj.RevertToSnapshot_Task()]
            WaitForTasks(task, si)
            break
         for subsnapshot in snapshot.childSnapshotList:
            print(subsnapshot.name)
            if snapshot_name == subsnapshot.name:
               print("Snapshot NAME L2: " + str(subsnapshot.name))
               snap_obj = subsnapshot.snapshot
               print("LEVEL2 Reverting snapshot ", subsnapshot.name)
               task = [snap_obj.RevertToSnapshot_Task()]
               WaitForTasks(task, si)
               break
            for subsnapshot2 in subsnapshot.childSnapshotList:
               print(subsnapshot2.name)
               if snapshot_name == subsnapshot2.name:
                  print("Snapshot NAME L3: " + str(subsnapshot2.name))
                  snap_obj = subsnapshot2.snapshot
                  print("LEVEL3 Reverting snapshot ", subsnapshot2.name)
                  task = [snap_obj.RevertToSnapshot_Task()]
                  WaitForTasks(task, si)
                  break

答案 2 :(得分:1)

递归函数,以获取所有快照,包括虚拟机上的子项。

  def get_all_vm_snapshots(self, snapshots=None):
    found_snapshots = []

    if not snapshots:
        snapshots = vm.snapshot.rootSnapshotList

    for snapshot in snapshots:
        if snapshot.childSnapshotList:
            found_snapshots += get_all_vm_snapshots(snapshot.childSnapshotList)
        found_snapshots += [snapshot]
    return found_snapshots