I threw together a batch file this morning that goes out and backs up a users files to my hard drive (or wherever) before I wipe out the computer. But it's really ugly...was wondering if a scripting master could look at this and suggest any way to clean it up?
Code:
:: Purpose: Backs up a users profile data (only the important stuff). Use before wiping a computer
:: Requirements: 1. Run this script with a network admin account
:: 2. Required files: psexec.exe, robocopy.exe
:: History: 1.0 Initial write
:: TODO: - add local system backup ability (i.e. the script is running on the system the profile exists on)
:: - add run-once ability
:: Prep
@echo off
SETLOCAL
cls
set VERSION=1.0
title BackupUserProfile v%VERSION%
:: !!! Set your variables here !!!
set BACKUP_DESTINATION=C:\User_Backups
set FINAL_LOG_PATH=\\%COMPUTERNAME%\C$\Logs
set MAX_FILE_AGE=180
:start
:: Get user name & PC name
cls
echo.
echo User Profile Backup v%VERSION%
echo Requires robocopy.exe, psexec.exe
echo.
echo Is the users profile on [t]his computer or [a]nother computer?
echo.
:start_loop
set /P CHOICE= Enter t for (t)his computer or a for (a)nother computer:
if %CHOICE%==t echo. && echo Local backup currently not functional && echo. && goto start_loop
if %CHOICE%==a goto remote_backup
if %CHOICE%==exit goto end
:remote_backup
set TARGET=
set USER=
echo.
set /P TARGET= Enter computer name or IP where the users profile is located:
if %TARGET%==exit goto end
set /P USER= Enter full username to backup:
if %USER%==exit goto end
goto remote_backup_test
:remote_backup_test
echo.
echo Checking to see if user profile exists...
echo.
if exist \\%TARGET%\C$\Users\%USER% echo Windows Vista user profile found! && goto remote_backup_confirm
if exist "\\%TARGET%\C$\Documents and Settings\%USER%" echo. && echo Windows XP user profile found! && goto remote_backup confirm
echo ERROR: Unable to find user profile at that location. && goto remote_backup
:remote_backup_confirm
echo.
echo Target profile: %USER% on %TARGET%
echo Backup destination: %BACKUP_DESTINATION%\%TARGET%\%USER%
echo.
echo Confirm?
echo.
pause
goto step_1
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Step 1: Make sure Outlook is killed so it doesn't mess up our PST file copy ::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:step_1
echo.
echo Starting backup of %USER%'s profile at %TIME%
echo.
echo Killing Outlook on target system...
echo.
ping %TARGET% -n 2 >NUL
psexec \\%TARGET% taskkill /im outlook.exe /F
goto step_2
:::::::::::::::::::::::::::::
:: Step 2: Map share drive :: DEPRECATED
:::::::::::::::::::::::::::::
:step_2
:::::::::::::::::::::::::::::::::::::::
:: Step 3: Create backup folder tree ::
:::::::::::::::::::::::::::::::::::::::
:step_3
echo.
echo Creating backup folders for %USER%...
echo - Desktop
echo - Documents
echo - Favorites
echo - Pictures
echo - Outlook_Backup (all PSTs will be placed here)
echo Skipping (NOT backing up):
echo - Music
echo - Videos
echo.
if exist %BACKUP_DESTINATION%\%TARGET% rmdir /S /Q %BACKUP_DESTINATION%\%TARGET%
mkdir %BACKUP_DESTINATION%\%TARGET%\%USER%\Desktop %BACKUP_DESTINATION%\%TARGET%\%USER%\Documents %BACKUP_DESTINATION%\%TARGET%\%USER%\Favorites %BACKUP_DESTINATION%\%TARGET%\%USER%\Pictures %BACKUP_DESTINATION%\%TARGET%\%USER%\Outlook_Backup
::::::::::::::::::::::::::::
:: Step 4: Find PST files :: [b]This is where it gets ugly[/b]
::::::::::::::::::::::::::::
:step_4
echo.
echo Finding and collecting all PST files...
echo.
:: test for version
if exist \\%TARGET%\C$\Users goto step_4_vista
:step_4_xp
:: make Outlook_Backup folder
mkdir \\%TARGET%\C$\Documents and Settings\%USER%\Outlook_Backup"
:: Search the desktop and documents areas for random PST files and save to a list
dir /b /s /d \\%TARGET%\C$\Documents and Settings\%USER%\Desktop\*.pst >> \\%TARGET%\C$\Documents and Settings\%USER%\Outlook_Backup\extra_PST_list.txt
dir /b /s /d \\%TARGET%\C$\Documents and Settings\%USER%\My Documents\*.pst >> \\%TARGET%\C$\Documents and Settings\%USER%\Outlook_Backup\extra_PST_list.txt
:: get any extra PST's the user had laying around and dump them for pickup later
FOR /F %%i in (\\%TARGET%\C$\Documents and Settings\%USER%\Outlook_Backup\extra_PST_list.txt) do (
echo Moving %%i
move %%i \\%TARGET%\C$\Documents and Settings\%USER%\Outlook_Backup\
)
goto step_5
:step_4_vista
:: make Outlook_Backup folder
mkdir \\%TARGET%\C$\Users\%USER%\Outlook_Backup
:: Search the desktop and documents areas for random PST files and save to a list
dir /b /s /d \\%TARGET%\C$\Users\%USER%\Desktop\*.pst >> \\%TARGET%\C$\Users\%USER%\Outlook_Backup\extra_PST_list.txt
dir /b /s /d \\%TARGET%\C$\Users\%USER%\Documents\*.pst >> \\%TARGET%\C$\Users\%USER%\Outlook_Backup\extra_PST_list.txt
:: get any extra PST's the user had laying around and dump them for pickup later
FOR /F %%i in (\\%TARGET%\C$\Users\%USER%\Outlook_Backup\extra_PST_list.txt) do (
echo Moving %%i
move %%i \\%TARGET%\C$\Users\%USER%\Outlook_Backup\
)
goto step_5
:::::::::::::::::::
:: Step 5: Copy :: [b]This is where it gets even uglier[/b]
:::::::::::::::::::
:step_5
echo.
echo Starting backup, please wait...
echo.
:: make log file
if not exist \\%TARGET%\C$\Logs mkdir \\%TARGET%\C$\Logs
echo. > \\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log
:: Windows version test
if exist "\\%TARGET%\c$\Users" goto step_5_vista
:step_5_xp
echo.
echo Desktop...
robocopy "\\%TARGET%\C$\Documents and Settings\%USER%\Desktop" %BACKUP_DESTINATION%\%TARGET%\%USER%\Desktop /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /NJH /NJS /TEE
echo.
echo Documents...
robocopy "\\%TARGET%\C$\Documents and Settings\%USER%\My Documents" %BACKUP_DESTINATION%\%TARGET%\%USER%\Documents /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /NJH /NJS /TEE *.*
echo.
echo Favorites...
robocopy "\\%TARGET%\C$\Documents and Settings\%USER%\Favorites" %BACKUP_DESTINATION%\%TARGET%\%USER%\Favorites /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /NJH /NJS /TEE
echo.
echo Pictures...
robocopy "\\%TARGET%\C$\Documents and Settings\%USER%\My Documents\My Pictures" %BACKUP_DESTINATION%\%TARGET%\%USER%\Pictures /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /NJH /NJS /TEE
echo.
echo Outlook stuff, this may take a while...
:: get PST's and miscellany from the standard location
robocopy "\\%TARGET%\C$\Documents and Settings\%USER%\Local Settings\Application Data\Microsoft\Outlook" %BACKUP_DESTINATION%\%TARGET%\%USER%\Outlook_Backup /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS *.*
:: get other random PSTs that were dumped off earlier, as well as the list of miscellaneous PST files
robocopy "\\%TARGET%\C$\Documents and Settings\%USER%\Outlook_Backup" %BACKUP_DESTINATION%\%TARGET%\%USER%\Outlook_Backup /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS *t*
goto step_6_report
:step_5_vista
echo.
echo Desktop...
robocopy "\\%TARGET%\C$\Users\%USER%\Desktop" %BACKUP_DESTINATION%\%TARGET%\%USER%\Desktop /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS
echo.
echo Documents...
robocopy "\\%TARGET%\C$\Users\%USER%\Documents" %BACKUP_DESTINATION%\%TARGET%\%USER%\Documents /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS
echo.
echo Favorites...
robocopy "\\%TARGET%\C$\Users\%USER%\Favorites" %BACKUP_DESTINATION%\%TARGET%\%USER%\Favorites /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS /XD "MSN Websites" "Microsoft Websites" "Windows Live"
echo.
echo Pictures...
robocopy "\\%TARGET%\C$\Users\%USER%\Pictures" %BACKUP_DESTINATION%\%TARGET%\%USER%\Pictures /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS
echo.
echo Outlook stuff, this may take a while...
:: get PST's and miscellany from the standard location
robocopy "\\%TARGET%\C$\Users\%USER%\AppData\Local\Microsoft\Outlook" %BACKUP_DESTINATION%\%TARGET%\%USER%\Outlook_Backup /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS *.*
:: get other random PSTs that were dumped off earlier, as well as the list of miscellaneous PST files
robocopy "\\%TARGET%\C$\Users\%USER%\Outlook_Backup" %BACKUP_DESTINATION%\%TARGET%\%USER%\Outlook_Backup /Z /S /MAXAGE:%MAX_FILE_AGE% /W:7 /R:3 /LOG+:\\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log /TEE /NJH /NJS *t*
goto step_6_report
:step_6_report
:: grab the log file and put it on our machine here
echo.
echo Grabbing log file...
echo.
del \\%COMPUTERNAME%\C$\Scripts\Logs\BackupUserProfile_%TARGET%.log >NUL
copy \\%TARGET%\C$\Logs\BackupUserProfile_%TARGET%.log %FINAL_LOG_PATH%
echo.
echo User %USER% DONE! at %TIME%
ECHO Log is at %FINAL_LOG_PATH%
echo.
:end
ENDLOCAL
title %USERNAME%