Skip navigation
VMware

This Question is Answered (go to answer)

2,886 Views 14 Replies Last post: May 24, 2007 1:16 PM by snowmizer RSS
snowmizer Hot Shot 271 posts since
Jun 8, 2005
Currently Being Moderated

May 14, 2007 7:17 AM

Remove snapshots

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.

conyards Expert 505 posts since
Sep 22, 2006
Currently Being Moderated
1. May 14, 2007 7:20 AM in response to: snowmizer
Re: Remove snapshots

As an aside, if you end up with 'consolidate helper' as part of the VCB backup proocess, the VM requires to be powered off for the snapshot removal to be sucessful.

oreeh Guru 10,046 posts since
Nov 30, 2005
Currently Being Moderated
2. May 14, 2007 7:23 AM in response to: snowmizer
Re: Remove snapshots

After a system (or backtick) operation $? contains the result code.

 

Example:

Re: Remove snapshots

my @args = ("bla","option");

            system(@args);

 

            my $result = $?;

            if ($result ne "1") {

                ...

/code

oreeh Guru 10,046 posts since
Nov 30, 2005
Currently Being Moderated
4. May 14, 2007 7:46 AM in response to: snowmizer
Re: Remove snapshots

this is not Perl but a shell script

 

to evaluate the result code in a shell script you need to do the following

 

Re: Remove snapshots

/usr/bin/vmware-cmd "$vm" hassnapshot

resultcode=$?

if \[ $resultcode -eq 1 ]; then

  echo "$vm" has snapshot

fi

/code

oreeh Guru 10,046 posts since
Nov 30, 2005
Currently Being Moderated
6. May 14, 2007 8:01 AM in response to: snowmizer
Re: Remove snapshots

Found the error:

vmware-cmd doesn't return a result code instead it prints the result to stdout.

So you have to do something like this

 

$result=`vmware-cmd .....`

if \[ $result eq "hassnapshot() = 1" ]; .....

dsanders Expert 516 posts since
Mar 2, 2005
Currently Being Moderated
7. May 14, 2007 12:28 PM in response to: snowmizer
Re: Remove snapshots

Here is the code I wrote to do what you want:

Re: Remove snapshots

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

/code

 

Basically add the '-q' switch and vmware-cmd will just return 1 if the snapshot is present.

dsanders Expert 516 posts since
Mar 2, 2005
Currently Being Moderated
8. May 14, 2007 1:13 PM in response to: snowmizer
Re: Remove snapshots

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

jmcar Novice 18 posts since
Jul 24, 2006
Currently Being Moderated
10. May 21, 2007 6:02 AM in response to: snowmizer
Re: Remove snapshots

This is exactly the script I am after but being a bit green on this type of scripting I am having a few troubles.

 

Would it be possible to post your completed script here for me to see?

jmcar Novice 18 posts since
Jul 24, 2006
Currently Being Moderated
12. May 21, 2007 6:14 AM in response to: snowmizer
Re: Remove snapshots

You're a star - thanks.

 

I need to pursue this until I can get VCB and Netbackup to co-operate on removing snapshots

dsanders Expert 516 posts since
Mar 2, 2005
Currently Being Moderated
13. May 24, 2007 12:46 PM in response to: jmcar
Re: Remove snapshots

I discovered that this script is broken if you have any spaces in the path for the VMX.  Please see the updated code

Re: Remove snapshots

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

/code

 

For an explaination see here:

http://www.vmware.com/community/message.jspa?messageID=653154#653154

Bookmarked By (0)

Share This Page

Communities