Solved Need backup batch for Vista

November 26, 2011 at 14:36:59
Specs: Windows Vista
I need separate backup batch scripts for XP and Vista/Win7. Given any one of the OSes, I think I can modify the script to accommodate the other.

My goal is a script that will backup specific OS directories to an external drive for each non-default user account (including Owner). I'd like the script to either scan the OS for non-default account names, or read each account name from an array in the script, or read the account names from an external text file, I want to save the following OS directories

desktop
documents
favorites
music
pictures
videos

Any assistance with such a script will be greatly appreciated. Thanks.


See More: Need backup batch for Vista

Report •


#1
November 27, 2011 at 14:59:18
✔ Best Answer
You'll need robocopy.exe for this script. You can get it here:

http://www.microsoft.com/download/e...

This script will try to identify the os version based on the file structure. I realize there are better ways of doing that, but I wrote this in about 2 minutes. Once it identifies the os, it runs a series of robocopy commands to backup the directories you wanted to drive f:. This script is not tested, and you will have to tweak it to your specifications.

@echo off

setlocal

REM Making sure robocopy.exe exists in %systemroot%\system32
REM ***IMPORTANT*** robocopy.exe must be stored in the same directory as this script

if exist %systemroot%\system32\robocopy.exe goto start
copy robocopy.exe %systemroot%\system32\

:start

if exist "c:\documents and settings\" goto startxp
if exist "c:\users" goto startvis7

:startxp

dir /b "c:\documents and settings\" > users.tmp

for /f (tokens=*) %%a in "users.tmp" do (
if /I "%%a" equ "All Users" goto eof
if /I "%%a" equ "Default" goto eof
if /I "%%a" equ "DefaultAppPool" goto eof
if /I "%%a" equ "Default User" goto eof
if /I "%%a" equ "Public" goto eof
robocopy "c:\documents and settings\%%a\desktop" f:\%%a\desktop *.* /E /ZB /copyall /TEE /ETA
robocopy "c:\documents and settings\%%a\documents" f:\%%a\documents *.* /E /ZB /copyall /TEE /ETA
robocopy "c:\documents and settings\%%a\favorites" f:\%%a\favorites *.* /E /ZB /copyall /TEE /ETA
robocopy "c:\documents and settings\%%a\music" f:\%%a\music *.* /E /ZB /copyall /TEE /ETA
robocopy "c:\documents and settings\%%a\pictures" f:\%%a\pictures *.* /E /ZB /copyall /TEE /ETA
robocopy "c:\documents and settings\%%a\videos" f:\%%a\videos *.* /E /ZB /copyall /TEE /ETA

goto eof

:startvis7
dir /b c:\users > users.tmp

for /f (tokens=*) %%a in "users.tmp" do (
if /I "%%a" equ "All Users" goto eof
if /I "%%a" equ "Default" goto eof
if /I "%%a" equ "DefaultAppPool" goto eof
if /I "%%a" equ "Default User" goto eof
if /I "%%a" equ "Public" goto eof
robocopy c:\users\%%a\desktop f:\%%a\desktop *.* /E /ZB /copyall /TEE /ETA
robocopy c:\users\%%a\documents f:\%%a\documents *.* /E /ZB /copyall /TEE /ETA
robocopy c:\users\%%a\favorites f:\%%a\favorites *.* /E /ZB /copyall /TEE /ETA
robocopy c:\users\%%a\music f:\%%a\music *.* /E /ZB /copyall /TEE /ETA
robocopy c:\users\%%a\pictures f:\%%a\pictures *.* /E /ZB /copyall /TEE /ETA
robocopy c:\users\%%a\videos f:\%%a\videos *.* /E /ZB /copyall /TEE /ETA

:eof


Report •

#2
November 27, 2011 at 16:05:26
Thank you for your prompt response, jowah. Your solution looks promising. However, I'd like to discuss some modifications to make it more specific to my needs.

1.) The reason I wanted to script my backup was to increase my success in case the OS was unstable or infected to the point that installing 3rd party software might be a problem. I'd like to know if I can modify your script to use xcopy instead of robocopy.

2.) Since you're creating a user.tmp file to hold user accounts, I'd like to know if I it's OK to simply create user.tmp myself and populate it with the user accounts whose data I want to save.

3.) If #2 is feasible, could I delete the conditional statements that check for default accounts?

Your script appears to be everything I need, and I especially like that it works for XP, Vista and Win7. Thank you so much.


Report •

#3
November 27, 2011 at 20:34:21
Hello mel2000,

1.) Yes you can substitute xcopy for robocopy. The syntax is different so you'll have to modify those statements, but xcopy should work just as well as robocopy.

