Link to home
Start Free TrialLog in
Avatar of Bill H
Bill H

asked on

Robocopy Script to Copy User Profiles

I'm looking to create a script that i can schedule on a few machines that will copy an entire users profile to a shared folder. I do not want anything deleted.I dont need mirroring or anything like that, it would be just a one time backup. I have to use this same script for everyone.
Avatar of Phiwi Moyo
Phiwi Moyo
Flag of Germany image

Or you can use xcopy:

Example :xcopy "c:\Documents and Settings\Walter" "FileServer\I$\Backup" /S /E /C /Y /K /D /Z
Avatar of Bill H
Bill H

ASKER

Would prefer robocopy.
@ECHO OFF

SET ROBOCOPY_FILE_NAME=robocopy.exe
SET ROBOCOPY_BASE_TEST_PATH=%WINDIR%\system32
SET ROBOCOPY_MASTER_COPY_PATH=\\192.168.1.10\robocopy
::%PROGRAMFILES%\Windows Resource Kits\Tools

SET ROBOCOPY_LOCAL_DIR=C:\ROBOCOPY

SET REMOTE_BACKUP_BASE_PATH=\\192.168.1.100\profiles
SET REMOTE_BACKUP_USER_PATH=%REMOTE_BACKUP_BASE_PATH%\%USERNAME%
SET REMOTE_BACKUP_USER_COMPUTER_PATH=%REMOTE_BACKUP_BASE_PATH%\%USERNAME%\%COMPUTERNAME%

::Robocopy exists in the System32 folder in both MS Vista and MS Windows 7.
IF EXIST "%ROBOCOPY_BASE_TEST_PATH%\%ROBOCOPY_FILE_NAME%" (
        SET ROBOCOPY_EXECUTABLE_FILE_PATH=%ROBOCOPY_BASE_TEST_PATH%\%ROBOCOPY_FILE_NAME%
) ELSE (
        ::Create the local directory for the Robocopy executable.
        MKDIR "%ROBOCOPY_LOCAL_DIR%"
       
        ::Copy the robocopy executable to the local directory.
        ::/y Suppresses prompting to confirm that you want to overwrite an existing destination file.
        COPY "%ROBOCOPY_MASTER_COPY_PATH%\%ROBOCOPY_FILE_NAME%" "%ROBOCOPY_LOCAL_DIR%\%ROBOCOPY_FILE_NAME%" /y
       
        SET ROBOCOPY_EXECUTABLE_FILE_PATH=%ROBOCOPY_LOCAL_DIR%\%ROBOCOPY_FILE_NAME%
)

ECHO Current Variables:
ECHO %ROBOCOPY_BASE_VISTA_PATH%
ECHO %REMOTE_BACKUP_BASE_PATH%
ECHO %REMOTE_BACKUP_USER_PATH%
ECHO %REMOTE_BACKUP_USER_COMPUTER_PATH%
ECHO "%ROBOCOPY_EXECUTABLE_FILE_PATH%"
ECHO "%USERPROFILE%"

::Create the remote profile backup directory for the current user.
MKDIR "%REMOTE_BACKUP_USER_PATH%"
::Create the remote profile backup directory for the current computer.
MKDIR "%REMOTE_BACKUP_USER_COMPUTER_PATH%"

::Execute robocopy to copy the user's profile folder contents to the remote storage location.
::/MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).
::/R:n :: number of Retries on failed copies: default 1 million.
::/W:n :: Wait time between retries: default is 30 seconds.
::/XA:[RASHCNETO] :: eXclude files with any of the given Attributes set, e.g., S=System, H=Hidden.
::/XD dirs [dirs]... :: eXclude Directories matching given names/paths.
"%ROBOCOPY_EXECUTABLE_FILE_PATH%" "%USERPROFILE%" "%REMOTE_BACKUP_USER_COMPUTER_PATH%\" /MIR /R:1 /W:2 /XA:SH /XD *temp "temporary internet files" *cache mozilla

Open in new window

Avatar of Bill H

ASKER

I need something universal also, like where i can back up each users profile by running the same script locally on each machine.
Avatar of Bill H

ASKER

wouldnt this work:

robocopy c:\documents and settings\ \\backupserver\profiles /E /Z /LOG+:"robo.log"
Wont copy alot of things out of the currently logged in user.....

So it would depend on how you launched the script....
Avatar of Bill H

ASKER

hmm, ok, how do i go around that, log in as a diff user?
Machine Startup , or after hours deployed script (to reboot the machine and launch the copy)

If a user is logged in, you may get most of the profiles, but the one thats logged in cannot be fully backed up using robocopy/xcopy etc, as there are files open, mainly the registry, and anything else left open.
Avatar of Bill H

ASKER

I plan on doing this after hours anyways, is there a way to include any hidden files?
ASKER CERTIFIED SOLUTION
Avatar of GhouseAdmin
GhouseAdmin
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Might this help you,

-------------------------------------------------------------------------------------------------------------------------
@Echo off
cls
set user=
echo Enter Username
set /p user=

robocopy "source\%user% "Destination\%User% /e /v /r:2 /w:2
Are you in a domain environment?  If you are you could run this batch file from the server:

That will reboot the computer and then copy everything
shutdown -m \\computername -r -t 00
robocopy "\\machinename\c$\Documents and Settings\" "Destination full path" *.* /e /zb /copyall

Open in new window

If you have more than one computer to run it on you could put all the computernames in a text file and pull the computername from the file.  That would save you from going to each PC individually.

Create a text file with all the computer names first and put it somewhere.  The script is below:

forgot something:  you'll have to have the sleep command.   It can be found in the Server 2003 Resource Kit.  http://www.microsoft.com/downloads/en/details.aspx?familyid=9d467a69-57ff-4ae7-96ee-b18c4790cffd&displaylang=en
The batch file below is set for two minutes(120 seconds) of waiting for the computer to reboot. You may need to extend it for older computers.
for /f "tokens=*" %%a in (C:\OldHDD\ComputerList.txt) do (
	shutdown -m \\%%a -r -t 00
         sleep 120
         robocopy "\\%%a\c$\Documents and Settings\" "Destination full path" *.* /e /zb /copyall
)

Open in new window

Avatar of Bill H

ASKER

I got the script working so far, and everything copied over. However, a few of the files that copied over, like say the Outlook .nk2 file is 0KB, when it should be 5kb. Why would this be?
nk2 files are for Outlook's Autocomplete List.  Was Outlook open when it was copied?  That would result in a corrupt backup.  Technically, copying the nk2 file isn't a good idea anyway, because there are potential problems with display names not matching underlying addresses and such.
Avatar of Bill H

ASKER

putergeek,

i got the nk2 issue sorted out. Now, regarding your script, i put a computer name in a text file and updated the paths. But the error message i get is error 53: accessing source directory \\%%a\C$\documents and settings\administrator.

My robocopy script looks like this if you need it:
robocopy "\\%%a\C$\documents and settings\administrator" "\\remotesan\testing\backup" *.* /E /Z
Are you putting in the entire script that I posted?  You have to have the "for" command in there, also.