Virtualization

VMWare Virtual Machine Backups Part 2

In an earlier post, we discussed how you can utilize Scheduled tasks in vCenter Server to clone machines at regular intervals and then using PowerCLI to delete existing backups so that at the next scheduled interval the scheduled task can run without issue to backup the same virtual machines.  We want to take this a little bit further as you can see there would be a period of time that we would be running without our backup as the powercli script has already deleted the backup so the clone can take place again.  Using a CRONTAB job and a Shell script containing a TAR process we can accomplish this by tarring up existing backups to storage before the script deletes the backup machines before the scheduled task runs.  Also, utilizing Microsoft Windows services for UNIX we can mount an NFS store in our ESXi environment to use as a backup target for our script.

There are a lot of good articles out there on how to set up WSU so we won’t reinvent the wheel here, but one of the best and detailed blogs we found on how to set this up is located here.  After you have your datastore mounted in your vSphere environment, then we can utilize this as the backup target found in our shell script.

Some of the ideas and information for the shell script are taken from the ghettoVCB.sh script that is found on the VMware site here: https://communities.vmware.com/docs/DOC-8760

However, we wanted to utilize something that would take advantage of what we already had in place in our blog article mentioned at the outset with the scheduled tasks since these have been rock solid since we have implemented them and are a core part of the vSphere vCenter architecture.  Also, with the busybox shell in ESXi there are some limitations and size constraints that can cause difficulties in solely using the script.  So our plan of action to integrate with what we already have in place:

  • Create a shell script to TAR up our backup VM created by the scheduled CLONE task and target the backup to our NFS share created using WSU
  • Use a CRONTAB file to kick this process off automatically at a set time to copy the recently cloned VMs to our backup target

Below is our shell script

# What to backup.
server1=”/vmfs/volumes/Backups/server1_backup”
server2=”/vmfs/volumes/Backups/server2_backup”
server3=”/vmfs/volumes/Backups/server3_backup”
server4=”/vmfs/volumes/Backups/server4_backup”
server5=”/vmfs/volumes/Backups/server5_backup”
server6=”/vmfs/volumes/Backups/server6_backup”
server7=”/vmfs/volumes/Backups/server7_backup”
server8=”/vmfs/volumes/Backups/server8_backup”
server9=”/vmfs/volumes/Backups/server9_backup”
server10=”/vmfs/volumes/Backups/server10_backup”
server11=”/vmfs/volumes/Backups/server11_backup”
server12=”/vmfs/volumes/Backups/server12_backup”
server13=”/vmfs/volumes/Backups/server13_backup”
server14=”/vmfs/volumes/Backups/server14_backup”
server15=”/vmfs/volumes/Backups/server15_backup”
server16=”/vmfs/volumes/Backups/server16_backup”

# Where to backup to.
dest=”/vmfs/volumes/VMBackup”

# Create archive filename.
day=$(date +%A+%y%m%d)
hostname=$(hostname -s)
archive_server1=”server1_backup-$day.tar.gz”
archive_server2=”server2_backup-$day.tar.gz”
archive_server3=”server3_backup-$day.tar.gz”
archive_server4=”server4_backup-$day.tar.gz”
archive_server5=”server5_backup-$day.tar.gz”
archive_server6=”server6_backup-$day.tar.gz”
archive_server7=”server7_backup-$day.tar.gz”
archive_server8=”server8_backup-$day.tar.gz”
archive_server9=”server9_backup-$day.tar.gz”
archive_server10=”server10_backup-$day.tar.gz”
archive_server11=”server11_backup-$day.tar.gz”
archive_server12=”server12_backup-$day.tar.gz”
archive_server13=”server13_backup-$day.tar.gz”
archive_server14=”server14_backup-$day.tar.gz”
archive_server15=”server15_backup-$day.tar.gz”
archive_server16=”server16_backup-$day.tar.gz”

# Print start status message.
echo “Backing up $server1 to $dest/$archive_server1”
date
echo# Backup the files using tar.
tar czvf $dest/$archive_server1 $server1
tar czvf $dest/$archive_server2 $server2
tar czvf $dest/$archive_server3 $server3
tar czvf $dest/$archive_server4 $server4
tar czvf $dest/$archive_server5 $server5
tar czvf $dest/$archive_server6 $server6
tar czvf $dest/$archive_server7 $server7
tar czvf $dest/$archive_server8 $server8
tar czvf $dest/$archive_server9 $server9
tar czvf $dest/$archive_server10 $server10
tar czvf $dest/$archive_server11 $server11
tar czvf $dest/$archive_server12 $server12
tar czvf $dest/$archive_server13 $server13
tar czvf $dest/$archive_server14 $server14
tar czvf $dest/$archive_server15 $server15
tar czvf $dest/$archive_server16 $server16# Print end status message.
echo
echo “Backup finished”
date# Long listing of files in $dest to check file sizes.
ls -lh $dest

 

  • Note – Be sure to change the mode of the script you create as by default it is not set with execute permissions.  To do this, use the chmod +x “yourscript.sh” command replacing the “yourscript.sh” with the actual name of your script without the quotes.

In the shell script, the $dest variable is our NFS datastore that resides on a Windows 2003 box with a storage array attached.  The script is a crude example of what you can do with a shell script pointing to an NFS share.  You can back your VMs up using the TAR process and they are copied at the same time to the share.  Now if we have a VM that is huge and you receive errors trying to TAR a certain VM up, you can strictly use a cp command to copy the entire folder out to the NFS share if you have the space.  Out of the 15 VMs or so that I wanted to backup to our NFS share, only one was large enough that TAR complained about inside of busybox.

For the CRONTAB file, follow the instructions that are given in the VMWare communities article about ghettoVCB.  They show you how to set this up in both ESX as well as ESXi.

Final Thoughts

With virtual infrastructures becoming more and more common and integral in most infrastructures of enterprise environments, backing up your VMs is crucial.  There are a lot of ways to accomplish this and there are certainly some really nice 3rd party tools out there to do VM backups including vRanger and Veeam.  However, if budgets are an issue or you simply have a lot of the pieces infrastructure wise already in place, you can utilize some good old linux shell scripting and capabilities already built into VMware to accomplish your backups as well.

Back to top button