2.) Yes you can manually create users.tmp. I put a "REM " (without the quotes, like lines 5 & 6) in front of the lines you don't need. That way you can just remove the REM if you ever decide to use those lines again.

3.) Yes. See the new script below. Now all you need to do is create the correct syntax xcopy commands and place them at the indicated points.

@echo off

setlocal

REM Making sure robocopy.exe exists in %systemroot%\system32
REM ***IMPORTANT*** robocopy.exe must be stored in the same directory as this script

REM if exist %systemroot%\system32\robocopy.exe goto start
REM copy robocopy.exe %systemroot%\system32\

REM :start

if exist "c:\documents and settings\" goto startxp
if exist "c:\users" goto startvis7

:startxp

REM dir /b "c:\documents and settings\" > users.tmp

for /f (tokens=*) %%a in "users.tmp" do (
REM if /I "%%a" equ "All Users" goto eof
REM if /I "%%a" equ "Default" goto eof
REM if /I "%%a" equ "DefaultAppPool" goto eof
REM if /I "%%a" equ "Default User" goto eof
REM if /I "%%a" equ "Public" goto eof
REM robocopy "c:\documents and settings\%%a\desktop" f:\%%a\desktop *.* /E /ZB /copyall /TEE /ETA
REM robocopy "c:\documents and settings\%%a\my documents" f:\%%a\documents *.* /E /ZB /copyall /TEE /ETA
REM robocopy "c:\documents and settings\%%a\favorites" f:\%%a\favorites *.* /E /ZB /copyall /TEE /ETA
REM robocopy "c:\documents and settings\%%a\music" f:\%%a\music *.* /E /ZB /copyall /TEE /ETA
REM robocopy "c:\documents and settings\%%a\pictures" f:\%%a\pictures *.* /E /ZB /copyall /TEE /ETA
REM robocopy "c:\documents and settings\%%a\videos" f:\%%a\videos *.* /E /ZB /copyall /TEE /ETA
REM *** PLACE NEW XP XCOPY COMMANDS BELOW THIS LINE***

goto eof

:startvis7
REM dir /b c:\users > users.tmp

for /f (tokens=*) %%a in "users.tmp" do (
REM if /I "%%a" equ "All Users" goto eof
REM if /I "%%a" equ "Default" goto eof
REM if /I "%%a" equ "DefaultAppPool" goto eof
REM if /I "%%a" equ "Default User" goto eof
REM if /I "%%a" equ "Public" goto eof
REM robocopy c:\users\%%a\desktop f:\%%a\desktop *.* /E /ZB /copyall /TEE /ETA
REM robocopy c:\users\%%a\documents f:\%%a\documents *.* /E /ZB /copyall /TEE /ETA
REM robocopy c:\users\%%a\favorites f:\%%a\favorites *.* /E /ZB /copyall /TEE /ETA
REM robocopy c:\users\%%a\music f:\%%a\music *.* /E /ZB /copyall /TEE /ETA
REM robocopy c:\users\%%a\pictures f:\%%a\pictures *.* /E /ZB /copyall /TEE /ETA
REM robocopy c:\users\%%a\videos f:\%%a\videos *.* /E /ZB /copyall /TEE /ETA
REM *** PLACE NEW VISTA/7 XCOPY COMMANDS BELOW THIS LINE***

:eof
endlocal


Report •

Related Solutions

#4
November 30, 2011 at 17:14:09
Here is my modified backup batch file. Unfortunately, I get the following error when ran:

"*) was unexpected at this time."
I assume it is referencing the following line:
"for /f (tokens=*) %%a in "users.tmp" do ("

I created a file, users.tmp containing only the word, Owner. It is located in the same directory as the script. I don't understand why I'm getting the error and would like someone to parse my batch code for answers. Thanks.

**********************
@echo off

setlocal
set drive=O:\Backups\test
set backupcmd=xcopy /s /c /d /e /h /i /r /y

if exist "c:\documents and settings\" goto startxp
if exist "c:\users" goto startvis7

:startxp

for /f (tokens=*) %%a in "users.tmp" do (
echo ### Backing up desktop...
%backupcmd% "c:\documents and settings\%%a\desktop" "%drive%\%%a\desktop"
echo ### Backing up Favorites...
%backupcmd% "c:\documents and settings\%%a\favorites" "%drive%\%%a\favorites"
echo ### Backing up my documents...
%backupcmd% "c:\documents and settings\%%a\my documents" "%drive%\%%a\my documents"
echo ### Backing up music...
%backupcmd% "c:\documents and settings\%%a\music" "%drive%\%%a\music"
echo ### Backing up pictures...
%backupcmd% "c:\documents and settings\%%a\pictures" "%drive%\%%a\pictures"
echo ### Backing up videos...
%backupcmd% "c:\documents and settings\%%a\videos" "%drive%\%%a\videos"

goto eof

:startvis7

for /f (tokens=*) %%a in "users.tmp" do (
echo ### Backing up desktop...
%backupcmd% "c:\users\%%a\desktop" "%drive%\%%a\desktop"
echo ### Backing up documents...
%backupcmd% "c:\users\%%a\documents" "%drive%\%%a\documents"
echo ### Backing up Favorites...
%backupcmd% "c:\users\%%a\favorites" "%drive%\%%a\favorites"
echo ### Backing up music...
%backupcmd% "c:\users\%%a\music" "%drive%\%%a\music"
echo ### Backing up pictures...
%backupcmd% "c:\users\%%a\pictures" "%drive%\%%a\pictures"
echo ### Backing up videos...
%backupcmd% "c:\users\%%a\videos" "%drive%\%%a\videos"

:eof

echo Backup Complete!
@pause
endlocal
**********************


Report •

#5
November 30, 2011 at 17:58:30
Try changing "@echo off" to "@echo on" and running it again to verify where it stops. I would do it myself but I don't have time right now.

Report •

#6
November 30, 2011 at 18:24:02
Thanks again for your prompt reply, jowah. I was able to fix the script issues as follows:

1.) changed (tokens=*) to "tokens=*"
2.) changed "users.tmp" to (users.tmp)
3.) changed "exist" order for XP and Vista/7 so that Vista/7 was checked first since "documents and settings" evaluates=true for both XP and Vista/7, which caused an "access denied" when Vista tried to access the "c:\documents and settings" directory instead of "c:\users".
4.) closed DO loop parentheses

