Microsoft Backup (NTBackup.exe)

I created this blog so I could post about different things I’m working on, and I resolve with the new year to start doing so. I thought I’d begin by relating a difficult situation I had setting up an automated backup for my brother-in-law’s computer.

I was trying to automate the use of NTBackup.exe which is the standard Microsoft backup tool that comes with Windows XP. I wanted something that would backup the ‘My Documents’ and ‘Desktop’ folders, but was having difficulty creating the ‘.BKS’ file that NTBackup uses as a list of source directories. The trick is that it has to be a Unicode text file and not you basic ASCII. You’ll see that below in how that file is created.

Here’s the script:

@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
rem SET date=%mm%%dd%%yyyy%
SET datepart=%yyyy%-%mm%-%dd%

SET backupdir=E:\Backups
SET backupfile=%backupdir%\%USERNAME%'s Backup.bkf
SET logdir=%HOMEDRIVE%%HOMEPATH%\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data
SET logfile=%backupdir%\Backup.log
SET selectionfile=Selections.bks
SET backupoptions=backup "@%selectionfile%" /a /d "Set created By NTBackup.bat" /v:no /r:no /rs:no /hc:off /m differential /j "Backup for %USERNAME%" /l:s /f "%backupfile%"

cls
echo.
echo.Nathan's Easy Backup
echo.--------------------
echo.
echo.This will backup the following folders:
echo.  %HOMEDRIVE%%HOMEPATH%\Desktop
echo.  %HOMEDRIVE%%HOMEPATH%\My Documents
echo.
echo.To the following destination:
echo.  %backupfile%
echo.
echo.And creating a log file:
echo.  %logfile%
echo.
echo.
echo.NOTE: If you already ran a backup today, then it will be overwritten
echo.with the current contents of the source folders.
echo.
echo.
echo.To abort, press Ctrl+C now or
pause

md "%backupdir%" > NUL
copy /V /Y NTBACKUP.MSI "%backupdir%"

rem Selections file MUST be in Unicode!!
cmd /U /C "echo.C:\BackupMaker > "%selectionfile%""
cmd /U /C "echo.%HOMEDRIVE%%HOMEPATH%\Desktop >> "%selectionfile%""
cmd /U /C "echo.%HOMEDRIVE%%HOMEPATH%\My Documents >> "%selectionfile%""

rem Erase old backup logs so the only one remaining is the one for our current backup
erase "%logdir%\*.*" /q

echo.
echo.
echo.The backup will start when you press a key. Please do not close this window
echo.before the backup has completed.
echo.
pause
ntbackup %backupoptions%

erase /F /Q "%logfile%"
ren "%logdir%\backup*.log" "backup.log"
move "%logdir%\backup.log" "%logfile%"

pause

When creating a simple backup, I also made a script for the popular ‘Robocopy.exe’ utility found in the ‘Windows Server Resource Kit Tools’. It does the same thing, but preserves a mirror of the source rather than creating a proprietary data file. Note that it excludes exe and dll files since these are usually downloaded or compiled files that are considered temporary.

Here’s RoboBackup.bat

@echo off
FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
rem SET date=%mm%%dd%%yyyy%
SET datepart=%yyyy%-%mm%-%dd%

SET backupdir=E:\%USERNAME%'s Backups\%datepart%
SET logfile=%backupdir%\Backup.log
SET backupoptions=/MIR /XF "thumbs.db" "*.exe" "*.dll" /LOG+:"%logfile%" /NP /TEE

cls
echo.
echo.Nathan's Easy Backup
echo.--------------------
echo.
echo.This will backup the following folders:
echo.  %HOMEDRIVE%%HOMEPATH%\Desktop
echo.  %HOMEDRIVE%%HOMEPATH%\My Documents
echo.
echo.To the following destination:
echo.  %backupdir%
echo.
echo.And creating a log file:
echo.  %logfile%
echo.
echo.
echo.NOTE: If you already ran a backup today, then it will be overwritten
echo.with the current contents of the source folders.
echo.
echo.
echo.To abort, press Ctrl+C now or
pause

md "%backupdir%" > NUL
erase "%logfile%" > NUL

md "%backupdir%\Desktop" > NUL
robocopy "%HOMEDRIVE%%HOMEPATH%\Desktop" "%backupdir%\Desktop" %backupoptions%

md "%backupdir%\My Documents" > NUL
robocopy "%HOMEDRIVE%%HOMEPATH%\My Documents" "%backupdir%\My Documents" %backupoptions%

Leave a Reply

Your email address will not be published. Required fields are marked *