How to

Automating File-Based Backup of vCenter Server Appliance with Python

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, my teammate, 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:

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:

Example: Help Output from backup_job_crud.py

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 [email protected]
server-password VMware1!
location ftp://ftp01.corp.local/
location-user backup
location-password VMware1!

Here’s an example of running the sample script:

Example: Performing a VCSA backup

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:

Example: Get Backup Job Status

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.

Example: backup_schedule.py Help Output

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:

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:

  1. Create a new schedule
  2. Update the schedule to run on Saturday and Sunday
  3. Get the current status of the schedule
  4. Run the schedule backup job
  5. Delete the schedule

Here’s an example of running the sample:

Example: backup_schedule.py Usage

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

Let us know in the comments how you’re automating your VCSA backups!