Rudimentary .BATch backup with 7zip

A very basic .bat that archives a bunch of files using 7zip in a different package each day of the week (i.e. bak_1.7zip to bak_7.7zip), then copies the archive to a different machine in a shared folder and fires off an email with the result. Effectively a week’s worth of full backups. This is NOT real backup.

It needs 7zip, the command line version for creating the archive and Blat to send the email. By the way, Blat is really great, free of charge and the libraries are easy to use, you can integrate it in almost any programming language to send/receive email straight from your program. Awesome.

While you’re downloading additional programs for this, might as well get Realdate too and use it instead of line 5. The problem with parsing current date/time in .bat files is that DATE and TIME return their results in the system’s locale format, so on one computer it might be “2012-01-11”, while on another system it could be “Wed 01/11/2011” or somesuch. Line 5, as it is in this script, uses Windows Management Instrumentation to get the day of week, independent of locale settings. But, as Microsoft standards go, it doesn’t work on all versions of Windows. So instead of using some convoluted way of getting such simple information, if the line below doesn’t work just use Realdate.

@echo off
REM this probably won't work on some newer OS

REM sets DOW= day of week in numeric format (1/2/3/etc.)
for /f %%i in ('wmic path win32_LocalTime get DayOfWeek') do set DOW=%%i

REM what to zip
SET ZINPUT="C:\path to files\that you want\backed up\*.*"

REM where to zip
SET ZOUTPUT="C:\zbak\arch\bak_%DOW%.7z"

REM zip log file
SET zLOG=c:\zbak\arch\bak_%DOW%.log

REM email settings for blat
SET ESMTPSRV=smtp.example.com
SET EFROM=bak_sender@example.com
SET ESUBJ="daily backup log"
SET ETO=it@example.com

REM samba path to mount
SET SMBPATH=\\fileserver\share\arch_dir

REM DoTheZip, mount the V, copy backups, unmout, send mail

7za.exe a %ZOUTPUT% %ZINPUT% > %ZLOG%

net use V: %SMBPATH% /PERSISTENT:No
IF ERRORLEVEL 1 (
	echo Error mapping drive V: >> %ZLOG%
	GOTO :mail
)

copy %ZOUTPUT% V:\
dir V:\*.* >> %ZLOG%

net use V: /delete 
IF ERRORLEVEL 1 (
	echo Error unmapping drive V: >> %ZLOG%
)

:mail
REM Send log by mail to IT
blat %ZLOG% -server %ESMTPSRV% -f %EFROM% -subject %ESUBJ% -to %ETO%

Obviously, if PowerShell is available or some real scripting/programming language this can be much more elegant.