I a newbie when it comes to writing perl scripts on ESX. I have been having a problem with snapshots from VCB backups not being deleted so I would like to use vmware-cmd to find if a machine has a snapshot and then delete the snapshot if it exists. I have the looping structure to loop through the VMs and am using the "hassnapshot" parameter with vmware-cmd.
My problem is this, how do I check the return code from the hassnapshot command to see if it is a 1?
Thanks.
Ok so I've modified my script as follows:
#!/bin/bash
echo "Getting snapshots of VM Guests"
for vm in $(/usr/bin/vmware-cmd -l)
do
#echo "$vm"
/usr/bin/vmware-cmd "$vm" hassnapshot
if ($? eq "1") then
echo "$vm" has snapshot
fi
done
When my script gets to the "if" statement I get a "command not found" message.
Ok so I've made these changes but the commands inside my if statement aren't being executed so I checked the value of the resultcode variable. The value in this variable is 0 so it looks like it's not bringing back the result from the hassnapshot value. I see the following output:
hassnapshot() = 1
hassnapshot() =
hassnapshot() =
I need to retrieve the value of hassnapshot.
Thanks.
Here is the code I wrote to do what you want:
for vm in `vmware-cmd -l`
do
name=`vmware-cmd "$vm" getconfig displayname -q`
snap=`vmware-cmd "$vm" hassnapshot -q`
if \[ "$snap" == "1" ]
then
echo "Removing snapshots from $name"
remove=`vmware-cmd "$vm" removesnapshots -q`
if \[ "$remove" == "1" ]
then
echo "Snapshots successfully removed from $name"
else
echo "*** Unable to remove snapshots from $name ***"
fi
fi
done
Basically add the '-q' switch and vmware-cmd will just return 1 if the snapshot is present.
I think your problem may be the quotes
Re: Remove snapshotsecho "$vm" has snapshot /code
try this instead:
Re: Remove snapshotsecho "$vm has snapshot" /code
#!/bin/bash
echo "Getting snapshots of VM Guests"
cat /home/myuser/VMIPList.txt |
while read vmip
do vmname=`nslookup $vmip | grep name | awk -F'= ' '\{print $2)' | awk -F. '{print $1}'`
for vm in `/usr/bin/vmware-cmd -l`
do
if \[ "$vm" == "$vmname" ]
then
name=`/usr/bin/vmware-cmd "$vm" getconfig displayname -q`
snap=`/usr/bin/vmware-cmd "$vm" hassnapshot -q`
if \[ "$snap" == "1" ]
then
echo "Removing snapshots from $name"
remove=`/usr/bin/vmware-cmd "$vm" removesnapshots -q`
if \[ "$remove" == "1" ]
then
echo "Snapshots successfully removed from $name"
else
echo "*** Unable to remove snapshots from $name ***"
fi
else
echo "No snapshots found for $name"
fi
fi
done
I'm currently working on retrofiting this code into my backup script. Hope this helps.
I discovered that this script is broken if you have any spaces in the path for the VMX. Please see the updated code
IFS='
'
for vm in `vmware-cmd -l`
do
name=`vmware-cmd "$vm" getconfig displayname -q`
snap=`vmware-cmd "$vm" hassnapshot -q`
if \[ "$snap" == "1" ]
then
echo "Removing snapshots from $name"
remove=`vmware-cmd "$vm" removesnapshots -q`
if \[ "$remove" == "1" ]
then
echo "Snapshots successfully removed from $name"
else
echo "*** Unable to remove snapshots from $name ***"
fi
fi
done
For an explaination see here:
http://www.vmware.com/community/message.jspa?messageID=653154#653154