Exchange 2003 Automatic Backup Script using NTBackup

Objective: Backup my Exchange Server mailbox/mailstore with Windows server built-in NTBACKUP.

My example allow NORMAL and DIFFERENTIAL backup to be run using same batch file.

Backup of Exchange is a must.  Besides having all the backup data for recovery purpose, it cleans up the exchange transaction log which will use up all your harddisk space eventually.

NTBACKUP can be run in GUI or in command line mode.  My example here makes use of the command line mode so that the backup can be scheduled and run automatically.

The reason for this project is I don't have budget for the Exchange mailbox option of my BackupExec.  I use NTBACKUP to backup the exchange mailbox which allow me to backup the mailbox while the mailbox is in operation.  The backup file is then backup to tape using standard backup method.

The below script is tested on Exchange 2003 server on Windows 2003 Standard.

All you need is to do the below:

  1. Define Backup Selection (*.bks) (which mailbox to backup) by using Backup Utility (ntbackup.exe)
    1. run NTBACKUP.EXE
    2. select the "Backup" tab
    3. On the left panel,  expand the tree node "Microsoft Exchange Server"
    4. Drill down to the Exchange server you interested in.
    5. Expand "Microsoft Information Store"
    6. Check any storage group or individual mailbox you want to include in the "selection".
    7. On the menu bar, select Job - Save Selection As, and save the selection file in a location which your windows scheduler can later access.
    8. In this example, I use EXMAIL_EXCHANGE.bks for my file name.
  2. Create a batch file: backup.bat
    1. I usually put it alongside with by *.bks file
  3. Create two backup schedules using standard windows scheduler
    1. I have two backup schedule, with different options calling the same batch file

      Daily Differtial Backup (Mon-Sat)
      \\BACKUPSVR\EXMAIL_backup\BACKUP.BAT DIFF

      Weekly Normal Backup at Sunday
      \\BACKUPSVR\EXMAIL_backup\BACKUP.BAT NORMAL




Content of Backup.bat

(The original script was in German and was copied from somewhere online but I loss the original URL.  I have added email function to ship the log to support tech.)


net use k: \\BACKUPSVR\EXMAIL_backup

IF "%1" == ""  goto WRONGINPUT
IF "%1" == "NORMAL"  goto START
IF "%1" == "DIFF"  goto START

:WRONGINPUT
    echo Please give parameter, either NORMAL or DIFF
    pause
    goto eof

:START

IF "%1" == "NORMAL" (
    REM set BackupJob=SG_Normal
    set jobDescription=EXMAIL_SG_Normal
    set backupType=normal
) ELSE (
    set jobDescription=EXMAIL_SG_Differential
    set backupType=differential
)

set BackupJob=%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\EXMAIL_EXCHANGE.bks


set blat=C:\BLATMAIL\blat.exe
set Recipient=support@mydomain.com
set Subj="%computername% Backup Successfully Completed"
set Desc=Backup ran OK for %computername% at %USERDOMAIN% on
set Subj1="%computername% backup completed with exceptions"
set Subj2="%computername% backup failed - no media"
set Subj3="%computername% backup completed with exceptions"
set Subj4="%computername% backup aborted"
set Subj5="%computername% backup failure"
set Subj9="%computername% backup failure"
set Desc1=Problems with Exchange backup on %computername% at %USERDOMAIN% on
set Desc2=No external hard disk found on %computername% at %USERDOMAIN% on
set Desc3=Problems with the hard disk found on %computername% at %USERDOMAIN% on
set Desc4=Backup broken up by user on %computername% at %USERDOMAIN% on
set Desc5=Backup failed - cannot access file on %computername% at %USERDOMAIN% on
set Desc9=Backup did not successfully complete on %computername% at %USERDOMAIN% on
set ntbackup=%windir%\system32\NTBACKUP.EXE
set LOGDIR=%USERPROFILE%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data
set blatErrorFile="%temp%\%random%%random%.txt"
set blatTEMPfile="%temp%\%random%%random%.txt"