The final working script follows. jowah I really think your script will be a big help to everyone who needs to do a quick backup. Thanks again for all your efforts.

**********************
@echo off

setlocal
set drive=O:\Backups\test
set backupcmd=xcopy /s /c /d /e /h /i /r /y

if exist "c:\users" goto startvis7
if exist "c:\documents and settings\" goto startxp

:startxp

for /f "tokens=*" %%a in (users.tmp) do (
echo ### Backing up desktop...
%backupcmd% "c:\documents and settings\%%a\desktop" "%drive%\%%a\desktop"
echo ### Backing up Favorites...
%backupcmd% "c:\documents and settings\%%a\favorites" "%drive%\%%a\favorites"
echo ### Backing up my documents...
%backupcmd% "c:\documents and settings\%%a\my documents" "%drive%\%%a\my documents"
echo ### Backing up music...
%backupcmd% "c:\documents and settings\%%a\music" "%drive%\%%a\music"
echo ### Backing up pictures...
%backupcmd% "c:\documents and settings\%%a\pictures" "%drive%\%%a\pictures"
echo ### Backing up videos...
%backupcmd% "c:\documents and settings\%%a\videos" "%drive%\%%a\videos"
)
goto eof

:startvis7

for /f "tokens=*" %%a in (users.tmp) do (
echo ### Backing up desktop...
%backupcmd% "c:\users\%%a\desktop" "%drive%\%%a\desktop"
echo ### Backing up documents...
%backupcmd% "c:\users\%%a\documents" "%drive%\%%a\documents"
echo ### Backing up Favorites...
%backupcmd% "c:\users\%%a\favorites" "%drive%\%%a\favorites"
echo ### Backing up music...
%backupcmd% "c:\users\%%a\music" "%drive%\%%a\music"
echo ### Backing up pictures...
%backupcmd% "c:\users\%%a\pictures" "%drive%\%%a\pictures"
echo ### Backing up videos...
%backupcmd% "c:\users\%%a\videos" "%drive%\%%a\videos"
)
:eof

echo Backup Complete!
@pause
endlocal
**********************


Report •

#7
November 30, 2011 at 18:52:08
Good work Mel2000! Thanks for the kind words.

Report •

#8
November 30, 2011 at 19:26:08
One last addendum. The XP My Documents tree already includes My Music, My Pictures and My Videos. Therefore the following six XP lines should be deleted:

echo ### Backing up music...
%backupcmd% "c:\documents and settings\%%a\music" "%drive%\%%a\music"
echo ### Backing up pictures...
%backupcmd% "c:\documents and settings\%%a\pictures" "%drive%\%%a\pictures"
echo ### Backing up videos...
%backupcmd% "c:\documents and settings\%%a\videos" "%drive%\%%a\videos"


Report •

#9
November 30, 2011 at 19:36:04
Yep, I knew I was forgetting something about that file structure...

Report •


Ask Question