So if you have been creating all of the batch files in the series you're ready for this one. This script will shuffle the snapshots for a virtual server, pause the server, create a netapp snapshot, and resume the vm server. You want to make sure you're following the NetApp / VMware best practice of moving the working directory and swap location to a non-snapshot volume so your snapshots don't include unnecessary changes. I can write more about that if requested but for now I would refer you to the NetApp or VMWare sites for best practices:
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM
REM Usage
REM snapshot [vmname] [filer] [ESXi Host] [SAN Volume] [vmx location] [# of Snapshots to keep] [csv file: optional]
REM
REM This batch file performs the following tasks:
REM Deletes the oldest Snapshot for [SAN Volume]
REM Shuffles Snapshots on [filer] for [SAN Volume] using [vmname] as the Snapshot name
REM Suspends [vmx location] on [ESXi Host]
REM Sync's [ESXi Host]
REM Creates Snapshot on [filer] for [SAN Volume] using [vmname]
REM Resumes [vmxlocation] on [ESXi Host]
REM
REM This script can be used ad hoc to create Snapshots or called from a maintenance script that runs nightly
REM
REM All variables must be set below
REM
REM DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
if "%6" == "" (
goto param_error
)
Set Guestname=%1
Set SANAddress=%2
Set ESXiAddress=%3
set SANVolume=%4
Set VMXLocation=%5
set /a count=%6
if "%7" == "" (
set logname=%1
) ELSE (
set logname=%7
)
REM Begin the Script
echo ---------------------------------------------------
echo Starting at %date% %time%
echo Starting %Guestname% Snapshot at %date% %time% >> Snapshot_%logname%.log
echo ---------------------------------------------------
echo.
set /a count=count-1
set /a counta=count
echo Deleting the oldest Snapshot . . .
REM Delete the oldest Snapshot
call ncmd %SANAddress% "snap delete %SANVolume% %Guestname%_snap.%count%"
echo.
echo.
echo ---------------------------------------------------
:Loop
if /i %count% GEQ 0 (
set /a count1=%count%
set /a count=count-1
if /i %count% EQU %counta% (
goto Loop
)
REM Shuffle remaining Snapshots
echo Shuffling Snapshot %count1%
call ncmd %SANAddress% "snap rename %SANVolume% %Guestname%_snap.%count% %Guestname%_snap.%count1%"
goto Loop
) else (
echo ---------------------------------------------------
echo.
REM Suspend the VM
echo.
echo ---------------------------------------------------
echo Suspending %1
echo.
call vcmd %ESXiAddress% %VMXLocation% suspend soft >> Snapshot_%logname%.log
echo. >> Snapshot_%logname%.log
REM Sync the ESXi Server
echo.
echo.
echo ---------------------------------------------------
echo Syncronizing %3...
echo.
call ersh %ESXiAddress% "sync"
REM Create the new Snapshot
echo.
echo.
echo ---------------------------------------------------
echo Creating Snapshot on %2...
echo.
call ncmd %SANAddress% "snap create %SANVolume% %Guestname%_snap.0" >> Snapshot_%logname%.log
echo. >> Snapshot_%logname%.log
goto finish
)
:finish
REM Restart the VM
call vcmd %ESXiAddress% %VMXLocation% start soft >> Snapshot_%logname%.log
echo. >> Snapshot_%logname%.log
echo.
echo.
echo ---------------------------------------------------
echo Finished at %date% %time%
echo Finished %Guestname% Snapshot at %date% %time% >> Snapshot_%logname%.log
echo ---------------------------------------------------
echo.
goto end
:param_error
echo ERROR! You did not enter the correct number of parameters!
echo.
echo usage: snapshot [vmname] [filer] [ESXi Host] [SAN Volume] [vmx location] [# of Snapshots to keep]
echo.
echo [vmname] - You entered [%1]
echo.
echo This is the name of the virtual server
echo (ie: vmserver01)
echo.
echo [filer] - You entered [%2]
echo.
echo This is the name of the NetApp filer
echo (ie: netapp01)
echo.
echo [ESXi Host] - You entered [%3]
echo.
echo This is the name of the ESXi Host server
echo (ie: vm01)
echo.
echo [SAN Volume] - You entered [%4]
echo.
echo This is the name of the NetApp volume
echo (ie: vmserver01)
echo.
echo [vmx location] - You entered [%5]
echo.
echo This is the location of the vmx file
echo (ie: "[vmserver01] vmserver01/vmserver01.vmx")
echo You should enclose this in ""
echo also make sure you get the [datastore] vmpath correct
echo.
echo [# of Snapshots] - You entered [%6]
echo.
echo This is the number of Snapshots to keep for this VM
echo (ie: 7)
goto end
:end
Friday, April 3, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment