System Backup batch file (works in Windows 7) with network and home drive check

This script is designed to help users back up their profiles.

It will backup and restore the following:

  1. Microsoft Office custom dictionary (including outlook dictionary)
  2. Outlook Autocomplete
  3. Outlook Signatures
  4. Internet Explorer favourites
  5. My Documents
  6. Windows 7 Jumplists
  7. User Desktop
  8. List all programs installed

====================================

@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 currently has the following features:
REM Number Choice error correction.
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 /c /d /e /h /i /r /y /k /v /z
set dircheck=H:\Private

:NetworkCheck
PING -n 1 %logonserver:~2,99%|find "Reply from " >NUL
IF NOT ERRORLEVEL 1 goto HomeDriveCheck
IF     ERRORLEVEL 1 goto Netfail

:HomeDriveCheck
dir %dircheck% >nul 2>nul
if errorlevel 1 (
goto DirCheckBad
)

dir %dircheck% >nul 2>nul
if not errorlevel 1 (
goto Start
)

:Netfail
echo You do not have access to the network. Please check your connection and run this script again.
echo.
echo Please press any key to try again.
pause>nul
cls
IF NOT ERRORLEVEL 1 goto NetworkCheck
IF     ERRORLEVEL 1 echo Network access could not be established. Please contact IT.
echo.
echo Press any key to exit
pause>nul
exit

:DirCheckBad
echo Your Home Drive does not seem to exist. Please check that your H:\ drive exists and run this script again.
echo.
echo Please press any key to try again.
pause>nul
cls
IF NOT ERRORLEVEL 1 goto HomeDriveCheck
IF     ERRORLEVEL 1 echo Your H:\ does not seem to exist. Please contact IT.
echo.
echo Press any key to exit
pause>nul
exit

:Start
cls
echo Network and Home Drive access confirmed
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
dir %USERPROFILE%\My Documents >nul 2>nul
if errorlevel 1 (
%backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents"
)

dir %USERPROFILE%\My Documents >nul 2>nul
if not errorlevel 1 (
%backupcmd% "%USERPROFILE%\Documents" "%drive%\My Documents"
)

echo ### Backing up Jump Lists
%backupcmd% "%APPDATA%\Microsoft\Windows\Recent\AutomaticDestinations" "%drive%\Jump-Lists-Backup\AutomaticDestinations"
%backupcmd% "%APPDATA%\Microsoft\Windows\Recent\CustomDestinations" "%drive%\Jump-Lists-Backup\CustomDestinations"

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
dir %USERPROFILE%\My Documents >nul 2>nul
if errorlevel 1 (
%backupcmd% "%drive%\My Documents" "%USERPROFILE%\My Documents"
)

