NetApp Snapshots in ESX

0 Comments Backup & Recovery, ESX 3.5 Tips, ESXi 3.5 Tips, NetApp, Storage, VMware

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 snap delete vmsnap.8
ssh snap rename vmsnap.7 vmsnap.8
ssh snap rename vmsnap.6 vmsnap.7
ssh snap rename vmsnap.5 vmsnap.6
ssh snap rename vmsnap.4 vmsnap.5
ssh snap rename vmsnap.3 vmsnap.4
ssh snap rename vmsnap.2 vmsnap.3
ssh snap rename vmsnap.1 vmsnap.2
ssh snap create vmsnap.1</span>

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

for i in vmware-vim-cmd vmsvc/getallvms | sed &#8216;1d&#8217; | awk &#8216;{print $1}&#8217;
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 &#8216;1d&#8217; | awk &#8216;{print $1}&#8217;
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 &#8216;1d&#8217; | awk &#8216;{print $1}&#8217;
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.