Today I was busy trying to find out a way to schedule VCB backups with Commvault with just 1 schedule without having to rewrite your script every time someone creates a VM. The standard procedure is to create a subclient for every VM which is very human error prone. I stumbled upon a blog on RTFM about backing up all running VM’s, the actual script was done by Andrew Neilson, thanks! Of which the following was the most important part:
for /f “tokens=2 delims=:” %%i in (‘vcbvmname -h virtualcenter.rtfm-ed.co.uk -u administrator -p password -s Powerstate:on ^| find “name:”‘) do @rd /s /q “D:\Backups\All\%%i” &vcbmounter -h virtualcenter.rtfm-ed.co.uk -u administrator -p password -a name:”%%i” -r “D:\Backups\All\%%i” -t fullvm > “D:\Backups\All\%%i.log”
In other words: the second word of the outcome of the vcbvmname command will be parsed into the vcbmounter command. You could use this line and mix it with the standard script that Commvault uses:
for /f “tokens=2 delims=:” %%i in (‘vcbvmname -h <virtualcenterserver> -u <user> -p <password> -s Powerstate:on ^| find “name:”‘) do cscript pre-command.wsf “c:\program files\vmware\vmware consolidated backup framework\” %%i fullvm
Kevin Cheramie says
Here is a bash script I use to hot backup the VM’s on my ESX servers to NFS storage. This script takes certain things for granted, to an extent. Some things have to be in place before this script will work properly. I will attempt to list these things here:
1) An account used only for backup administration should be created. I don’t recommend using the root account. A user such as “vmbkupadmin” with an associated password and appropriate permissions on each VM should be created. This information will then be added to the file /etc/vmware/backuptools.conf, for which the following lines must be changed:
VCHOST=ESX-SERVER-HOST-NAME
USERNAME=vmbkupadmin
PASSWORD=SECURE-PASSWORD
Hopefully you can tell where you need to change information to match your environment.
Next, all my NFS shares have the name “datastore-bkup”, so my script looks for that and builds the repository directory off that information.
If you have some basic bash scripting skills, you are probably doing better than me and should be able to bend this to your will.
#!/bin/sh
# Kevin Cheramie 03-13-2008
set -u
set -e
##################
# Trap premature exit conditions
trap badexit 1 2 3 6
badexit ()
{
printf “Premature exit, caught signal… exiting: `date` \n —\n” | tee -a /var/log/vmhotbackup.log
exit 1
}
##################
# Establish a start timestamp, and later, an end timestamp.
#
printf “VM hot backups begin: `date` \n” | tee -a /var/log/vmhotbackup.log
##################
# Construct the datastore name as it may be different from host to host
#
printf “\nDetermining datastore location information…\n”
DATASTORE=`find /vmfs/volumes/ -name datastore*`
HOSTNAME=`hostname`
printf “Datastore info: ${DATASTORE}\n”
##################
# If the datastore does not exist, the script will exit. Go create it, then try again.
#
if [ -z “$DATASTORE” ]
then
printf “\n$0: ERROR: Please ensure that a valid NFS datastore exists.\n”
printf “Exiting…\n”
exit 1
fi
printf “\nDatastore does exist. Continuing…\n”
##################
# If the datastore exists, but the host does not have it’s own repository folder, we will make one.
#
if [ ! -d ${DATASTORE}/${HOSTNAME} ]
then
printf “\n$0: ERROR: Datastore repository does not exist – Creating…\n”
mkdir ${DATASTORE}/${HOSTNAME}
printf “Done.\n”
else
printf “\nDatastore repository ${DATASTORE}/${HOSTNAME} does exist.\n”
fi
##################
# exit script if /etc/vmware/backuptools.conf does not contain username vmbkupadmin
#
printf “\nVerifying /etc/backuptools.conf configuration…\n”
grep vmbkupadmin /etc/vmware/backuptools.conf > /dev/null 2>&1 || { printf “$0: ERROR: /etc/backuptools.conf is misconfigured. Repair and try again
.\n”; exit 1; }
printf “/etc/backuptools.conf is configured properly…\n”
##################
# Get a list of all VM’s and feed it into an array
#
printf “\nRetreiving list of valid VM guests…\n”
i=0
for vmguest in `vcbVmName -h $HOSTNAME -s any: | grep name | awk -F: ‘{print $2}’`
do
ARRAY[$i]=$vmguest
# printf “${ARRAY[i]} is in position $i of the array.\n”
i=`expr $i + 1`
done
##################
# Determine the number of elements in the array
#
VMTOTAL=${#ARRAY[@]}
# echo array’s content
#echo ${ARRAY[@]}
printf “\nTotal elements in the array=$VMTOTAL\n”
##################
# Begin the back up of guest OS’s from the derived list
# the form of the command is as follows:
# vcbMounter -a name:GUEST_VM_NAME -r /PATH-TO-NFS-DATASTORE/ESX-HOSTNAME/GUEST_VM_NAME
#
# We will work within a loop to walk through the derived list one VM at a time.
# For the record, this will create a hot backup of each VM by causing each to momentarily quiesce,
# create a recoverable snapshot, export that snapshot and all associated VM configuration files
# to an NFS repository, and remove the snapshot. Running VM operations are only temporarily
# interrupted. Users should not notice a difference.
#
x=0
while [ $x -lt “$VMTOTAL” ]
do
## printf “${ARRAY[x]} is in position $x of the array.\n”
printf “\nBegin back up of ${ARRAY[x]} at `date`\n”
/usr/sbin/vcbMounter -a name:${ARRAY[x]} -r ${DATASTORE}/${HOSTNAME}/${ARRAY[x]}
printf “\nEnd backup of ${ARRAY[x]} at `date`\n”
x=`expr $x + 1`
done
##################
# End script
printf “\nAll VM guests have been backed up.\n\nEnd timestamp: `date` \n —\n” | tee -a /var/log/vmhotbackup.log
Duncan Epping says
thanks for sharing!
Jeff d says
I’ve been testing this script and it’s working well but I get a “The system cannot find the specified file” printed to the screen once for every VM that it backed up.
tdubb says
I am getting an error saying
“2 was expected at this time”
Mads says
I to get the error “2 was unexpected at this time”.
Any suggestions?
Jose Hernandez says
The script did not delete the snapshot reference in the .vmx file.
If delete this, work fine.