for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%k%%j%%i_%%l
for /f "Tokens=1-2 Delims=: " %%i in ('time /t') do set tm=-%%i%%j
set dtt=%dt%%tm%
echo %dtt%


REM The below session is remarked.  The orginal purpose is to allow multiple Backup setting.  I do not use the below function but retained here for my reference only.
@REM for %%a in (V W X Y) do if exist %%a:\ set BackupJob=%%a.bks
  
@REM if /i "%BackupJob%"=="V.bks" (
  @REM set jobDescription=Full backup Monday through Friday
  @REM set DriveLetter=V
@REM )

@REM if /i "%BackupJob%"=="W.bks" (
  @REM set jobDescription=Friday backup
  @REM set DriveLetter=W
@REM )

@REM if /i "%BackupJob%"=="X.bks" (
  @REM set jobDescription=Monday and Wednesday backup
  @REM set DriveLetter=X
@REM )

@REM if /i "%BackupJob%"=="Y.bks" (
  @REM set jobDescription=Tuesday and Thursday backup
  @REM set DriveLetter=Y
@REM )

  @rem set DriveLetter=Y

:RUNBACKUP
rem For a reference of NTBACKUP command line parameters, run NTBACKUP /?, or refer to MS KB814583.
del "\\BACKUPSVR\EXMAIL_backup\SG_%backupType%.bkf"
%ntbackup% backup "@%BackupJob%" /n "%computername%-%dtt%" /d "%jobDescription%" /v:no /r:no /rs:no /m %backupType% /j "%computername% SG %backupType% Backup" /l:f /f "K:\SG_%backupType%.bkf" /fu

:CHECKLOGFORERRORS
FOR /F %%F IN ('DIR /B /A-D /OD /TW "%LOGDIR%\Backup??.log"') DO (
 SET LOGFILE=%%F
)

type "%LOGDIR%\%LOGFILE%" | FINDSTR /C:"damaged or incomplete" > NUL
IF NOT ERRORLEVEL 1 (
 set Subj=%Subj1%
 set Desc=%Desc1%
 GOTO BLAT
)

type "%LOGDIR%\%LOGFILE%" | FINDSTR /C:"media not found" > NUL
IF NOT ERRORLEVEL 1 (
 set Subj=%Subj2%
 set Desc=%Desc2%
 GOTO BLAT
)

type "%LOGDIR%\%LOGFILE%" | FINDSTR /C:"device reported an error" > NUL
IF NOT ERRORLEVEL 1 (
 set Subj=%Subj3%
 set Desc=%Desc3%
 GOTO BLAT
)

type "%LOGDIR%\%LOGFILE%" | FINDSTR /C:"User aborted" > NUL
IF NOT ERRORLEVEL 1 (
 set Subj=%Subj4%
 set Desc=%Desc4%
 GOTO BLAT
)
  
type "%LOGDIR%\%LOGFILE%" | FINDSTR /C:"do not have access" > NUL
IF NOT ERRORLEVEL 1 (
 set Subj=%Subj5%
 set Desc=%Desc5%
 GOTO BLAT
)

type "%LOGDIR%\%LOGFILE%" | FINDSTR /C:"did not successfully complete" > NUL
IF NOT ERRORLEVEL 1 (
 set Subj=%Subj9%
 set Desc=%Desc9%
 GOTO BLAT
)


REM Successfull!!
rem Email address of our mailbox for Helpdesk log
set Recipient=monitor@mydomain.com

:BLAT
rem Send email to our help desk to notify the result
echo %Desc% %dtt% > %blatTEMPfile%
%blat% %blatTEMPfile%  -server EXMAIL -f monitor@mydomain.com -to %Recipient% -s %Subj% -attach "%LOGDIR%\%LOGFILE%"
del /q /f "%blatTEMPfile%"
goto :EOF

:EOF
REM echo Y | net use k: /delete
rem pause
rem exit





Comments

Popular Posts