NetApp Snapshots in ESX

UPDATE: I have re-written this script for enhancements, these can be found on the post titled NetApp Snapshots in ESX – Take 2

Many of you NetApp/ESX users should know by now that SnapManager for VI has been released. Many of you should also know that NetApp’s costs of licensing such tools can be fairly pricey. For those of us that can’t afford or justify the purchase of SMVI, I give to you a very handy script that I wrote….

This script will first create quiesced VMware ESX Snapshots, then connect to your NetApp FAS and kick off a snapshot creation.

There is one prerequisite and that is to enable password-less SSH access from your ESX host and into your filer. Please look up the post ‘SSH from an ESX host into NetApp OnTap for more information on how to do this. Another would be if you have multiple ESX servers, you need to enable password-less SSH access into those machines as well. Please look up the post ‘SSH from ESX to ESX‘ for more information on how to do this.

The script is very basic, I will include it below and go over each line and what they do.

#first we set which shell to use, adding -x to /bin/sh will enable verbose output

#!/bin/sh -x

# set path and date, this will be used to idenify your snapshots in case of failure

DATE=`date +%m.%d.%G.%H%M`

#now we will use a for-do-done loop to get the VM ID’s and create a snapshot.
#vmware-vim-cmd vmsvc/getallvms will retreive ALL virtual machines on that host
#piping it to sed and awk will let us grab JUST the VM ID

for i in `vmware-vim-cmd vmsvc/getallvms | sed ‘1d’ | awk ‘{print $1}’`

#Now we will do the do loop with the VM ID’s we grabbed.
#vmware-vim-cmd vmsvc/snapshot.create will let us create snapshots
#$i is the VM ID we grabbed
#The first string after the VM ID is the Snapshot Name
#Second string is the Snapshot Description, see how we’re using $DATE

vmware-vim-cmd vmsvc/snapshot.create $i Nightly Netapp.Snapshot.$DATE true

#Now we can create our NetApp snapshots – Here I keep one week worth of snapshots
#Taken once per day that means I need 7 snaps and the 8th is deleted
#So first we will delete the 8th, then rename the others in reverse order, then create a new one

ssh <netapp> snap delete <volume> vmsnap.8
ssh <netapp> snap rename <volume> vmsnap.7 vmsnap.8
ssh <netapp> snap rename <volume> vmsnap.6 vmsnap.7
ssh <netapp> snap rename <volume> vmsnap.5 vmsnap.6
ssh <netapp> snap rename <volume> vmsnap.4 vmsnap.5
ssh <netapp> snap rename <volume> vmsnap.3 vmsnap.4
ssh <netapp> snap rename <volume> vmsnap.2 vmsnap.3
ssh <netapp> snap rename <volume> vmsnap.1 vmsnap.2
ssh <netapp> snap create <volume> vmsnap.1

#Finally we will go back and remove those VMware snapshots using vmware-vim-cmd

for i in `vmware-vim-cmd vmsvc/getallvms | sed ‘1d’ | awk ‘{print $1}’`
vmware-vim-cmd vmsvc/snapshot.removeall $i

If you have multiple ESX hosts you can use the do loop statement to SSH into them and create/remove snapshots.

#Same script as above, but we will SSH into the host and run the command remotely
#To generate snapshots….

for i in `ssh dpcrcvmesx2 vmware-vim-cmd vmsvc/getallvms | sed ‘1d’ | awk ‘{print $1}’`
ssh dpcrcvmesx2 vmware-vim-cmd vmsvc/snapshot.create $i Nightly Netapp.Snapshot.$DATE includeMemory

#To remove snapshots…
for i in `ssh dpcrcvmesx2 vmware-vim-cmd vmsvc/getallvms | sed ‘1d’ | awk ‘{print $1}’`
ssh dpcrcvmesx2 vmware-vim-cmd vmsvc/snapshot.removeall $i

I’ve included the script in this post so you can download it. You may have noticed that the script will remove all of your snapshots, frankly I do not like this but I have yet to hear from VMware support on how to effectively use the vmsvc/snapshot.remove function. If you know how to do this please email me or comment and I’ll be sure to give you proper recognition.

One last thing to do after you write your script is to put it in the crontab of your server, crontab is similar to the Scheduled Tasks function of Windows, it allows you to run programs/scripts at select times. It is extremely flexible!

crontab -e (this edits your existing crontab)

The following shows my crontab, I run my script at Midnight and 12:00PM every day, I also output the results of the job to a log file (that gets written over each time).

[root@dpcrcvmesx1 .ssh]# crontab -l
0 0,12 * * * /usr/sbin/vmnfssnap > /var/log/vmnfssnap 2>&1

There are thousands of different ways you can do this, but this is how we do it at my job and it works without error. Hopefully this encourages you to look into all the different ways you can use scripting with VMware ESX to maintain and manage your system.

Created on September 12, 2008 by Rick Scherer

Posted under Backup & Recovery, ESX 3.5 Tips, ESXi 3.5 Tips, NetApp, Storage, VMware.

This blog has 18,338 views.

Tags: , , , ,

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

4 Comments so far

  1. skiser
    11:36 am on October 8th, 2008

    We created an elaborate script to do the same thing when we first put VI3 in. We were using the VCB 1.0 framework to do this. This caused several VMs to crash on a random basis when the VCB framework was trying to release the ESX snapshots.

    We got fed up with the process and took ESX snapshots out of the picture. If your VMware infrastructure is on a NetApp, you have great snapshot/snapvault technology built in. We just use NetApp to trigger snapvaults of our datastores to a filer at our DR facility. We are able to restore from these without issue (both bring whole VMs back, or just pull files out with a VDK explorer).

    Bottom line, if the script is working well for you, great…if not, scrap it and just use your NetApp.

  2. Rick Scherer
    11:27 pm on October 13th, 2008

    Well I agree, it is not for everyone but I must say we have not had an issue with this yet.

    I have my VM/NetApp snapshots taken nightly then on Sunday during the day (4am-4pm) I have VCB take file level backups to our IBM Tivoli server.

    At a later time I will try to look into NetApp’s built in NDMP capability which should simplify things even further.

  3. Nick
    11:12 am on November 26th, 2008

    If you are a NetApp customer you have access to download their VIBE (Virtual Infrastructure Backup Engine) under the toolchest download page on NOW. It’s a PERL based application that uses SSH as well. Since it was created for NetApp Professional Services and VARs (before SM-VI was released) you won’t be officially supported, but it is well documented and easy to use.

  4. Hajo Ehlers
    5:25 am on December 3rd, 2008

    > You may have noticed that the script will remove
    > all of your snapshots, frankly I do not like this

    Its looks like that in case you do the snapshot via the VC Server using vcbSnapshot other snapshots are not affected.

    esx : ESX Server
    esx-vcs : MS Server running the VCS

    ## Login to any ESX Server
    ssh esx

    ## Create snapshot via VCS
    # Get moref from the VCS Server
    vcbVmName -h esx-vcs -u administrator -s ipaddr:MyVM | grep “^moref:”

    Result is: moref:vm-544

    #Create Snapshot
    vcbSnapshot -h esx-vcs -u administrator -c moref:vm-544 Snapshot
    Result is:

    ## Backup VM with your favorite tool

    ## Remove backup snapshots
    #List Snapshot(s) for a given VM
    # Snapsshot done via GUI are not seen !
    vcbSnapshot -h esx-vcs -u administrator -f moref:vm-211 Snapshot

    Result is:

    # Commit /delete snapshot
    vcbSnapshot -h esx-vcs -u administrator -d moref:vm-211 SsId:snapshot-661

    So just create a snapshot via GUI and check that the vcbsnapshot does not find this snapshot. Maybe even snapshots via vmware-cmd are not seen.



Leave a Comment

Name (required)

Email (required)



More Blog Post