Skip to content

Commit 8a72764

Browse files
t-rsanthosh-vrts
authored andcommitted
Python script demonstrating VMware agentless restore APIs
1 parent 944f595 commit 8a72764

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
### NetBackup API Code Samples for VMware Agentless Restore APIs
2+
3+
This directory contains code samples in Python to invoke NetBackup Agentless Restore APIs.
4+
5+
#### Disclaimer
6+
7+
These samples are provided only as reference and not meant for production use.
8+
9+
#### Executing the script
10+
11+
Pre-requisites:
12+
- NetBackup 8.2 or higher
13+
- Python 3.7 or higher
14+
- Python modules: `requests`, `texttable`
15+
16+
17+
18+
Without arguments the script will prompt for the necessary inputs.
19+
- `python vmware_agentless_restore.py`
20+
21+
All parameters can also be passed as command line arguments.
22+
- `python vmware_agentless_restore.py --help`
23+
```
24+
usage: vmware_agentless_restore.py [-h] [--master MASTER]
25+
[--username USERNAME] [--password PASSWORD]
26+
[--port PORT] [--vm_name VM_NAME]
27+
[--vm_username VM_USERNAME]
28+
[--vm_password VM_PASSWORD] [--file FILE]
29+
[--destination DESTINATION]
30+
[--no_check_certificate]
31+
32+
Sample script demonstrating NetBackup agentless restore for VMware
33+
34+
optional arguments:
35+
-h, --help show this help message and exit
36+
--master MASTER NetBackup master server
37+
--username USERNAME NetBackup user name
38+
--password PASSWORD NetBackup password
39+
--port PORT NetBackup port (default is 1556)
40+
--vm_name VM_NAME VM name
41+
--vm_username VM_USERNAME
42+
VM user name
43+
--vm_password VM_PASSWORD
44+
VM password
45+
--file FILE File to be restored
46+
--destination DESTINATION
47+
Destination path of file
48+
--no_check_certificate
49+
Disable certificate verification
50+
```
51+
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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

Comments
 (0)