Python - Using the vSphere Automation SDK with VMware Cloud on AWS
Did you know the vCenter Server Appliance (VCSA) has file-based backup options?
This ability was actually released in vSphere 6.5. However, there was one feature in particular that was missing: a scheduler. I’m happy to say that as part of vSphere 6.7, the VCSA received a backup scheduler!
Recently, Emad Younis released a couple cool walkthroughs to the vSphere Central site to manage file-based backup and restore actions. Under the covers, both of these actions are served up by vSphere’s RESTful APIs and therefore we can use the vSphere Automation SDK for Python to automate these actions!
Let’s see a couple examples of this in action.
Create a File-Based Backup
The vSphere Automation SDK for Python comes complete with an existing sample to easily create a file-based backup of the VCSA. If you don’t have the SDK on your local system already, use the following blog post to achieve that: Getting Started with the vSphere Automation SDK for Python
The first step is to, within your terminal session, change over to the root of the SDK directory and update the PYTHONPATH variable. Example on MacOS:
cd GitHub/vsphere-automation-sdk-python/
export PYTHONPATH=${PWD}:$PYTHONPATH
Next, we’ll want to examine the following sample: ./samples/vsphere/backuprestore/backup_job_crud.py
To do that, we can either open it in the IDE of our choice or simply choose to view the help output by calling the script and adding the ‘-h’ parameter. Example:
python3 ./samples/vsphere/backuprestore/backup_job_crud.py -h
Based on the output, we can see this sample does more than just create backup jobs. Therefore, we’ll need to verify what parameters we are going to be using. This can be accomplished by viewing the API Explorer, which happens to be built into the VCSA and is available at: https://vcsa.fqdn/apiexplorer
From that point, we’ll want to browse to the Appliance API service and then the ‘appliance/recovery/backup/job’ section. Expanding that section, we can see a POST method with a description of ‘Initiate backup’. Expanding that method, we can see the parameter ‘request_body’ and all of the information we’ll need to enter. All of that information will need to be added to the parameters for the script.
For my environment, we’ll be using the following parameters and values: server | vcsa01.corp.local server-user | kruddy@corp.local server-password | VMware1! location | ftp://ftp01.corp.local/ location-user | backup location-password | VMware1!
Here’s an example of running the sample script:
python3 ./samples/vsphere/backuprestore/backup_job_crud.py --server vcsa01.corp.local --server-user 'kruddy@corp.local' --server-password $server_pass --skipverification --create-backup --location 'ftp://ftp01.corp.local/' --location-user 'backup' --location-password 'VMware1!' --backup-comment "VCSA Backup Job - Python SDK"
We can use that same sample script to keep up to date on the status of the backup job with either the ‘Get-Backup’ or ‘List-Backup’ parameters. Example:
python3 ./samples/vsphere/backuprestore/backup_job_crud.py --server vcsa01.corp.local --server-user 'kruddy@corp.local' --server-password $server_pass --skipverification --get-backup ‘20180801-011504-8217866’
Create a File-Based Backup Schedule
There’s also an existing sample resource to create our backup schedule! This is in the same directory as the prior sample, it’s named: backup_schedule.py If we examine the help, we can view what parameters will be necessary in this case.
If we refer back to the API Explorer and the parameter input, we’ll notice a couple inputs are missing. These are included as part of the init function on lines 41 through 49 of the sample code:
# Scheudle backup to run on weekdays at 10:30 pm
self.days = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY"]
self.hour = 22
self.minute = 30
# Retain last 30 backups
self.max_count = 30
self._schedule_id = 'test_schedule'
Before we run the sample, I should explain that this sample is going to perform the multiple methods that are available to manage the backup scheduler. This sample will perform the following actions:
- Create a new schedule
- Update the schedule to run on Saturday and Sunday
- Get the current status of the schedule
- Run the schedule backup job
- Delete the schedule
Here’s an example of running the sample:
python3 ./samples/vsphere/backuprestore/backup_schedule.py --server vcsa01.corp.local --username 'kruddy@corp.local' --password $server_pass --skipverification --location 'ftp://ftp01.corp.local/' --location_user backup --location_password 'VMware1!'
Summary
The ability to create file-based backups of your vCenter Server is a function that is only available to the VCSA. This function is made possible by a set of RESTful APIs which can be performed with the vSphere Automation SDK for Python! This blog post walked through a couple examples of creating a file-based backup job as well as managing the lifecycle of a scheduled file-based backup job. More information about VCSA file-based backup can be found on the vSphere Central site: vCenter Server Appliance 6.7 File-Based Backup