dir %dircheck% >nul 2>nul
if not errorlevel 1 (
%backupcmd% "%drive%\My Documents" "%USERPROFILE%\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

: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.
echo Things that have been backed up:
echo.
DIR %drive% /A:D /O /B
echo.
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.
echo Things that have been restored:
echo.
DIR %drive% /A:D /O /B
echo.
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 Please press any key to exit
pause>nul
exit
Advertisements

17 thoughts on “System Backup batch file (works in Windows 7) with network and home drive check

  1. Thanks for this, but getting an error with:

    C:\Users\pullnadiva>PING -n 1 %logonserver:~2,99%|find “Reply from ” >NUL
    FIND: Parameter format not correct

    Windows 7 Pro

    • Heya,

      If you’re not on a domain, then check out the following lines:

      :NetworkCheck
      PING -n 1 %logonserver:~2,99%|find "Reply from " >NUL
      IF NOT ERRORLEVEL 1 goto HomeDriveCheck
      IF ERRORLEVEL 1 goto Netfail

      :HomeDriveCheck
      dir %dircheck% >nul 2>nul
      if errorlevel 1 (
      goto DirCheckBad
      )

      dir %dircheck% >nul 2>nul
      if not errorlevel 1 (
      goto Start
      )

      As they may not apply to your scenario. So if not, feel free to remove them! I had to do this to be able to ping the domain controller, as you normally are not able to (due to %logonserver% gives “\\logonserver” instead of “logonserver”, which fails ping). Hope that helps! If not, please reply and I’ll do what I can :)

    • No problems! :)

      If you go to command prompt and type in %logonserver%, what do you get? It seems that the find command isn’t finding the results “reply from”, which would indicate that the ping isn’t working for whatever reason.

      So if we try %logonserver% first, see the result. If you get an acceptable result, try pinging it (obviously without the \\). What results do you get?

  2. Ok, so i get the name of the domain controller i am currently connected to. When i ping it i get a response ;)

    So do i need to edit:
    PING -n 1 %logonserver:~2,99%|find “Reply from ” >NUL

    • Hmm very odd! The PING -n 1 %logonserver:~2,99%|find “Reply from ” >NUL section basically pings the logonserver, but the command %logonserver% brings up the \\logonserver, the command removes the \\, then pings the results, finding the “reply from” section. Is your W7 in English? If not, you may need to edit the “reply from ” section.

      • OK, I found out what the problem is. WordPress seems to render quotes as smartquotes rather than curly quotes. Please go through the quotes and retype them. I’m working on a way to sort this out globally on wordpress on my articles.
        Edit: Please try now!

  3. Thanks Alex for the help again, but sadly still the same issue :(

    FIND: Parameter format not correct
    Your Home Drive does not seem to exist. Please check that your H:\ drive exists
    and run this script again.

    Please press any key to try again.

    Edit:
    Just to add to that, i actually am mapped to a drive called H: which is like a personal drive that only i have access to.

    • Heya,

      Within the H:\ drive, did you put in a “Private” folder? E.g. H:\Private (as that’s what the script is looking at in %dircheck% (set dircheck=H:\Private))

  4. Alex, i can’t believe how stupid i am, that worked perfectly. Thank you very much for all the help and your patience with me ;).

    Now i just want to edit it to suit my needs for backing up:
    DESKTOP
    IE FAVOURITES
    DOCUMENTS
    PICTURES
    MUSIC
    VIDEOS
    FOLDER LINKS
    EMAIL SIGNATURE
    OUTLOOK AUTOCOMPLETE

    So i will probably be annoying you again soon ;)

    Keep up the great work, it is extremely helpful.

    • Haha, no problems! Took me a while to figure out the SmartQuotes as well, much less things like an additional folder!

      Remember that it’s going to be a pig working between XP and 7, due to the location of the “My Documents” folder (in 7 it’s a library, so all the subfolders you’d expect to find (My Music, My Videos etc) are all separate folders, and not subfolders of “My Documents” like they are in XP. That’s what the line “ver |find ” 6.”>nul && (set pname=My Documents) || (set pname=Documents)” tries to resolve.

  5. Hi Alex,

    Just noticed the *Restore Outlook Signatures* needed a slight bit of tweaking to work right with Win7 Office 2010. To restore to the correct path the code i used is:

    echo ### Restoring Outlook Signatures
    %backupcmd% “%drive%\Outlookbackup\Signatures” “%USERPROFILE%\AppData\Roaming\Microsoft\Signatures”

    • Ah thank you!

      We’re currently on ’07, so that would explain why I haven’t seen the bug!

      It might even be worthwhile changing it to a variable such as “%APPDATA%\Microsoft\Signatures” instead of putting “%USERPROFILE%\Application Data\microsoft\signatures”.

      More variables: http://ss64.com/nt/syntax-variables.html

      Although it is very tempting to backup %HOMEPATH% as well…

      I’ll give it a test on Monday.

    • Hi Billy,

      You would need to change location of where things are getting backed up to, and maybe remove (or change) the checks for network drive connectivity.

      After this, delete everything after “goto endbackup :2”

      Easiest thing to do would be to delete everything before “:1” and everything after “:2”, that way you have the bulk of the script without the restore functionality, without wallpaper backup, etc.

      Of course, you can also include the firefox backup section of the script, but in all fairness? I’d say use the Firefox sync ability which does everything including your plugins.

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