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
srsh.bat
This batch file sends commands to the NetApp filer.
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM srsh [filer] "[command]"
REM
REM Be sure to enclose the command in "" marks
REM
REM This batch file assumes that the public private key pairing has been configured on the filer
REM
REM This batch file will send the [command] to the [filer] using a private key. This allows a script
REM access to the filer without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
plink -l root %1 -i "x:\path\to\yourkey.ppk" %2
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM srsh [filer] "[command]"
REM
REM Be sure to enclose the command in "" marks
REM
REM This batch file assumes that the public private key pairing has been configured on the filer
REM
REM This batch file will send the [command] to the [filer] using a private key. This allows a script
REM access to the filer without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
plink -l root %1 -i "x:\path\to\yourkey.ppk" %2
rescan.bat
This batch file rescans the ESXi hba for new changes. I run this twice from scripts because there is a bug in ESXi that causes you to rescan twice for some hba's:
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM rescan [ESXi Host] [hba]
REM
REM This batch file assumes that the public private key pairing has been configured on the ESXi Host
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\esxcfg-rescan.pl" --server %1 --username root --password password %2
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM rescan [ESXi Host] [hba]
REM
REM This batch file assumes that the public private key pairing has been configured on the ESXi Host
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\esxcfg-rescan.pl" --server %1 --username root --password password %2
register.bat
This batch file will register a vm on an ESXi server. This assumes you have configured the public/private key authentication and that you have installed the vmware cli utility for ESXi:
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM
REM This file has 2 variations on input:
REM
REM Option 1
REM register [ESXi Host] [vmxname] [Resource] [Pool]
REM if you pass 4 variables to the file it will assume you have a vm with the structure [vmxname] vmxname/vmxname.vmx
REM We use resouce pools with 2 names (ie: Maintenance Systems, Development Systems, etc) which is why there's the "%3 %4" at the end
REM
REM Option 2
REM register [ESXi Host] [vmxname] [Resource] [Pool] [full vmx path]
REM If you pass 5 varialbes the second variable is ignored and the 5th variable should be the full path to the vmx file.
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
if %5=="" (
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\vmware-cmd.pl" -v -H %1 -U root -P password -s register "[%2] %2/%2.vmx" %1 "%3 %4"
) else (
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\vmware-cmd.pl" -v -H %1 -U root -P password -s register %5 %1 "%3 %4"
)
make sure you change password to your password.
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM
REM This file has 2 variations on input:
REM
REM Option 1
REM register [ESXi Host] [vmxname] [Resource] [Pool]
REM if you pass 4 variables to the file it will assume you have a vm with the structure [vmxname] vmxname/vmxname.vmx
REM We use resouce pools with 2 names (ie: Maintenance Systems, Development Systems, etc) which is why there's the "%3 %4" at the end
REM
REM Option 2
REM register [ESXi Host] [vmxname] [Resource] [Pool] [full vmx path]
REM If you pass 5 varialbes the second variable is ignored and the 5th variable should be the full path to the vmx file.
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
if %5=="" (
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\vmware-cmd.pl" -v -H %1 -U root -P password -s register "[%2] %2/%2.vmx" %1 "%3 %4"
) else (
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\vmware-cmd.pl" -v -H %1 -U root -P password -s register %5 %1 "%3 %4"
)
make sure you change password to your password.
ersh.bat
This batch file will send commands to the ESXi host. These are command line commands to the host vs. vmware-cmd.pl commands as in my vcmd.bat sample.
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM ersh [ESXi Host] "[command]"
REM
REM Be sure to enclose the command in "" marks
REM
REM This batch file assumes that the public private key pairing has been configured on the ESXi Host
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
plink -l root %1 -i "x:\path\to\yourkey.ppk" %2
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM ersh [ESXi Host] "[command]"
REM
REM Be sure to enclose the command in "" marks
REM
REM This batch file assumes that the public private key pairing has been configured on the ESXi Host
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
plink -l root %1 -i "x:\path\to\yourkey.ppk" %2
vcmd
This is the script I use to send vmware-cmd commands to my VMWare host servers. This assumes you have installed the Vmware VI Remote CLI app that includes perl (I installed the package VMware-VIRemoteCLI-3.5.0-104314.exe):
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM vcmd [ESXi Host] "[command]"
REM
REM Be sure to enclose the command in "" marks
REM
REM This batch file assumes that the public private key pairing has been configured on the ESXi Host
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\vmware-cmd.pl" -v -H %1 -U root -P password %2 %3 %4 %5 %6
As you can see the usage is vcmd.bat [ESXi Host] "[command]"
You will need to change the path to the private key you created from my previous post. Make sure you enclose the command in "'s
@echo off
REM --------------------------------------------------------------------------------------------------------------
REM Usage
REM vcmd [ESXi Host] "[command]"
REM
REM Be sure to enclose the command in "" marks
REM
REM This batch file assumes that the public private key pairing has been configured on the ESXi Host
REM
REM This batch file will send the [command] to the [ESXi Host] using a private key. This allows a script
REM access to the ESXi Host without the need to login in order to automate tasks
REM
REM This batch file is called from other batch files.
REM
REM DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!
REM --------------------------------------------------------------------------------------------------------------
perl "C:\Program Files\VMware\VMware VI Remote CLI\bin\vmware-cmd.pl" -v -H %1 -U root -P password %2 %3 %4 %5 %6
As you can see the usage is vcmd.bat [ESXi Host] "[command]"
You will need to change the path to the private key you created from my previous post. Make sure you enclose the command in "'s
Subscribe to:
Posts (Atom)