Backup Batch File

I was recently asked me to write a script to back up certain files for our users. Here is what I came up with.

@echo off

REM Backup Batch File

REM Created and Modified by Alex Lane

REM This script is designed to back up user files.

REM It current has the following features:
REM Number Choice error correction.
REM OS Detection. (Will only work on Windows XP, if another OS is detected, it will either run custom code or give an incompatability error.
REM Will backup (and restore): Microsoft Office custom dictionary (including outlook dictionary), Outlook Autocomplete, Outlook Signatures, Internet Explorer favourites, User “My Documents”, User Desktop and will list all programs installed.
REM It will also backup and restore Firefox Profile (including all plugins).
REM This script is also designed to backup user defined Wallpaper and Screensaver. However it is not designed to restore these due to probable lack of local admin rights blocking access to write to the registry.

COLOR 0e

:: variables
set drive=H:\Private\ITBackup\
set backupcmd=xcopy /s /c /d /e /h /i /r /y /k /v /z

:FindOS
ver | find “2003” > nul
if %ERRORLEVEL% == 0 goto ver_2003

ver | find “XP” > nul
if %ERRORLEVEL% == 0 goto ver_xp

ver | find “2000” > nul
if %ERRORLEVEL% == 0 goto ver_2000

ver | find “NT” > nul
if %ERRORLEVEL% == 0 goto ver_nt

if not exist %SystemRoot%\system32\systeminfo.exe goto warnthenexit

systeminfo | find “OS Name” > %TEMP%\osname.txt
FOR /F “usebackq delims=: tokens=2” %%i IN (%TEMP%\osname.txt) DO set vers=%%i

echo %vers% | find “Windows 7” > nul
if %ERRORLEVEL% == 0 goto ver_7

echo %vers% | find “Windows Server 2008” > nul
if %ERRORLEVEL% == 0 goto ver_2008

echo %vers% | find “Windows Vista” > nul
if %ERRORLEVEL% == 0 goto ver_vista

goto warnthenexit

:ver_xp
cls
echo You have Windows XP
echo.
:Start
echo ### Backup drive set as: %drive%
echo.
echo Press 1 to Backup Files.
echo.
echo Press 2 to Restore Files.
echo.
echo Press 3 to Backup Wallpaper
echo.
echo Press 4 to Backup Firefox Profile.
echo.
echo Press 5 to Restore Firefox Profile.
echo.
echo Press 6 to Quit.
echo.

set /p userinp=Choice #:
set userinp=%userinp:~0,1%
if “%userinp%”==”1” goto 1
if “%userinp%”==”2” goto 2
if “%userinp%”==”3” goto 3
if “%userinp%”==”4” goto 4
if “%userinp%”==”5” goto 5
if “%userinp%”==”6” goto 6
echo ‘%userinp%’ is not a valid choice, press any key to try again.

pause>nul

goto start

:1

echo ### Backing up files…
echo ### Setting backup drive as: %drive%

echo ### Backing up Custom Dictionary
%backupcmd% “%USERPROFILE%\Application Data\microsoft\proof” “%drive%\Outlookbackup\Proof”

echo ### Backing up Outlook Autocomplete
%backupcmd% “%USERPROFILE%\Application Data\microsoft\outlook” “%drive%\Outlookbackup\Outlook”

echo ### Backing up Outlook Signatures
%backupcmd% “%USERPROFILE%\Application Data\microsoft\signatures” “%drive%\Outlookbackup\Signatures”

echo ### Backing up Internet Explorer Favourites
%backupcmd% “%USERPROFILE%\Favorites” “%drive%\Favorites”

REM ### You now have the final countdown stuck in your head

echo ### Backing up My Documents
%backupcmd% “%USERPROFILE%\My Documents” “%drive%\My Documents”

echo ### Backing up Desktop
%backupcmd% “%USERPROFILE%\Desktop” “%drive%\Desktop”

echo ### Listing all programs installed on the computer

regedit /e %temp%\regexport.txt “HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall”

find “DisplayName” %temp%\regexport.txt >%temp%\regprogs.txt

for /f “tokens=2 delims==” %%a in (%temp%\regprogs.txt) do echo %%~a >>”%drive%\installedprogs.txt”

del %temp%\regexport.txt
del %temp%\regprogs.txt

goto endbackup

:2

echo ### Restoring files…
echo ### Restoring files from %drive%

echo ### Restoring Custom Dictionary
%backupcmd% “%drive%\Outlookbackup\Proof” “%USERPROFILE%\Application Data\microsoft\proof”

echo ### Restoring Outlook Autocomplete
%backupcmd% “%drive%\Outlookbackup\Outlook” “%USERPROFILE%\Application Data\microsoft\outlook”

REM ### You have lost the game

echo ### Restoring Outlook Signatures
%backupcmd% “%drive%\Outlookbackup\Signatures” “%USERPROFILE%\Application Data\microsoft\signatures”

echo ### Restoring Internet Explorer Favourites
%backupcmd% “%drive%\Favorites” “%USERPROFILE%\Favorites”

echo ### Restoring My Documents
%backupcmd% “%drive%\My Documents” “%USERPROFILE%\My Documents”

echo ### Restoring Desktop
%backupcmd% “%drive%\Desktop” “%USERPROFILE%\Desktop”

goto endrestore

:3
call:ReadDesktopRegistry Wallpaper
call:ReadDesktopRegistry SCRNSAVE.EXE
call:ReadDesktopRegistry WallpaperStyle
call:ReadDesktopRegistry ScreenSaveActive
call:ReadDesktopRegistry ScreenSaveTimeOut

xcopy /c /h /i /r /y /k /v %Wallpaper% %drive%\Wallpaper\
xcopy /c /h /i /r /y /k /v %SCRNSAVE.EXE% %drive%\Wallpaper\Screensaver\

goto endwallpaper

:ReadDesktopRegistry
FOR /F “delims=” %%A in (‘reg query “HKCU\Control Panel\Desktop” /v “%~1″‘) DO (SET RegRead=%%A)
FOR /F “tokens=3” %%A IN (“%RegRead%” ) DO (SET “%~1=%%A” )
SET RegRead=””
goto:eof

:4
echo ### Backing up Firefox Profile
%backupcmd% “%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles” “%drive%\Firefox\Profiles”

goto endbackup

:5

echo ### Restoring Firefox Profile
%backupcmd% “%drive%\Firefox\Profiles” “%USERPROFILE%\Application Data\Mozilla\Firefox\Profiles”

goto endrestore

:ver_2000
echo You have Windows 2000
echo.
:Run Windows 2000 specific commands here.
goto warnthenexit

:ver_nt
echo You have Windows NT
:Run Windows NT specific commands here.
goto warnthenexit

:ver_7
echo You have Windows 7
:Run Windows 7 specific commands here.
goto warnthenexit

:ver_2008
echo You have Windows Server 2008
:Run Windows Server 2008 specific commands here.
goto warnthenexit

:ver_vista
echo You have Windows Vista
:Run Windows Vista specific commands here.
goto warnthenexit

:ver_2003
echo You have Windows Server 2003
:Run Windows Server 2003 specific commands here.
goto warnthenexit

:warnthenexit
echo.
echo I’m Sorry, this script is not compatible with this OS.
pause > nul
goto 6

:endbackup
cls
echo Backup Complete!
echo.
echo.
echo.
echo Thank you for using the IT Department’s Profile autobackup facility
echo.
echo Your Backup is located in %drive%
echo Please press any key to go back to menu
pause>nul
goto start

:endrestore
cls
echo Restore Complete!
echo.
echo.
echo.
echo Thank you for using the IT Department’s Profile autobackup facility
echo.
echo Your Backup has been restored from %drive%
echo Please press any key to go back to menu
pause>nul
goto start

:endwallpaper
cls
echo Wallpaper backed up to %drive%Wallpaper\
echo.
echo Screensaver backed up to %drive%Wallpaper\Screensaver\
echo.
echo Thank you for using the IT Department’s Profile autobackup facility
echo.
echo Please press any key to go back to menu
pause>nul
goto start

:6

cls
echo Thank you for using the IT Department’s Profile autobackup facility
echo.
echo No options have been actioned.
echo.
echo Please press any key to exit
pause>nul
exit

This script was hard work.
Here is the rundown:
It will give you 6 options:
  1. Backup Files
  2. Restore Files
  3. Backup Wallpaper
  4. Backup Firefox Profile
  5. Restore Firefox Profile
  6. Quit

You notice that there is no “restore wallpaper” option. The reason for this is that the majority of our users do not have admin access, thus they cannot write to the registry.

The things I love about this script is the fact that it will detect your OS, and run code that is OS specific. Especially as XP has some different code that can be run than say….Windows 7.

Also, it has standard error correction and reads dynamic values for the wallpaper and screensaver (useful as the majority of users will have custom wallpapers/screensaver names).

To summarise, this script allowed me to revisit the old school methods of batch scripting, and allowed me to find new methods to back up profiles, my documents, outlook signatures, Internet Explorer Favourites, microsoft office autocomplete and custom dictionaries. The usual things that users won’t remember to back up, or in some cases, they plain don’t know how.

I hope this post has at least sparked an interest in batch files for you, or given you an idea with what you wish to do with your own script!

Advertisements

7 thoughts on “Backup Batch File

  1. Hello! I’ve been following your web site for a long time now and finally got the courage to go ahead and give you a shout out from Kingwood Texas! Just wanted to mention keep up the excellent job!

  2. Amazing script! Just two questions..we’re doing windows Xp to 7 upgrades here and I’ve used robocopy with great success but i can’t figure out a way to export their user defined wallpaper, I know it’s stored in a key and I see you have it as part of the script but I can’t run/dont understand what it does..Anyway you can help ?

    • Thank you! :)

      The Wallpaper one was an interesting problem I had. Note that you can only back it up, not restore it (as you’d need local admin permissions).

      In the script you have the following lines:

      call:ReadDesktopRegistry Wallpaper
      call:ReadDesktopRegistry SCRNSAVE.EXE
      call:ReadDesktopRegistry WallpaperStyle
      call:ReadDesktopRegistry ScreenSaveActive
      call:ReadDesktopRegistry ScreenSaveTimeOut

      xcopy /c /h /i /r /y /k /v %Wallpaper% %drive%\Wallpaper\
      xcopy /c /h /i /r /y /k /v %SCRNSAVE.EXE% %drive%\Wallpaper\Screensaver\

      The “call” portion, calls a bit of code just underneath it:

      :ReadDesktopRegistry
      FOR /F "delims=" %%A in ('reg query "HKCU\Control Panel\Desktop" /v "%~1"') DO (SET RegRead=%%A)
      FOR /F "tokens=3" %%A IN ("%RegRead%" ) DO (SET "%~1=%%A" )
      SET RegRead=""
      goto:eof

      It calls each of those instructions above, stores it in the variable %regread%, and saves some of the settings (specifically the paths in %wallpaper% and %SCRNSAVE.EXE%), in the %drive% variable (which I set at the beginning of the batch file).

      But remember, the issue you’re going to have, if you haven’t set local admin access for them, is restoring the files – as it would require access to the registry.

      Hope that helps a bit!

      • Brilliant ..silly question. this can only be run from within the user, correct? I have a program called regfileexport.exe that i use :
        “%~dp0RegFileExport.exe” “C:\Documents and settings\%_src%\NTUSER.DAT” “%_DESTINATION%\mappeddrives.reg” “HKEY_CURRENT_USER\NETWORK”

        To export mapped drives.. Do you have any idea if that would work so I dont need to be logged in as the user? I know I’m throwing in a wrench but often times I remotely back up multiple users using Remote Desktop and I can’t log in as them prior to the upgrade..

      • Hi Assad,

        For most of it, I’d recommend the batch file being run as the local user, especially with all of the variables in it (it was originally designed so that users could run the file, back up their files to the network drive, and check for themselves that everything they wanted was backed up).

        From the info I’ve gotten so far, yes and no. If the user has permanently mapped drives, then you *should* be able to get the info from HKEYCU\Network, but in most domain cases, drives are unmapped after the user logs off, and re-mapped when they log back in.

        If you wish to get a list of mapped drives (for the local user), you can always use the “Net use” command (e.g. net use > %drive%\mappeddrives.txt), that will export all of their currently mapped drives. There are also a few other resources that you can use to eliminate the use of an external program (such as http://gallery.technet.microsoft.com/scriptcenter/3dd6af3e-edfa-4581-bc35-805314f26bb8). I hope that helps!

  3. Oh the second question is after I back up their .pst I was looking for a way to import them (as a second part of the script) to outlook..do you know of anything that lets you import multiple outlook .psts via command line?

    • Unfortunately ANOTHER problem I ran into. The restore section of the PST only works once they initialise their Outlook profile to create the folders. There are some programs out there that will do it, but as more and more companies are moving into web based email (Office365 for example), this becomes less and less relevant.

      I was thinking of updating the batch file to find and include any attached OST’s to the backup, as if users lose those offline archives, they’ll go nuts at you (regardless how many times you’ve told them to store it on the network/not use it at all).

      The other issue, is that the PST files are name based. Thus, if the PST file is named bob.pst, and they name their new profile Bob1, then the restore will technically fail as Outlook will be looking for Bob1.pst instead of Bob.pst

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s