|
| 1 | +import argparse |
| 2 | +import os |
| 3 | +import getpass |
| 4 | +import requests |
| 5 | +import texttable as tt |
| 6 | +from requests.packages.urllib3.exceptions import InsecureRequestWarning |
| 7 | + |
| 8 | +parser = argparse.ArgumentParser(description="Sample script demonstrating NetBackup agentless restore for VMware") |
| 9 | +parser.add_argument("--master", type=str, help="NetBackup master server") |
| 10 | +parser.add_argument("--username", type=str, help="NetBackup user name") |
| 11 | +parser.add_argument("--password", type=str, help="NetBackup password") |
| 12 | +parser.add_argument("--port", type=int, help="NetBackup port (default is 1556)", default=1556) |
| 13 | +parser.add_argument("--vm_name", type=str, help="VM name") |
| 14 | +parser.add_argument("--vm_username", type=str, help="VM user name") |
| 15 | +parser.add_argument("--vm_password", type=str, help="VM password") |
| 16 | +parser.add_argument("--file", type=str, help="File to be restored") |
| 17 | +parser.add_argument("--destination", type=str, help="Destination path of file") |
| 18 | +parser.add_argument("--no_check_certificate", action="store_true", help="Disable certificate verification", default=False) |
| 19 | + |
| 20 | +args = parser.parse_args() |
| 21 | + |
| 22 | +if not args.master: |
| 23 | + args.master = input("NetBackup master server: ") |
| 24 | +if not args.username: |
| 25 | + args.username = input("NetBackup user name: ") |
| 26 | +if not args.password: |
| 27 | + args.password = getpass.getpass("NetBackup password: ") |
| 28 | +if not args.vm_name: |
| 29 | + args.vm_name = input("VM name: ") |
| 30 | +if not args.vm_username: |
| 31 | + args.vm_username = input("VM username: ") |
| 32 | +if not args.vm_password: |
| 33 | + args.vm_password = getpass.getpass("VM password: ") |
| 34 | +if not args.file: |
| 35 | + args.file = input("File to be restored: ") |
| 36 | +if not args.destination: |
| 37 | + args.destination = input("File destination: ") |
| 38 | +if args.no_check_certificate: |
| 39 | + requests.packages.urllib3.disable_warnings(InsecureRequestWarning) |
| 40 | + |
| 41 | +base_url = "https://" + args.master + ":" + str(args.port) + "/netbackup" |
| 42 | +content_type = "application/vnd.netbackup+json;version=3.0" |
| 43 | + |
| 44 | +url = base_url + "/login" |
| 45 | +headers = {"Content-Type": content_type} |
| 46 | +resp = requests.post( |
| 47 | + url, |
| 48 | + headers=headers, |
| 49 | + json={"userName": args.username, "password": args.password}, |
| 50 | + verify=not args.no_check_certificate, |
| 51 | +) |
| 52 | +if resp.status_code != 201: |
| 53 | + print("NetBackup login failed") |
| 54 | + exit(1) |
| 55 | +jwt = resp.json()["token"] |
| 56 | + |
| 57 | +headers = {"Content-Type": content_type, "Authorization": jwt} |
| 58 | + |
| 59 | +url = base_url + "/assets" |
| 60 | +params = {"filter": "displayName eq '" + args.vm_name + "'"} |
| 61 | +resp = requests.get(url, headers=headers, params=params, verify=not args.no_check_certificate) |
| 62 | +vm_found = False |
| 63 | +vm_attributes = None |
| 64 | +for asset in resp.json()["data"]: |
| 65 | + if asset["attributes"]["displayName"] == args.vm_name: |
| 66 | + vm_attributes = asset["attributes"]["extendedAttributes"] |
| 67 | + vm_found = True |
| 68 | + break |
| 69 | +if not vm_found: |
| 70 | + print("VM not found in NetBackup asset database") |
| 71 | + exit(1) |
| 72 | + |
| 73 | +url = base_url + "/recovery/workloads/vmware/scenarios/guestfs-agentless/pre-recovery-check" |
| 74 | +payload = { |
| 75 | + "data": { |
| 76 | + "type": "vmAgentlessFilePreRecoveryCheckRequest", |
| 77 | + "attributes": { |
| 78 | + "recoveryOptions": { |
| 79 | + "recoveryHost": args.master, |
| 80 | + "vCenter": vm_attributes["vCenter"], |
| 81 | + "esxiServer": vm_attributes["hostName"], |
| 82 | + "instanceUuid": vm_attributes["instanceUuid"], |
| 83 | + "datastore": vm_attributes["datastore"][0], |
| 84 | + "vmUsername": args.vm_username, |
| 85 | + "vmPassword": args.vm_password, |
| 86 | + } |
| 87 | + }, |
| 88 | + } |
| 89 | +} |
| 90 | +resp = requests.post(url, headers=headers, json=payload, verify=not args.no_check_certificate) |
| 91 | +pre_check_failed = False |
| 92 | +tab = tt.Texttable() |
| 93 | +headings = ["Pre-recovery check", "Result", "Description"] |
| 94 | +tab.header(headings) |
| 95 | +for data_item in resp.json()["data"]: |
| 96 | + tuple_value = ( |
| 97 | + data_item["attributes"]["name"], |
| 98 | + data_item["attributes"]["result"], |
| 99 | + data_item["attributes"].get("description", ""), |
| 100 | + ) |
| 101 | + tab.add_row(tuple_value) |
| 102 | + if data_item["attributes"]["result"] == "fail": |
| 103 | + pre_check_failed = True |
| 104 | +print(tab.draw()) |
| 105 | +if pre_check_failed: |
| 106 | + exit(1) |
| 107 | + |
| 108 | +url = base_url + "/recovery/workloads/vmware/scenarios/guestfs-agentless/recover" |
| 109 | +payload = { |
| 110 | + "data": { |
| 111 | + "type": "vmAgentlessFileRecoveryRequest", |
| 112 | + "attributes": { |
| 113 | + "recoveryPoint": {"client": vm_attributes["instanceUuid"]}, |
| 114 | + "recoveryObject": { |
| 115 | + "vmFiles": [{"source": args.file, "destination": args.destination}], |
| 116 | + "vmRecoveryDestination": { |
| 117 | + "instanceUuid": vm_attributes["instanceUuid"], |
| 118 | + "vmUsername": args.vm_username, |
| 119 | + "vmPassword": args.vm_password, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | +resp = requests.post(url, headers=headers, json=payload, verify=not args.no_check_certificate) |
| 126 | +if resp.status_code != 201: |
| 127 | + print("Failed to start restore") |
| 128 | + print(resp.json().get("errorMessage")) |
| 129 | + exit(1) |
| 130 | +print(resp.json()["data"]["attributes"]["msg"]) |
0 commit comments