|
| 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 | + |
| 21 | +from samples.vsphere.common import sample_cli |
| 22 | +from samples.vsphere.common import sample_util |
| 23 | +from samples.vsphere.common import vapiconnect |
| 24 | +from tabulate import tabulate |
| 25 | + |
| 26 | +from com.vmware.appliance.recovery.backup_client import Schedules |
| 27 | + |
| 28 | + |
| 29 | +class BackupSchedule(object): |
| 30 | + """ |
| 31 | + Demonstrates backup schedule operations |
| 32 | +
|
| 33 | + Prerequisites: |
| 34 | + - vCenter |
| 35 | + - Backup server (ftp/ftps/http/https/scp) |
| 36 | + """ |
| 37 | + |
| 38 | + def __init__(self): |
| 39 | + self.stub_config = None |
| 40 | + |
| 41 | + # Scheudle backup to run on weekdays at 10:30 pm |
| 42 | + self.days = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"] |
| 43 | + self.hour = 22 |
| 44 | + self.minute = 30 |
| 45 | + |
| 46 | + # Retain last 30 backups |
| 47 | + self.max_count = 30 |
| 48 | + |
| 49 | + self._schedule_id = 'test_schedule' |
| 50 | + |
| 51 | + |
| 52 | + def setup(self): |
| 53 | + parser = sample_cli.build_arg_parser() |
| 54 | + |
| 55 | + parser.add_argument('-location', '--location', |
| 56 | + required=True, |
| 57 | + action='store', |
| 58 | + help='URL of the backup location') |
| 59 | + parser.add_argument('--location_user', |
| 60 | + required=True, |
| 61 | + action='store', |
| 62 | + help='Username for the given location') |
| 63 | + parser.add_argument('--location_password', |
| 64 | + required=True, |
| 65 | + action='store', |
| 66 | + help='Password for the given location') |
| 67 | + |
| 68 | + args = sample_util.process_cli_args(parser.parse_args()) |
| 69 | + self.location = args.location |
| 70 | + self.location_user = args.location_user |
| 71 | + self.location_password = args.location_password |
| 72 | + |
| 73 | + # Connect to vAPI services |
| 74 | + self.stub_config = vapiconnect.connect( |
| 75 | + host=args.server, |
| 76 | + user=args.username, |
| 77 | + pwd=args.password, |
| 78 | + skip_verification=args.skipverification) |
| 79 | + |
| 80 | + self.schedule_client = Schedules(self.stub_config) |
| 81 | + |
| 82 | + def run(self): |
| 83 | + # Create a backup schedule |
| 84 | + self.create_schedule() |
| 85 | + |
| 86 | + # Update the backup schedule to take backup only on weekends |
| 87 | + self.days = ["SATURDAY", "SUNDAY"] |
| 88 | + self.update_schedule() |
| 89 | + |
| 90 | + # Get the updated backup schedule |
| 91 | + self.get_schedule() |
| 92 | + |
| 93 | + # Run backup operation using the scheduled configuration |
| 94 | + self.run_backup() |
| 95 | + |
| 96 | + # Delete the backup schedule |
| 97 | + self.delete_schedule() |
| 98 | + |
| 99 | + def create_schedule(self): |
| 100 | + retention_info = Schedules.RetentionInfo(self.max_count) |
| 101 | + recurrence_info = Schedules.RecurrenceInfo( |
| 102 | + days=self.days, |
| 103 | + hour=self.hour, |
| 104 | + minute=self.minute) |
| 105 | + create_spec = Schedules.CreateSpec( |
| 106 | + location=self.location, |
| 107 | + location_user=self.location_user, |
| 108 | + location_password=self.location_password, |
| 109 | + recurrence_info=recurrence_info, |
| 110 | + retention_info=retention_info) |
| 111 | + |
| 112 | + self.schedule_client.create(self._schedule_id, create_spec) |
| 113 | + |
| 114 | + def update_schedule(self): |
| 115 | + retention_info = Schedules.RetentionInfo(self.max_count) |
| 116 | + recurrence_info = Schedules.RecurrenceInfo( |
| 117 | + days=self.days, |
| 118 | + hour=self.hour, |
| 119 | + minute=self.minute) |
| 120 | + update_spec = Schedules.UpdateSpec( |
| 121 | + location=self.location, |
| 122 | + location_user=self.location_user, |
| 123 | + location_password=self.location_password, |
| 124 | + recurrence_info=recurrence_info, |
| 125 | + retention_info=retention_info) |
| 126 | + |
| 127 | + self.schedule_client.update(self._schedule_id, update_spec) |
| 128 | + |
| 129 | + def get_schedule(self): |
| 130 | + self.schedule_client = Schedules(self.stub_config) |
| 131 | + schedule_spec = self.schedule_client.get(self._schedule_id) |
| 132 | + |
| 133 | + recurrence_info = schedule_spec.recurrence_info |
| 134 | + retention_info = schedule_spec.retention_info |
| 135 | + |
| 136 | + table = [] |
| 137 | + data = [self._schedule_id, |
| 138 | + "{}:{}".format(recurrence_info.hour, recurrence_info.minute), |
| 139 | + " ".join(recurrence_info.days), |
| 140 | + retention_info.max_count] |
| 141 | + table.append(data) |
| 142 | + headers = ["Schedule ID", "Time", "Days", "Retention"] |
| 143 | + print(tabulate(table, headers)) |
| 144 | + |
| 145 | + def run_backup(self): |
| 146 | + schedule_spec = self.schedule_client.run(self._schedule_id) |
| 147 | + |
| 148 | + def delete_schedule(self): |
| 149 | + self.schedule_client.delete(self._schedule_id) |
| 150 | + |
| 151 | + |
| 152 | +def main(): |
| 153 | + schedule = BackupSchedule() |
| 154 | + schedule.setup() |
| 155 | + schedule.run() |
| 156 | + |
| 157 | +if __name__ == '__main__': |
| 158 | + main() |
0 commit comments