Thursday, October 18, 2007

Backup to USB: Batch file to email DIR list of backup target folder

I run this bat file after a backup to file on a USB drive. It emails me the bkf file names, sizes, and times along with the volume name of the USB drive. This answers two questions which are always at the back of my mind when I'm reviewing backup logs:
  1. Is last night's backup significantly different in size or completion time than the historic trend for this customer?
  2. Did the customer's designated Drive Swapping Person remember to do the drive swap?
Note that there are some long command lines in the bat file which will be wrapped on this webpage and require some manual repair before the bat file is usable. To make this a little easier to figure out, all the comments are green. Each command is followed by a blank line.
-----------------------------------------------------------------------------------
:: Email Directory List of Backup Target Folder at Completion of Backup


:: This bat file will email the contents of a text file as the message body, along
:: with designated attachments. It uses the free Blat commandline mailer,
:: downloadable from www.blat.net. Copy blat.exe to %windir%\system32 or some
:: other place in the path.

:: Do NOT use ampersand in the string for any set statement.


:: Get a dir list of the target folder, including the volume name.
:: Target folder may be either mapped to drive letter or an UNC path.

:: Set Dir List Parameters Here

SET TARGET_FOLDER=\\wrkstn23\Nightly_Backup

SET DIR_OUTPUT=c:\Program Files\BackupAssist\TargetFolderDirList.txt

dir "%TARGET_FOLDER%" > "%DIR_OUTPUT%"


:: Set Email Parameters Here
::

SET CUSTOMER_NAME=Test Customer

SET MESSAGE_BODY=%DIR_OUTPUT%

SET SEND_LOGS_TO=joe@domain.com,jane@domain2.com

SET EMAIL_SUBJECT=%CUSTOMER_NAME% - Backup Volume and File List

:: Name or IP address for SMTP_SERVER.
SET SMTP_SERVER=smtp.someserver.com

:: SENDER_ADDRESS may need to be one known by server
SET SENDER_ADDRESS=BackupAssist@testcustomer.com

SET SEND_ATTEMPTS=10

SET AUTH_IF_NEEDED=

:: SET AUTH_IF_NEEDED=-u UserName -pw PassWord
:: Leave AUTH_IF_NEEDED blank if no authentication required
:: There are additional switches available for authentication by
:: various means. See www.blat.net.

:: Files below are sent as attachments.

:: SET BINARY_FILES="C:\Temp\pic1.bmp","C:\Temp\pic2.bmp"

:: SET TEXT_FILES="C:\WINDOWS\pfirewall.log","C:\WINDOWS\system.ini"

::
::***********************************************************************************


:: Send backup log to various recipients.
:: Note that the debug switch will make
:: the blat logfile grow quickly.
:: If the -attach or -attacht switches are in the
:: command line AND the corresponding
:: variable(s) is not set, blat will fail. Four
:: sample commands are shown below, with
:: differences noted.

:: The following command creates a blat log with timestamps but no debug info.

:: blat "%MESSAGE_BODY%" -to %SEND_LOGS_TO% -subject "%EMAIL_SUBJECT%" -log blat1.log -timestamp -serverSMTP %SMTP_SERVER% %AUTH_IF_NEEDED% -try %SEND_ATTEMPTS% -f %SENDER_ADDRESS% -attach %BINARY_FILEs% -attacht %TEXT_FILES%

:: The following command creates a blat log with timestamps and debug info.

:: blat "%MESSAGE_BODY%" -to %SEND_LOGS_TO% -subject "%EMAIL_SUBJECT%" -log blat2.log -debug -timestamp -serverSMTP %SMTP_SERVER% %AUTH_IF_NEEDED% -try %SEND_ATTEMPTS% -f %SENDER_ADDRESS% -attach %BINARY_FILEs% -attacht %TEXT_FILES%

:: The following command creates a blat log with timestamp, no debug info, and
:: does not send any attachments.

::blat "%MESSAGE_BODY%" -to %SEND_LOGS_TO% -subject "%EMAIL_SUBJECT%" -log blat2.log -timestamp -serverSMTP %SMTP_SERVER% %AUTH_IF_NEEDED% -try %SEND_ATTEMPTS% -f %SENDER_ADDRESS%

:: The following command creates a blat log with timestamp and debug info, and
:: does not send any attachments.

blat "%MESSAGE_BODY%" -to %SEND_LOGS_TO% -subject "%EMAIL_SUBJECT%" -log blat2.log -debug -timestamp -serverSMTP %SMTP_SERVER% %AUTH_IF_NEEDED% -try %SEND_ATTEMPTS% -f %SENDER_ADDRESS%

No comments: