I've attached the script in the post as a file in my previous post (vmbackup.sh), new version which does a backup from all machines that match using grep -i (not case sensitive).Just a bit more explanation on the script:
So you can use grep regular expressions (change the line to grep -E -i for advanced regular expressions).
It does the grep on the output of the "vim-cmd /vmsvc/getallvms" command which gives an output like this:
Vmid Name File Guest OS Version Annotation
16 vmname1 \[Datastore-Local-1\] XYZSrv1/XYZSrv1.vmx winLonghorn64Guest vmx-07
32 vmname2 \[Datastore-Local-1\] Xconsole-1/Xconsole-1.vmx winXPProGuest vmx-07
This means that you can also backup all vm's on a specific datastore by using for example:
vmbackup.sh Datastore-Local-1
or all windows systems by using for example:
vmbackup.sh winLonghorn64Guest
It stores all VM ID's and one by one ($j is the VMID) performs the backup to the given server. It also uses the output of "vim-cmd /vmsvc/getallvms" to extract the name of the vm (remember $j is the VM-ID):
VMNAME=`vim-cmd /vmsvc/getallvms | grep -i "^$j " | awk '{ print $2 }'`
Based on the status (powerd on or off) it will shutdown the specific vm (vim-cmd /vmsvc/power.shutdown $j) and wait until the vm is shutdown (remember that the power.shutdown function will return immediately, this is the while ; do ... done). Be aware that this could be a deadlock as when the vmware guest does not shutdown the script will wait forever (working on this, adding a time-out).
The ALLFILES var will hold all files that belong to the vm guest (ALLFILES=`ls -1 $VMLOCATE`) and the for i in $ALLFILES will traverse through all the files (filename is $i) and ftp them to the given location.
If VMSTAT was on (e.g. vm guest was running) the script will start the vm again, if it was not running it will leave it like that :smileyhappy: as I guess there is a reason why it was not running.
If there are errors try and put some echo $variable in the script to find out if the correct names etc are displayed. The script is not tested extensively and things that could go wrong are for example spaces in the names of vm guests (I don't know if that is allowed anyway)
varialbes used are:
SERVNAME: The string that is matched against the getallvms command
VMID: Contains all VM ID's that matched the SERVNAME
VMNAME: Contains the name of the VM guest extracted frin getallvms
VMSTAT: Contains the power state (on or off), fetched from power.getstate
VMDATASTORE: contains the absolute path of the datastore, fetched from get.datastore command
VMLOCATE: contains the absolute path to the directory where the vm guest is located
I just noticed in the script that I execute power.getstate twice, the first one can be removed.
Alternative to use tar is given in the previous post. I guess the rest you guys can figure out yourselve :smileyhappy:
Have fun