vHerseyVMware

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.

6 thoughts on “Automate Backup of the VCSA to the vMA

  • 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

    Reply
    • 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

      Reply
  • 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!

    Reply
    • 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

      Reply
  • Chad

    Excellent post! I wanted to share that I was able to create an email notification if the backup job fails by checking if the destination folder contained files or not. If the folder does not exist or there are zero files in it, then the “failure” email is sent. Its not fool proof, but its a start. I’m sure someone can build on it and add some better logic.

    if [ $(ls -1A /$BACKUP_FOLDER/$VC_ADDRESS/$TIME | wc -l) -eq 0 ] ; then echo “VCSA backup failed, please investigate. No files were found in /$BACKUP_FOLDER/$VC_ADDRESS/$TIME/. Review the backup.log attached for further details. If necessary, SSH into the VCSA and review /var/log/vmware/applmgmt/backup.log.” | mail -s “VMware VCSA Backup Failure – $VC_ADDRESS” -r “vMA@domain.com” -a “backup.log” -S replyto=”vMA@domain.com ” team.dl@domain.com; fi

    Reply
  • Joel Cottrell

    Thanks for that info Chad! I’ve been looking for a way to allow the script to email me either when it’s successful or failed. Would you also be able to assist in setting up this script to email on successful backups?

    Reply

Leave a Reply to Joel Cottrell Cancel reply

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

3 + 1 =