Skip to content

Commit 6ce4b4a

Browse files
author
Kyle Ruddy
committed
Add backup_job_crud Sample Script
Adding a new sample resource to help manage backup management options for appliances. Uses the Job class from the com.vmware.appliance.recovery.backup_client module Methods Available: List Backup Jobs Get Backup Job Create Backup Job Cancel Backup Job
1 parent 6cf1026 commit 6ce4b4a

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2017. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
16+
__author__ = 'VMware, Inc.'
17+
__copyright__ = 'Copyright 2017 VMware, Inc. All rights reserved.'
18+
__vcenter_version__ = '6.7+'
19+
20+
import argparse
21+
from tabulate import tabulate
22+
from samples.vsphere.common import vapiconnect
23+
from com.vmware.appliance.recovery.backup_client import Job
24+
25+
26+
class BackupJob(object):
27+
"""
28+
Demonstrates backup job operations
29+
30+
Retrieves backup job details from vCenter and prints the data in
31+
tabular format
32+
33+
Prerequisites:
34+
- vCenter
35+
- Backup operation is performed on the vCenter either manually or
36+
by scheduled backups
37+
"""
38+
39+
def __init__(self):
40+
self.stub_config = None
41+
42+
def setup(self):
43+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
44+
45+
parser.add_argument('--server',
46+
required=True,
47+
action='store',
48+
help='FQDN of the VCSA')
49+
50+
parser.add_argument('--server-user',
51+
required=True,
52+
action='store',
53+
help='Username for the VCSA')
54+
55+
parser.add_argument('--server-password',
56+
required=True,
57+
action='store',
58+
help='Password for the VCSA')
59+
60+
parser.add_argument('-v', '--skipverification',
61+
required=False,
62+
action='store_true',
63+
help='Skip SSL Verification')
64+
65+
parser.add_argument('--location',
66+
required=False,
67+
action='store',
68+
help='URL of the backup location')
69+
70+
parser.add_argument('--location-user',
71+
required=False,
72+
action='store',
73+
help='Username for the given location')
74+
75+
parser.add_argument('--location-password',
76+
required=False,
77+
action='store',
78+
help='Password for the given location')
79+
80+
parser.add_argument('--backup-password',
81+
required=False,
82+
action='store',
83+
help='Password for the backup')
84+
85+
parser.add_argument('--backup-comment',
86+
required=False,
87+
action='store',
88+
help='Comment for the backup')
89+
90+
parser.add_argument('-lb', '--list-backup',
91+
required=False,
92+
action='store_true',
93+
help='Switch to list backup jobs')
94+
95+
parser.add_argument('-gb', '--get-backup',
96+
required=False,
97+
action='store',
98+
help='Backup Job ID to Get Information About')
99+
100+
parser.add_argument('-nb', '--create-backup',
101+
required=False,
102+
action='store_true',
103+
help='Switch to create backup job')
104+
105+
parser.add_argument('-cb', '--cancel-backup',
106+
required=False,
107+
action='store',
108+
help='Backup Job ID to Cancel Job')
109+
110+
args = parser.parse_args()
111+
112+
self.server = args.server
113+
self.server_user = args.server_user
114+
self.server_password = args.server_password
115+
self.location = args.location
116+
self.location_user = args.location_user
117+
self.location_password = args.location_password
118+
self.backup_password = args.backup_password
119+
self.backup_comment = args.backup_comment
120+
self.list_backup = args.list_backup
121+
self.get_backup = args.get_backup
122+
self.create_backup = args.create_backup
123+
self.cancel_backup = args.cancel_backup
124+
125+
# Connect to vAPI services
126+
self.stub_config = vapiconnect.connect(
127+
host=self.server,
128+
user=self.server_user,
129+
pwd=self.server_password,
130+
skip_verification="TRUE")
131+
132+
self.job_client = Job(self.stub_config)
133+
134+
def list_backup_job(self):
135+
job_list = self.job_client.list()
136+
self.print_output(job_list)
137+
138+
def get_backup_job(self):
139+
self.print_output(self.get_backup)
140+
141+
def create_backup_job(self):
142+
backup_type = (self.location.split(':')[0]).upper()
143+
piece = self.job_client.BackupRequest(
144+
parts = ["common"],
145+
backup_password = self.backup_password,
146+
location_type = backup_type,
147+
location = self.location,
148+
location_user = self.location_user,
149+
location_password = self.location_password,
150+
comment = self.backup_comment)
151+
152+
job_create = self.job_client.create(piece)
153+
self.print_output(job_create.id)
154+
155+
156+
def cancel_backup_job(self):
157+
job_cancel = self.job_client.cancel(self.cancel_backup)
158+
self.print_output(self.cancel_backup)
159+
160+
def print_output(self, job):
161+
table = []
162+
163+
if type(job) is list:
164+
for bkupjob in job:
165+
info = self.job_client.get(bkupjob)
166+
if info.end_time is None:
167+
duration = None
168+
else:
169+
duration = info.end_time - info.start_time
170+
row = [info.id,
171+
info.state.capitalize(),
172+
info.start_time.strftime("%b %d %Y %H:%M"),
173+
duration]
174+
table.append(row)
175+
else:
176+
info = self.job_client.get(job)
177+
if info.end_time is None:
178+
duration = None
179+
else:
180+
duration = info.end_time - info.start_time
181+
row = [info.id,
182+
info.state.capitalize(),
183+
info.start_time.strftime("%b %d %Y %H:%M"),
184+
duration]
185+
table.append(row)
186+
187+
headers = ["ID", "Status", "Start time", "Duration"]
188+
print(tabulate(table, headers))
189+
190+
def main():
191+
backup_job = BackupJob()
192+
backup_job.setup()
193+
if backup_job.list_backup:
194+
backup_job.list_backup_job()
195+
if backup_job.get_backup:
196+
backup_job.get_backup_job()
197+
if backup_job.create_backup:
198+
backup_job.create_backup_job()
199+
if backup_job.cancel_backup:
200+
backup_job.cancel_backup_job()
201+
202+
if __name__ == '__main__':
203+
main()

0 commit comments

Comments
 (0)