#! /bin/sh
ds=/vmfs/volumes/datastore1
bk=/vmfs/volumes/backup1
export name
for vmstr in `vim-cmd vmsvc/getallvms | grep -v Name | awk '{ print $1 "," $4 }'`
do
vmid=`echo $vmstr | awk -F',' '{ print $1 }'`
vmname=`echo $vmstr | awk -F',' '{ print $2 }' | awk -F'/' '{ print $1
}'`
name=BACKUP_`date +%Y%m%d%H%M%S`
vim-cmd vmsvc/snapshot.create $vmid $name > /dev/null
if [ -f $bk/$vmname.tgz ]
then
mv $bk/$vmname.tgz $bk/$vmname.old.tgz
fi
tar zcf $bk/$vmname.tgz $ds/$vmname
snapLevel=`vim-cmd vmsvc/snapshot.get $vmid | grep Name | grep -n $name | awk -F':' '{ print $1-1 }'`
vim-cmd vmsvc/snapshot.remove $vmid removeChildren $snapLevel > /dev/null
snapID=`vim-cmd vmsvc/snapshot.get $vmid | awk '( $2=="Name"
) { nm=$4 } ( nm==ENVIRON["name"] && $2=="Id"
) { print $4 }'`
vim-cmd vmsvc/snapshot.remove $vmid $snapID removeChildren > /dev/null
done |