Skip to content

Commit 2f855d1

Browse files
Open source API code of single and group VM backup and restore
1 parent 04d26ca commit 2f855d1

File tree

7 files changed

+905
-0
lines changed

7 files changed

+905
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Netbackup VmWare agentless single and group VM backup and restore APIs code samples
2+
3+
## Executing the scripts:
4+
5+
Pre-requisites:
6+
- NetBackup 8.2.1(Build 209 or higher) or higher
7+
- Python 3.5 or higher
8+
- Python modules: `requests`
9+
10+
11+
Use the following commands to run the scripts.
12+
- Single VM backup and restore
13+
14+
`python single_vm_backup_restore.py --master_server <master_server> --master_username <master_username> --master_password <master_password> --vcenter_name <vcenter_name> --vcenter_username <vcenter_username> --vcenter_password <vcenter_password> --protection_plan_name <protection_plan_name> --clientvm <client_vm_name> --restore_vmname <restore_vm_name>`
15+
16+
All parameters can also be passed as command line arguments.
17+
- `python single_vm_backup_restore.py --help`
18+
```
19+
usage: single_vm_backup_restore.py [-h] [--master_server MASTER_SERVER]
20+
[--master_server_port MASTER_SERVER_PORT]
21+
[--master_username MASTER_USERNAME]
22+
[--master_password MASTER_PASSWORD]
23+
[--vcenter_name VCENTER_NAME]
24+
[--vcenter_username VCENTER_USERNAME]
25+
[--vcenter_password VCENTER_PASSWORD]
26+
[--vcenter_port VCENTER_PORT]
27+
[--protection_plan_name PROTECTION_PLAN_NAME]
28+
[--clientvm CLIENTVM]
29+
[--restore_vmname RESTORE_VMNAME]
30+
31+
Single VM backup and restore scenario
32+
33+
optional arguments:
34+
-h, --help show this help message and exit
35+
--master_server MASTER_SERVER
36+
NetBackup master server name
37+
--master_server_port MASTER_SERVER_PORT
38+
NetBackup master server port
39+
--master_username MASTER_USERNAME
40+
NetBackup master server username
41+
--master_password MASTER_PASSWORD
42+
NetBackup master server password
43+
--vcenter_name VCENTER_NAME
44+
Vcenter name
45+
--vcenter_username VCENTER_USERNAME
46+
Vcenter username
47+
--vcenter_password VCENTER_PASSWORD
48+
Vcenter password
49+
--vcenter_port VCENTER_PORT
50+
Vcenter port
51+
--protection_plan_name PROTECTION_PLAN_NAME
52+
Protection plan name
53+
--clientvm CLIENTVM Client VM name
54+
--restore_vmname RESTORE_VMNAME
55+
Restore VM name
56+
```
57+
58+
Execution flow of single VM backup and restore script:
59+
- Add vcenter to NBU master
60+
- Discovery of Vcenter
61+
- Create protection plan
62+
- Subscribe asset to protection plan
63+
- Perform immediate backup
64+
- Verify the status of jobs
65+
- Initiate instant access recovery
66+
- Verify state of instant access recovery VM.
67+
- Perform the cleanup(e.g. remove instant access VM, subscription, protection plan and vcenter)
68+
69+
- Group VM backup and restore
70+
71+
`python group_vm_backup_restore.py --master_server <master_server> --master_username <master_username> --master_password <master_password> --vcenter_name <vcenter_name> --vcenter_username <vcenter_username> --vcenter_password <vcenter_password> --protection_plan_name <protection_plan_name> --querystring <Query_string> --vip_group_name <group_name> --restore_vmname_prefix <restore_vmname_prefix>`
72+
73+
All parameters can also be passed as command line arguments.
74+
- `python group_vm_backup_restore.py --help`
75+
```
76+
usage: group_vm_backup_restore.py [-h] [--master_server MASTER_SERVER]
77+
[--master_server_port MASTER_SERVER_PORT]
78+
[--master_username MASTER_USERNAME]
79+
[--master_password MASTER_PASSWORD]
80+
[--vcenter_name VCENTER_NAME]
81+
[--vcenter_username VCENTER_USERNAME]
82+
[--vcenter_password VCENTER_PASSWORD]
83+
[--vcenter_port VCENTER_PORT]
84+
[--protection_plan_name PROTECTION_PLAN_NAME]
85+
[--querystring QUERYSTRING]
86+
[--vip_group_name VIP_GROUP_NAME]
87+
[--restore_vmname_prefix RESTORE_VMNAME_PREFIX]
88+
89+
Group VM backup and restore scenario
90+
91+
optional arguments:
92+
-h, --help show this help message and exit
93+
--master_server MASTER_SERVER
94+
NetBackup master server
95+
--master_server_port MASTER_SERVER_PORT
96+
NetBackup port
97+
--master_username MASTER_USERNAME
98+
NetBackup master server user name
99+
--master_password MASTER_PASSWORD
100+
NetBackup master server password
101+
--vcenter_name VCENTER_NAME
102+
Vcenter name
103+
--vcenter_username VCENTER_USERNAME
104+
Vcenter username
105+
--vcenter_password VCENTER_PASSWORD
106+
Vcenter password
107+
--vcenter_port VCENTER_PORT
108+
Vcenter port
109+
--protection_plan_name PROTECTION_PLAN_NAME
110+
Protection plan name
111+
--querystring QUERYSTRING
112+
Query string to create the VM intelligent group
113+
--vip_group_name VIP_GROUP_NAME
114+
VM intelligent group name
115+
--restore_vmname_prefix RESTORE_VMNAME_PREFIX
116+
Restore VM name prefix
117+
```
118+
Execution flow of group VM backup and restore script:
119+
- Add vcenter to NBU master
120+
- Discovery of Vcenter
121+
- Create protection plan
122+
- Create intelligent VM group based on the querystring
123+
- Subscribe VM group to protection plan
124+
- Perform immediate backup
125+
- Verify the status of jobs
126+
- Perform bulk restore
127+
- Perform the cleanup(e.g. remove bulk instant access VMs, subscription, protection plan, VM group and vcenter)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
## The library contain functions related to backup functionality.
2+
3+
## The script can be run with Python 3.5 or higher version.
4+
5+
## The script requires 'requests' library to make the API calls. The library can be installed using the command: pip install requests.
6+
7+
import common as common
8+
9+
headers = {"Content-Type" : "application/vnd.netbackup+json;version=4.0"}
10+
11+
# Perform single VM backup
12+
def perform_backup(baseurl, token, protection_plan_id, asset_id, is_vm_group = 0):
13+
headers.update({'Authorization': token})
14+
url = baseurl + "servicecatalog/slos/" + protection_plan_id + "/backup-now"
15+
selection_type = "ASSETGROUP" if is_vm_group else "ASSET"
16+
payload = {"data": {"type": "backupNowRequest", "attributes": {"selectionType": selection_type, "selectionId": asset_id}}}
17+
status_code, response_text = common.rest_request('POST', url, headers, data=payload)
18+
common.validate_response(status_code, 202, response_text)
19+
backup_job_id = response_text['data'][0]['id']
20+
print(f"Started backup for asset:[{asset_id}] and backup id is:[{backup_job_id}]")
21+
return backup_job_id
22+
23+
# Get protection and catalog backup id
24+
def get_backup_job_id(baseurl, token, backup_job_id, protection_plan_name):
25+
print("Find the protection plan and catalog backup job IDs")
26+
protection_backup_job_id = ''
27+
catalog_backup_job_id = ''
28+
headers.update({'Authorization': token})
29+
url = baseurl + "admin/jobs/?filter=jobId gt " + str(backup_job_id)
30+
status_code, response_text = common.rest_request('GET', url, headers)
31+
common.validate_response(status_code, 200, response_text)
32+
33+
for jobdata in response_text['data']:
34+
job_id = str(jobdata['id']).strip()
35+
parent_job_id = str(jobdata['attributes']['parentJobId']).strip()
36+
if jobdata['attributes']['policyName'].startswith(protection_plan_name) and job_id == parent_job_id:
37+
protection_backup_job_id = parent_job_id
38+
print(f"Protection backup Job Id:[{protection_backup_job_id}]")
39+
40+
if jobdata['attributes']['policyName'].startswith('NBU_Catalog_Default') and job_id == parent_job_id:
41+
catalog_backup_job_id = parent_job_id
42+
print(f"NBU Catalog backup Job Id:[{catalog_backup_job_id}]")
43+
return protection_backup_job_id, catalog_backup_job_id

0 commit comments

Comments
 (0)