Tuesday, August 04, 2009

Windows XP/Server NTBackup Script

WARNING
If technical content gives you a headache SKIP THIS POST. I make a lot of assumptions about the technical savvy and familiarity of the user with the command line and DOS batch files.
I've played this one pretty close to the vest, but it's time for the world to behold the glory that is . . . My Windows NTBackup Script. This baby will back up your registry, your Exchange data, Sharepoint, user documents, and, provided you use the proper net stop commands, your databases can all be backed up. Familiarity with your database server also helps - often there is a command to dump the data from the database into a separate set of files that can be backed up while the database is still running.

This script is for backup to an external hard drive - in this case the i:\ drive. It's specific to many of my health care servers, but is easily modified to work almost anywhere - just delete the references to SharePoint, MySql, ProgressiveSQL, etc.

You will need to create the following directories on your external hard drive:




\backups
\backups-shortterm
\backups-longterm
\backups-delete


You will also need to create %windir%\backups\data\sysstate.bks and%windir%\backups\data\userdata.bks by opening up NTBackup, making the selections for your system state and user data, and saving them into the necessary files name and placed appropriately.

Lastly you will need to install Blat in your c:\windows\system32.


REM @echo off

REM ***Set Variables***
Set DateCode=%date:~-4%%date:~4,2%%date:~7,2%.%time:~0,2%%time:~3,2%%time:~6,2%
set sharepointbackuplocation=d:\sharepoint\backup
set blat=c:\windows\system32\blat.exe
set relayserver=mail.charter.net
set yoursite=http://server
set emailsub=SharePointBackupReport
set templog=d:\backupscripts\spbackup.txt
set sharepointfile=Backup.bak
set to=desireduser@yourdomain.com
set who=sbackup@yourdomain.com
set
reply=noreply@yourdomain.com

echo System Backup Script v1.1 by J Hoeft >> %templog%

REM ***Lock Sharepoint as readonly, create backup, unlock***
echo Backing up Sharepoin" >> %templog%
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\Stsadm.exe" -o setsitelock -url %yoursite% -lock readonly
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\Stsadm.exe" -o backup -overwrite -url %yoursite% -backupmethod full -filename %sharepointbackuplocation%\%sharepointfile% >>%templog%
"C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN\Stsadm.exe" -o setsitelock -url %yoursite% -lock none
dir %sharepointbackuplocation% >> %templog%


REM ***shutdown Progressive SQL for ECS backup***
echo Stopping Progress SQL >> %templog%
\\server\keane\dlc\bin\pccmd.exe proservice stop >>%templog%


REM ***delete previous ECS backup***
del /q d:\AmericanData\EcsBackUp\dbar
del /q d:\AmericanData\EcsBackUp\dbarforms
del /q d:\AmericanData\EcsBackUp\dbdoc
del /q d:\AmericanData\EcsBackUp\dbhistory
del /q d:\AmericanData\EcsBackUp\dbmds
del /q d:\AmericanData\EcsBackUp\dbparadox
del /q d:\AmericanData\EcsBackUp\dbpicklist
del /q d:\AmericanData\EcsBackUp\dbreport
del /q d:\AmericanData\EcsBackUp\mysql

REM ***create new backup files***
d:
cd \
cd AmericanData
cd mysql
cd bin
mysqladmin flush-tables
mysqladmin flush-tables

copy d:\AmericanData\mysql\data\dbar d:\AmericanData\EcsBackUp\dbar
copy d:\AmericanData\mysql\data\dbarforms d:\AmericanData\EcsBackUp\dbarforms
copy d:\AmericanData\mysql\data\dbdoc d:\AmericanData\EcsBackUp\dbdoc
copy d:\AmericanData\mysql\data\dbhistory d:\AmericanData\EcsBackUp\dbhistory
copy d:\AmericanData\mysql\data\dbmds d:\AmericanData\EcsBackUp\dbmds
copy d:\AmericanData\mysql\data\dbparadox d:\AmericanData\EcsBackUp\dbparadox
copy d:\AmericanData\mysql\data\dbpicklist d:\AmericanData\EcsBackUp\dbpicklist
copy d:\AmericanData\mysql\data\dbreport d:\AmericanData\EcsBackUp\dbreport
copy d:\AmericanData\mysql\data\mysql d:\AmericanData\EcsBackUp\mysql

REM ***Start Progressive SQL***
echo Starting Progress SQL >> %templog%
\\server\keane\dlc\bin\pccmd.exe proservice start >>%templog%

REM ***Do System State Backup***
ntbackup backup "@%windir%\backups\data\sysstate.bks" /j "System State Backup" /f "i:\backups\SysState-%datecode%.bkf" /d "System State" /v:yes /r:no /l:s /m normal /rs:no /hc:off

REM ***Do User Data Backup***
ntbackup backup "@%windir%\backups\data\userdata.bks" /j "User Data" /f "i:\backups\UserData-%datecode%.bkf" /d "Full - User Data" /v:yes /r:no /l:s /m normal /rs:no /hc:off


REM ***Cleanup backup files weekly***
If /i "%date:~0,3%" == "Sun" (
Echo Weekly backup file considation performed.>>%templog%
move i:\backups-longterm\*.* i:\backups-delete
move i:\backups-shortterm\*.* i:\backups-longterm
move i:\backups\*.* i:\backups-shortterm
del /q i:\backups-delete\*.*
)

REM *Email backup results*
dir i:\backups >>%templog%
%blat% "%templog%" -t %to% -s "%emailsub%" -i %who% -f "%reply%" -q -server "%relayserver%"

REM ***Delete Temp Log***
del /q %templog%

I found out how to print the NTBackup Log!  (From WindowsITPro.com, thanks, guys!)


Create a .bat file called PrtBakLog.bat that contains the following:



@echo off
setlocal
if {%1}=={} @echo Syntax PrintDevice (\\Server\Printer)
set device=%1
set prt=N
for /f "Tokens=*" %%i in ('dir /o-d /b "%userprofile%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\backup*.log"') do call :print %%i
endlocal
goto :EOF
:print
if not "%prt%" EQU "N" goto :EOF
set prt=Y
print /D:%device% "%userprofile%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\%1"
Insert Call PrtBkLog \\Server\Printer In the backup batch file, and you're going - you can even leave out the lame e-mailed reports!


No comments: