NT Backup script

I was asked to create a script that would be an easy double click backup solution for sales people who are on the road. The following is a batch file that will do a differential backup of which ever folder you specify in the script. You only need to change the destination drive and source. It will look for a full backup, if one isn’t found, it will do one and do differential backups there after.

@echo off

:Variables – Change drive letter to match your backup drive
set destdrive=F:
set source=C:\Documents and Settings\cpratt

:You shouldn’t need to change any of these
set destination=%destdrive%\Backup
set server=%computername%
set textlogpath=%destdrive%\Backup\%computername%-Backup.txt
set archivepath=%destdrive%\Backup\OldLogs

:Set Time

for /F “tokens=1-4 delims=/ ” %%i in (‘date /t’) do (
set Month=%%j
set Day=%%k
set Year=%%l
set Date=%%j-%%k-%%l
set dirdate=%%j%%k%%l
)

for /f “Tokens=1-2 delims=/ ” %%i in (‘time /t’) do (
set tm=%%i
set ampm=%%j
)

echo.
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo Backup.bat v1
echo.
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo.

set logpath=C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data
if not exist “%archivepath%” md “%archivepath%” & echo “Archive Path did not exist, so it was created.” >> %textlogpath%
if exist “%textlogpath%” del “%textlogpath%” & echo “Previous %textlogpath% existed, so it was deleted.” >> %textlogpath%
if exist “%logpath%\*.log” del “%logpath%\*.log” & echo “Previous ntbackup logfile existed, so it was deleted.” >> %textlogpath%

:Backup Time

echo Backup started at %tm% %ampm% on %Year%-%Month%-%Day% into “%destination%” >> %textlogpath%
echo %server% backup starts %tm% %ampm% on %Year%-%Month%-%Day% into “%destination%”

:Check for full backup
if exist “%destination%\%server%-FullBackup.bkf” echo “Full backup exists” & c:\windows\system32\ntbackup.exe backup “@C:\Windows\backupdiff.bks” /a /d “%server%-Backup” /v:no /r:no /rs:no /hc:off /m differential /j “%server%-Backup” /l:s /f “%destination%\%server%-Differential.bkf” >> %textlogpath%
if not exist “%destination%\%server%-FullBackup.bkf” echo “Full backup does not exist” & c:\windows\system32\ntbackup.exe backup “@C:\Windows\backupdiff.bks” /a /d “%server%-Backup” /v:no /r:no /rs:no /hc:off /m differential /j “%server%-Backup” /l:s /f “%destination%\%server%-FullBackup.bkf” >> %textlogpath%

:RenameLogs

if exist “%logpath%\*.log” ren “%logpath%\*.log” %server%-Backup.log
if exist “%logpath%\%server%-Backup.log” copy “%logpath%\%server%-Backup.log” “%archivepath%” /y

echo NTBackup.exe .log file archived at “%archivepath%\%note%-%server%-%Year%-%Month%-%Day%.log” >> %textlogpath%
echo alcBackup.bat .txt file archived at “%archivepath%\%Year%-%Month%-%Day%-%server%-%note%.txt” >> “%textlogpath%”

:Archive
if exist “%archivepath%\templog.txt” del “%archivepath%\templog.txt”
if exist %textlogpath% copy “%textlogpath%” “%archivepath%\templog.txt”
if exist “%archivepath%\%Year%-%Month%-%Day%-%server%-%note%.txt” del %archivepath%\%Year%-%Month%-%Day%-%server%-%note%.txt

if exist %archivepath%\templog.txt ren %archivepath%\templog.txt “%Year%-%Month%-%Day%-%server%-%note%.txt”

if exist “%archivepath%\%note%-%server%-%Year%-%Month%-%Day%.log” del “%archivepath%\%note%-%server%-%Year%-%Month%-%Day%.log”

if exist “%archivepath%\%server%-Backup.log” ren “%archivepath%\%server%-Backup.log” “%note%-%server%-%Year%-%Month%-%Day%.log”

if exist “%archivepath%\%server%-Backup.log” del “%archivepath%\%server%-Backup.log”
if exist “%logpath%\%server%-Backup.log” del “%logpath%\%server%-Backup.log”

if exist “%textlogpath%” del “%textlogpath%”

goto eof
:eof

Wow. It's Quiet Here...

Be the first to start the conversation!

Leave a Reply:

Gravatar Image

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>