HOME
Stephen Knight
04/27/2010 01:55 PM
Backup script with rotation of old backups and logs by email

Type:

Batch/Command file

Category:

Backup, Batch, Email
This scriot will clearly need customisation for your own uses but includes lots of examples of code. It as it stands backs up files from e:\lotus\domino\data\*.* to G:\backup\files\weekly, daily, monthly, or yearly directories and then keeps a specified number in each directory:

daily 25
weekly 1
monthly 12
yearly all

It keeps a log file for each day in g:\backup\log\og-ddmmyyyy.txt and sends those to a specified email address with details of succes or failure.

Hide details for CodeCode
@ECHO OFF
REM Backup script.
REM Steve Knight http://scripts.dragon-it.co.uk/
REM August 2007
REM Amended October 2007 keeps upto four weekly backups too pending larger storage device.
REM Amended October 2007 to keep 30 daily, 4 weekly, 12 monthly and
REM yearly backups now on G: and copy last of each type to F: too.

REM For counter loop to work
SETLOCAL ENABLEDELAYEDEXPANSION

REM DT is date in format ddmmyyyy with / removed.
set dt=%date:/=%

REM Set directories to backup to etc.
set log=G:\backup\log\log-%dt%.txt

REM Split date into dd mm yyyy variables
set /a dd=1%dt:~0,2% - 100
set /a mm=1%dt:~2,2% - 100
set /a yyyy=1%dt:~4,4% - 10000

REM Check for every seventh day. IsWeek=0 when day is divisible by 7. Week is week number
set /a IsWeek=(%dd%/7)*7-%dd%
set /a week=(%dd%/7)

REM Check for 1st Jan for yearly backup
if not "%dd%-%mm%"=="1-1" goto monthly
set destdir=g:\backup\files\yearly
set /A lastyear=%yyyy%-1
set dest=%destdir%\%lastyear%
echo %date% %time% yearly backup for year %lastyear% >>%log%
call :backup
call :updatelog
call :copybackup yearly
call :copylog
call :sendlog
goto :eof

:monthly
REM Check for 1st of month for monthly backup
if not "%dd%"=="1" goto weekly
set destdir=g:\backup\files\monthly
set dest=%destdir%\%yyyy%-%mm%
echo %date% %time% monthly backup for month %mm% >>%log%
set keep=12
call :rotate monthly
call :backup
call :updatelog
call :copybackup monthly
call :copylog
call :sendlog
goto :eof

:weekly
REM Check for weekly
if not %IsWeek%==0 goto daily
set dest=g:\backup\files\Weekly\Week%week%
echo %date% %time% Weekly backup for week %week% >>%log%
call :rotateweekly
call :backup
call :updatelog
call :copybackup weekly
call :copylog
call :sendlog
goto :eof

:daily
set destdir=G:\backup\files\daily
set dest=%destdir%\%dt%
echo %date% %time% Daily backup for day %dd% >>%log%
set keep=25
call :rotate daily
call :backup
call :updatelog
call :copybackup daily
call :copylog
call :sendlog
goto :eof


:rotateweekly
echo %date% %time% Checking for old weekly backups to delete>>%log%
REM Delete current weekly backup if needed
if exist "%dest%" rd /s/q "%dest%" >>%log%
goto :eof

:rotate
echo %date% %time% Checking for old %1 backups to delete %keep% to keep >>%log%
REM Delete oldest backup if more than %keep% to keep
set count=0
for /f "tokens=*" %%a in ('dir /b /ad /o-d "%destdir%"') do (
set /a count=!count!+1
REM echo %date% %time% Found !count! old backups. %%a>>%log%
if !count! GEQ %keep% rd /s/q "%destdir%\%%a">>%log%
)
echo %date% %time% Found %count% old %1 backups>>%log%
goto :eof

:backup
REM Take new copy to directory
echo %date% %time% Starting XCOPY to %dest%>>%log%
xcopy /s/e/y/r/q/i/c/exclude:g:\backup\scripts\exclude.txt "e:\lotus\domino\data\*.*" "%dest%">>%log% 2>>&1
if errorlevel 1 set err=%err%COPY%errorlevel%
goto :eof

:updatelog
REM Show size of backup in log file
diruse /m /, /* %dest%" >>%log%
goto :eof

:copybackup
REM take copy of latest monthly/daily/yearly/weekly backup from external drive to internal
echo %date %time% Copying backup from external drive to internal >>%log%
rd /s /q "F:\backup\copy\%1" >>%log% 2>>&1
md "F:\backup\copy\%1" >>%log% 2>>&1
xcopy /s/e/y/r/q/i/c "%dest%\*.*" "F:\backup\copy\%1">>%log% 2>>&1
if errorlevel 1 set err=%err%COPY%errorlevel%
goto :eof

:copylog
copy /y "%log%" G:\backup\log\latestlog.txt
goto :eof

:sendlog
REM * Uses blat.exe and body.txt as body of message:
REM * Creates a log file in d:\ftp\blatlog.txt

set logfile=g:\backup\log\blatlog.txt
set hostname=ServerName
if not "%err%"=="" set subject="Backup has failed"
if "%err%"=="" set subject="Backup was successful"
set toaddress=deliver-email-to@domain.co.uk
set attach=g:\backup\log\latestlog.txt

if exist g:\backup\scripts\blatinstalled.txt goto endif
set smtpserver=smtpserver
set fromaddress=fromaddress@domain.co.uk
g:\backup\scripts\blat -install %smtpserver% %fromaddress%
echo %date% %time% > g:\backup\scripts\blatinstalled.txt
:endif

echo Sending files....
echo %date% - %time% > %logfile%
G:\backup\scripts\blat g:\backup\scripts\blatbody.txt -subject %subject% -to %toaddress% -hostname %hostname% -log %logfile% -attach %attach%
echo Send complete.
goto :eof


Show details for ExplanationExplanation
Hide details for ExamplesExamples


Hide details for AttachmentsAttachments