«

»

Jan 19

Automate Backup of the VCSA to the vMA

Recently I wrote a post on how to Target the vMA for VCSA Backups. This post takes things a step further and automates the backup of the VCSA using a shell script, the vCenter Appliance REST API, and cron on the vMA.

In the vSphere 6.5 Documentation there is a Bash Example of Backing Up the vCenter Server Instance so I did not have to re-invent the wheel. I made a few slight modifications to the example script to use SCP and separate VCSA instance backups into different folders.

My vcbu.sh script can be found here: https://github.com/herseyc/VMware/blob/master/vcbu.sh

To backup the VCSA to the vMA copy the vcbu.sh script to the vMA. Edit the variables in the script to specify the vCenter Server Instance (VC_ADDRESS), vCenter User(VC_USER)/Password(VC_PASSWORD), vMA address (SCP_ADDRESS), the vMA User(SCP_USER)/Password(SCP_PASSWORD), and the backup folder location (BACKUP_FOLDER).

Set the permissions so the script can be executed by vi-admin – chmod +x vcbu.sh. Once configured, vcbu.sh can be executed to test. If successful a VCSA backup will be taken and stored in the /BACKUP_FOLDER/VC_IPADDRESS/TIME.

To execute the script on a schedule, in this case daily, crontab is used. To configure crontab on the vMA edit crontab for vi-admin, sudo crontab -u vi-admin -e. Insert the following line to take a backup of the VCSA everyday at 1 minute after midnight:
1 0 * * * /workspace/scripts/vcbu.sh
Use :wq! to save the crontab file.

If you want to schedule backups of the VCSA more often check out this crontab quick reference for guidance on how to create crontab entries for different schedules.

The crontab can be validated by listing out vi-admin’s crontab using sudo crontab -u vi-admin -l.

When cron executes the vcbu.sh based on the configured schedule it logs the event here /var/log/cron.

When the vcbu.sh script is executed by cron a backup.log is generated by the script which is stored in vi-admin’s home directory (/home/vi-admin/backup.log). The backup files are stored in the configured BACKUP_FOLDER in a sub-directory created for the VC_ADDRESS. Each set of VCSA backup files are stored in their own directory which is the TIME the backup script was executed.

The following command can be used to clean up backups which are older than 7 days.
find /workspace/backups/vcenter/*/* -type d -ctime +7 -exec rm -rf {} +
This could be added to the script to automatically clean up backups older than 7 days when ever the script is executed.
find /$BACKUP_FOLDER/$VC_ADDRESS/* -type d -ctime +7 -exec rm -rf {} +
To keep fewer, or more, days just change the +7 to however many days back you want to keep.

Pretty simple process to provide an automated backup of the VCSA using the vMA.

About the author

vHersey

Hersey Cartwright is an IT professional with extensive experience designing, implementing, managing, and supporting technologies that improve business processes. Hersey is Solutions Architect for SimpliVity covering Virginia, Washington DC, and Maryland. He holds the VMware Certified Design Expert (VCDX-DV #128) certification. Hersey actively participates in the VMware community and was awarded the VMware vExpert title in 2016, 2015, 2014, 2013, and 2012. He enjoys working with, teaching, and writing about virtualization and other data center technologies. Follow Hersey on Twitter @herseyc

4 comments

Skip to comment form

  1. Huda

    Hi Brian,

    Thanks for the amazing article !

    I’m running vSphere 6.0 on vCenter 6.5.

    I want to Automate the VCSA’s backup, but REST API doesn’t seem to be installed in vSphere 6.0, so running your Script gives an error

    I have been looking for an approach for automation, and the only article I found was the following:

    http://www.vmwarebits.com/vcenterbackup

    which suggests using REST API from vRealize Orchestrator, connect to a REST host and create a VCSA backup workflow to automate the backup operation..

    Please advise on the best backup automation approach

    Thanks

    1. vHersey

      Huda,

      Thanks for stopping by.

      The REST API is new to vCenter 6.5 and this process is specifically for the vCenter Server Appliance – not vCenter Server running on Windows Server. If you open a browser to https://IPofVCSA/apiexplorer/ to navigate the API. IF this results in an error – verify you are running the VCSA 6.5. Earlier versions do not have the API exposed.

      Hersey

  2. Jason

    Hi.

    I’m pretty new at this (Linux and VMware). I’m trying to automate VSCA backups and your way seemed to be the best.
    I’m stuck at the first part (copying the script). I’ve tried mounting a SMB share using the mount command but was unsucessful.
    Anyway, any help would be appreciated.

    Thank you!

    1. vHersey

      Jason,

      Thanks for stopping by.

      The easiest way is to use wget from the vMA.

      wget https://raw.githubusercontent.com/herseyc/VMware/master/vcbu.sh

      Once you copy the script to the vMA you will need to set it to be executable.

      chmod +x vcbu.sh

      Edit the script using vi to set you variables and then you should be good to go.

      Hersey

Leave a Reply to vHersey Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

2 + fourteen =