NetApp SnapManager for SQL Server Update backup script

1.2k Views Asked by At

I was very disappointed that NetApp just doesn't add Feature to automatically update the (MS)SQL Job automatically every time a new Database gets created. (for a Full backup)

So i now want to share with you what I did to automatically add a new Database to out full SQL backup job.

Maybe someone has some ideas on how to make things a little bit more efficient or create not so much temporary files :)

Just Create a SQL job with executes the flowing batch file to update the NetApp SnapManager SQL Job.

@echo off

set SQLinstance=YOUR_SQL_SERVERNAME
REM if something differs
set SQL_SERVER=%SQLinstance%
REM SQL Verify Instalce
set SQL_VERIFY=ANOTHER_SERVERNAME\VERIFY

set db_list=list_%SQL_SERVER%.txt
set db_list_wo_summary=list_new_%SQL_SERVER%.txt
set linecount_file=linecount_%SQL_SERVER%.txt
set output-Full-User-file=output-Full-%SQL_SERVER%.txt
set output-Inc-User-file=output-Inc-%SQL_SERVER%.txt

set SQLCMD="C:\Program Files\Microsoft SQL Server\110\Tools\Binn\sqlcmd.exe"
set arg_start=C:\Program Files\NetApp\SnapManager for SQL Server\SmsqlJobLauncher.exe new-backup  -svr ''%SQLinstance%''  -d  ''%SQLinstance%'',
set arg_end_full= -ver  -verInst ''%SQL_VERIFY%'' -mp -mpdir ''C:\Program Files\NetApp\SnapManager for SQL Server\SnapMgrMountPoint'' -RetainBackupDays  8 -RetainShareBackupDays  17 -cpylgbkshare COPYLOG_TOSHARE -lb -bksif -RetainSnapofSnapInfoDays 17 -rudays 1 -gen -mgmt daily
set arg_end_incremental= -ver  -verInst ''%SQL_VERIFY%'' -mp -mpdir ''C:\Program Files\NetApp\SnapManager for SQL Server\SnapMgrMountPoint'' -RetainBackupDays  8 -RetainShareBackupDays  17 -cpylgbkshare COPYLOG_TOSHARE -lgbkonly -bksif -RetainSnapofSnapInfoDays 17 -gen -mgmt daily

set SQL_QUERIE="SELECT NAME FROM master.sys.databases AS dtb WHERE NOT (CAST(case when dtb.name in ('master','model','msdb','tempdb') then 1 else dtb.is_distributor end AS bit)=1) ORDER BY NAME"

set JOBNAME_FULL_BACKUP=jobname_full
set JOBNAME_INCREMENTAL_BACKUP=jobname_incremental

set SQL_UPDATE_COMMAND_START="UPDATE msdb.dbo.sysjobsteps SET command='"
set SQL_UPDATE_COMMAND_END=' FROM msdb.dbo.sysjobs, msdb.dbo.sysjobsteps WHERE msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobsteps.job_id AND name = '%JOBNAME_FULL_BACKUP%'"
set SQL_UPDATE_COMMAND_END_INC=' FROM msdb.dbo.sysjobs, msdb.dbo.sysjobsteps WHERE msdb.dbo.sysjobs.job_id = msdb.dbo.sysjobsteps.job_id AND name = '%JOBNAME_INCREMENTAL_BACKUP%'"

REM Delete Old files (just to be sure)
del %db_list_wo_summary% %db_list% %linecount_file%

REM Get a List af all Databases and write them into a File
%SQLCMD% -E -S %SQLinstance% -o %db_list% -h -1 -s "," -W -Q %SQL_QUERIE%

setLocal EnableDelayedExpansion

REM Remove the last 2 Lines (one is a summary line, ant the other is empty)
set count=
for /f %%x in ('type %db_list% ^| find /c /v ""') do set /a lines=%%x-2
copy /y nul %tmp%\tmp.zzz > nul
for /f "tokens=*" %%x in ('type %db_list% ^| find /v ""') do (
    set /a count=count+1
    if !count! leq %lines% echo %%x>>%tmp%\tmp.zzz
)
move /y %tmp%\tmp.zzz %db_list_wo_summary% > nul

REM removes all linebreaks and adds a ' on every star and ending of the string (and adds a comma)
for /f "tokens=* delims= " %%a in (%db_list_wo_summary%) do (set s=!s!%%a'', '')

REM removes the last four chars (, '')(comma, space, and 2 '')
set s=!s:~0,-4!

REM counts the lines of the file NOT containing the string XXXYYYZZZ (basicaly a line count)
type %db_list_wo_summary% | find /V /C "XXXYYYZZZ" > %linecount_file%
for /f "tokens=* delims= " %%c in (%linecount_file%) do (set cn=%%c)

REM Creates the final string for the SQL job
set s1=%arg_start% ''%CN%'', ''!s!%arg_end_full%
set s2=%arg_start% ''%CN%'', ''!s!%arg_end_incremental%

REM Update the SQL Job step command with the new string via SQLCMD
%SQLCMD% -E -S %SQLinstance% -Q %SQL_UPDATE_COMMAND_START%%s1%%SQL_UPDATE_COMMAND_END%
%SQLCMD% -E -S %SQLinstance% -Q %SQL_UPDATE_COMMAND_START%%s2%%SQL_UPDATE_COMMAND_END_INC%

REM deleting the temp file
del %db_list_wo_summary% %db_list% %linecount_file%

I hope this can help anyone make things a bit more efficient.

2

There are 2 best solutions below

0
On

The answer is above :) Hopefully someone will find that user full, or at least inspiring ;)

0
On

Don't specify a database to backup at all ... then SMSQL will backup all databases, also the new ones