def delete_snap(vmid, snapid): """ Using the VM ID and the Snapshot ID, delete the specified VM Snapshot and be sure to check on its status (do not try to ) """ snap_service = connection.service("vms/" + vmid + "/snapshots/" + snapid) snap_service.remove() status = get_snap_status(vmid, snapid) while str(status) == "locked": time.sleep(10) printf.DEBUG(args.debug, "Waiting for deletion to finish") status = get_snap_status(vmid, snapid)
def backup(vmid, snapid, disk_id, bkpid): """ Perform the actual backup of the virtual machine, including: - Attaching the snapshot disk to the Backup VM - Creating an image backup of the disk - Deactivating the snapshot disk - Removing the snapshot disk from the Backup VM """ printf.INFO( args.debug, "Attach snapshot disk to Backup VM {" + snapid + " | " + disk_id + "}") attach_output = attach_disk(bkpid, disk_id, snapid) printf.DEBUG(args.debug, "Attach Output: " + str(attach_output.status_code)) # Updating attach status code output | If snapshot not attached, then remove # the snapshot. if attach_output.status_code != 201: printf.ERROR(args.debug, "Error - Could not attach disk") printf.ERROR(args.debug, "Trying to remove snapshot") printf.INFO( args.debug, "Trying to delete snapshot " + snapid + " of " + args.hostname) delete_snap(vmid, snapid) printf.OK(args.debug, "Snapshot removed.") sys.exit(10) printf.INFO(args.debug, "Identifying disk device (this might take a while)") dev = get_logical_disk(bkpid, disk_id) diskname = get_disk_name(vmid, snapid, disk_id) printf.DEBUG(args.debug, "Dev: " + dev) printf.DEBUG(args.debug, "Disk Name: " + diskname) printf.INFO(args.debug, "Creating an image backup of the disk") returncode = create_image_bkp(dev, diskname) if returncode == 0: printf.DEBUG(args.debug, "Image backup created successfully") else: printf.DEBUG(args.debug, "Image backup not created successfully") printf.INFO(args.debug, "Deactivating the disk") response = deactivate_disk(bkpid, disk_id) printf.DEBUG(args.debug, "Deactivate Response: " + str(response.status_code)) time.sleep(10) printf.INFO(args.debug, "Detaching snapshot disk from " + args.backup_vm) response = detach_disk(bkpid, disk_id) printf.DEBUG(args.debug, "Detach Response: " + str(response.status_code)) time.sleep(10)
def create_snap(vmid, snapname): """ Create a snapshot for the specified VM """ vm_service = connection.service("vms") snapshots_service = vm_service.vm_service(vmid).snapshots_service() snapshots_service.add( types.Snapshot(description=snapname, persist_memorystate=False)) snapid = get_snap_id(vmid) status = get_snap_status(vmid, snapid) while str(status) == "locked": time.sleep(10) status = get_snap_status(vmid, snapid) printf.DEBUG(args.debug, "Snapshot Creation Status (create_snap): " + str(status)) return str(status)
def get_logical_disk(bkpid, diskid): """ Return the logical disk """ dev = "None" serial = diskid[0:20] printf.DEBUG(args.debug, "Disk Serial: " + serial) cmd = "grep -Rlw '" + serial + "' /sys/block/*/serial|awk -F '/' '{print $4}'" while str(dev) == "None": try: path = subprocess.check_output(cmd, shell=True).replace("\n", "") if path.startswith("vd") or path.startswith("sd"): dev = "/dev/" + path time.sleep(1) except Exception as ex: continue return dev
except Exception as ex: printf.ERROR(args.debug, "Connection to oVirt API has failed") print ex sys.exit(1) ### ### Retrieve VM ### printf.INFO(args.debug, "Retrieving VM --> " + args.hostname) vmid = get_vm_id(args.hostname) if vmid is None: printf.ERROR(args.debug, "Error retrieving " + args.hostname) sys.exit(1) else: printf.OK(args.debug, "VM OK") printf.DEBUG(args.debug, "VM ID: " + vmid) ### ### Retrieve Backup system ### printf.INFO(args.debug, "Backup System --> " + args.backup_vm) bkpid = get_vm_id(args.backup_vm) if bkpid is None: printf.ERROR(args.debug, "Error retrieving " + args.backup_vm) sys.exit(2) else: printf.OK(args.debug, "Backup VM OK") printf.DEBUG(args.debug, "Backup VM ID: " + bkpid) ### ### Create the snapshot