#!/bin/sh # NetApp Snapshot Script for VMWARE ESX # For use on the ESX host without VirtualCenter/VCB. # # Version 1.1 # 04 Dec 2008 # # By Jason Middleton (jason.middleton_@_netapp.com) # and Justin Mercier (justin.f.mercier_@_boeing.com) # # This script will quiesce the VMs for a clean NetApp snapshot. # It uses the variables below to define which filer, vol, and vfiler to snapshot on. # It also performs quota management and snapshot rotations on a hourly, daily, and weekly basis # as called by your crons. # # Usage: vmsnap hourly|daily|weekly # # Make sure that you schedule the script appropriately with the proper argument from your crontab # and have authorized keys setup on the filer to accept root logins from the ESX host. # Specify the local hostname, the NetApp volume, the filer hostname, and the vfiler we're on. VOL=esx_vol_vmdata_b FILER=ntap-b HOSTNAME=vmhost1 VFILER=ops # Set up quotas for the types of snapshots # This is not zero-based; to keep five daily snapshots set the value to 5 WEEKLY_QUOTA=1 DAILY_QUOTA=5 HOURLY_QUOTA=0 echo Starting "$0 $*" at `date` # Handle the argument to determine if this is a hourly, daily, or weekly backup. if [ $1 ]; then i=`echo $1 | tr '[:lower:]' '[:upper:]'` case "$i" in DAILY|WEEKLY|HOURLY) SNAPMODE=`echo $i | tr '[:upper:]' '[:lower:]'` QUOTA=`eval "echo \\$${i}_QUOTA"` ;; *) echo $i is not a valid argument. exit 0 esac else echo Please specify WEEKLY, DAILY, or HOURLY for the type of snaphot. exit 1 fi if [ "$QUOTA" -eq "0" ]; then echo Quota is set to 0, so no snapshot will be taken. exit 0 fi # save the old field separator and change it to newline OLDIFS="$IFS" IFS=" " # PATH Cleanup PATH=$PATH:/bin:/usr/bin # Since we have no synchronization between vmhosts, we're doing snapshots per host # and must name accordingly using the hostname, the vfiler name, the type of snap, # and a number. For example: vmhost0.sbu.daily.0 PRE=$HOSTNAME.$VFILER.$SNAPMODE SNAPCOUNT=`ssh $FILER snap list $VOL | grep $PRE | wc -l` # Now we need to truncate the older snapshots. This assumes that the rotation method below is sane. if [ "$SNAPCOUNT" -ge "$QUOTA" ]; then for (( i=$SNAPCOUNT-1; i>=$QUOTA-1; i-- )); do echo Truncating old snapshot $PRE.$i on $VOL ssh $FILER snap delete $VOL $PRE.$i done fi # Ok, now we can rotate the snapshots by incrementing them up. # i.e. vmhost0.sbu.daily.0 will become vmhost0.sbu.daily.1 for (( i=$QUOTA-2; i>=0; i-- )); do t=`expr $i + 1` echo Renaming $PRE.$i to $PRE.$t on $FILER ssh $FILER snap rename $VOL $PRE.$i $PRE.$t done # Command the ESX host to create vmware snapshots of all VMs for i in `vmware-cmd -l` do echo $i vmware-cmd $i createsnapshot backup NetApp true false done # Tell the NetApp to create a snapshot of the VOL echo Creating snapshot $PRE.0 on $FILER, volume $VOL ssh $FILER snap create $VOL $PRE.0 #Bring all VMs out of hot backup mode for i in `vmware-cmd -l` do echo $i vmware-cmd $i removesnapshots done # Now we set IFS back to its original value to ensure we don't exceed our local scope. IFS=$OLDIFS