Windows

Create a custom backup tool with Vista's Robocopy

Greg Shultz examines Robocopy and the necessary switches in detail. He then shows you how to create and use a command line script to create an exact mirrored duplicate of all the data files in your user profile folder.

If you're a conscientious computer user, chances are that you've used the Backup and Restore Center's Complete PC Backup to create an image file of your Vista hard disk, and you use the Windows Backup to back up your data files on a regular schedule. However, you may like to have an additional copy of your data files just to be on the safe side. While you can easily do so by copying the Documents folder to an external hard drive via drag-and-drop, that can be a tedious operation.

Fortunately, you can simplify your additional backup operation in Microsoft Windows Vista by using a tool called Robocopy. As you may know, Robocopy has been part of the Windows Resource Kit since Windows NT 4.0 days. However, Microsoft updated Robocopy with some extra features designed for Vista and decided to make it a regular part of the Windows Vista operating system. While this is a good thing in that it is readily accessible to all, there is a catch -- Robocopy is a command-line tool, and its power is tucked away in more than 80 switches.

This means that in order to harness the power of Robocopy, you have to spend a lot of time investigating and deciphering all the switches and then figuring out which ones you need to use. While this may not be a difficult procedure for us high-powered techies, it can be a daunting task to many casual users who would like to have an additional backup tool.

I recently spent some time delving into Robocopy 's command-line switches and have developed a nice little script that you can use to create an exact mirrored duplicate of all the data files in your user profile folder (C:\Users\YourName).

In this edition of the Windows Vista Report, I'll examine Robocopy and the necessary switches in detail. I'll then show you how to create and use a command-line script.

This blog post is also available in PDF format in a TechRepublic download.

Robocopy features

While the name implies a copy tool, Robocopy, or "Robust File Copy," is actually a much more powerful tool with a number of great features that make it a great backup tool. For instance, once you create your initial backup, on subsequent Robocopy operations, only files that have changed are copied again.

If you are backing up across a network connection that can be flaky or occasionally goes down, you can configure Robocopy to wait for the connection to come back up or, if that fails, to later pick up where the file transfer left off. Furthermore, Robocopy can preserve all the associated file information, including date and time stamps, security access control lists (ACLs), and more.

The switches

The Robocopy.exe file is stored in the \Windows\System32 directory on every Windows Vista installation. As such you can run it by opening a Command Prompt window. Once you do, type Robocopy /? > RobocopySwitches.txt to create a file that you can view in Notepad, as shown in Figure A.

Figure A

Creating a documentation file for easy reference will be easier than trying to learn about all the switches at a Command Prompt window.

As you look through the file, you'll see that it is divided into five sections, and the switches are broken down under those headings. This is a nicely formatted document that you can use to follow along with my example and later use to create or customize your Robocopy command-line script. The five sections are

  • Copy Options
  • File Selection Options
  • Retry Options
  • Logging Options
  • Job Options

Constructing the command line

For my example, I'm going to back up the data file contents of my user profile folder, C:\Users\Greg Shultz, to a folder named TheBackup on an external hard disk that is assigned drive letter J. (You'll, of course, substitute the names and paths with your own.) As such my command will begin with:

Robocopy "C:\Users\Greg Shultz" "J:\TheBackup"

Now, I want to back up every folder in the source, even any empty folders, as they may be placeholders for future data. I also don't want to have files on the backup that I deleted from my hard disk. While I can use the /S and /PURGE switches to accomplish my goal, I can use the /MIR switch to accomplish both of these tasks with one switch. Therefore, my command is now:

Robocopy "C:\Users\Greg Shultz" "J:\TheBackup" /MIR

The C:\Users\Greg Shultz folder contains several hidden system files and folders that I don't want or need to back up. For example, I don't need to back up the NTUSER.DAT file nor do I need to back up the contents of the AppData folder.

In addition, the C:\Users\Greg Shultz folder contains a host of junction points that I don't need to back up. Vista uses junction points to link various operating system folders to the user profile folder. For example, the Cookies folder and the SendTo folder are linked to the user profile folder via junction points.

I'll use the /XA:SH switch to exclude the hidden system files, and I can use the /XD AppData switch to exclude the entire AppData folder. I'll then use the /XJD to exclude all the junction points. My command is now:

Robocopy "C:\Users\Greg Shultz" "J:\TheBackup" /MIR /XA:SH /XD AppData /XJD

Now, one of Robocopy's features is that if it encounters a file that is in use, it will stop and wait for that file to be closed so that it can continue with the copy operation. It will retry to copy the file ever 30 seconds. The default number of retries is 1 million (no joke!). As this will most likely prevent the backup operation from ever completing, you should reset it to a reasonable number.

To change the number of retries, you'll use the /R switch, and to change the wait time between retries, you'll use the /W switch. I chose five retries with a 15-second wait time. That way after a reasonable number of retries and a waiting period, Robocopy will move on. My command is now:

Robocopy "C:\Users\Greg Shultz" "J:\TheBackup" /MIR /XA:SH /XD AppData /XJD /R:5 /W:15

Like all command-line tools, Robocopy keeps you apprised of the status of the operation right in the Command Prompt window. However, chances are that you'll want to customize and record that feedback in a log file. I like to have the whole picture, so I'll use the /V switch. However, I really don't need to know the percentage progress of each file copy, so I'll use the /NP switch. To create my log file, I'll use the /LOG switch, which will overwrite the existing log file each time. Now, my command is:

Robocopy "C:\Users\Greg Shultz" "J:\TheBackup" /MIR /XA:SH /XD AppData /XJD /R:5 /W:15 /V /NP /LOG:Backup.log

Creating and using your script

Now that you know how the script works and what the necessary switches are, you can launch Notepad, type the command, and save the file as RobocopyBackup.cmd. To make sure that the script and open log file don't interfere with the backup, I created a folder in the root directory called BackupTool (C:\BackupTool) and saved the script there.

Editor's Note: I included an example RobocopyBackup.cmd file in the download version of this document.

You'll find the log file in the same directory as the script after each backup operation. Keep in mind that while the log file is a simple text file, it can be larger than Notepad is able to handle. As such, you may want to use WordPad or another word processor to open and view the log file.

Now, anytime you want to make an extra backup, you can just double-click on the RobocopyBackup.cmd shortcut to launch it. When it is done, you can examine the Backup.log file. You can also use the Task Scheduler to automatically run your RobocopyBackup.cmd on a regular basis if you want.

What's your take?

Have you used Windows Vista's version of Robocopy? If so, what's your experience? Would you add any additional switches to the script that I presented in this article? Please drop by the Discussion Area and let us hear from you.

About

Greg Shultz is a freelance Technical Writer. Previously, he has worked as Documentation Specialist in the software industry, a Technical Support Specialist in educational industry, and a Technical Journalist in the computer publishing industry.

48 comments
onlinerack
onlinerack

We used to use robocopy in the past but with the lack to copy open files and getting the dates on the destination folders to be like the original, we had to find another tool. We used a utility called GSCOPYPRO from gurusquad. The tool is much simpler than robocopy and can copy open files with no issues let alone with proper support behind it.


Hopefully this saves others that ran into the same issue.

Arcturus909
Arcturus909

I am trying to back up my user Profile using a wildcard (%USERNAME%\My Documents) but I am getting and error 5 with the message Accessing source directory C:\Users\Steve Wilde\My Documents\ Access is Denied. How can I get around this? Obviously I am new to Windows 7. We are finally moving from XP.

TrueDinosaur
TrueDinosaur

The XD parameter is good but how do you supply a list of directories to exclude?

rrve
rrve

In your article you say "For example, I don?t need to back up the NTUSER.DAT file nor do I need to back up the contents of the AppData folder." Wrong! The AppData folder is where Outlook stores your emails by default. You most definitely want to backup this folder.

dschweickert
dschweickert

I had problems when using robocopy to backup my pictures directory. I couldn't find new folders and files in the destination drive. Please note that I was using the /M switch. It took me six months to realize that the folders and files were being copied to a hidden system folder (MY PICTURES) under the Documents directory. I discovered that if I just backup the Pictures directory first - then the Documents directory - that the backup routine works as desired.

DPCTutor
DPCTutor

I tried to use robocopy to copy my c drive to a backup e drive using the following: robocopy . e: /mir /e:3 /xj /e /w:10 /v /log:e:\backup.log of the 701 dirs it copied 370 and skipped 331 of the 3787 files, it copied 2593 and skipped 1186. With the /mir why did it skip anything? AND OOPS! it wiped out the other files and subdirectories I had on e drive. dpc

FrankGC
FrankGC

If you use Task Scheduler and the output is on a shared network drive The bat file should Use UNC addressing in the target file: \\externaldrive\public\Shared-Music\recordings\ not this: S:\Shared-Music\recordings\ You can find the UNC form with the DOS cmd Net Use I had no end of problems locating the destination using the disk mapping form.

FrankGC
FrankGC

Greg, Thanks for the instructions. I do have a batch file working. I need help with the /Log switch. Here are two attempts both failed (it works without the /log). I need the quotes around source and dest. because some folder names have embedded blanks. Robocopy "c:\Users\Owner\Music\Recordings\Back Up" "s:\Shared-Music\Back Up" /LOG:Backup.log Robocopy "c:\Users\Owner\Music\Recordings\Back Up" "s:\Shared-Music\Back Up /LOG:Backup.log"

regana
regana

Informative and useful. Your instructions are to exclude AppData from the backup. However, Outlook pst and data files are stored in Vista's AppData Local and Roaming subfolders. I need to backup Outlook. What syntax or work around would you suggest to robocopy outlook data? Any feedback is much appreciated. Thanks

sunway.is
sunway.is

I do not have comments but deeply appreciate this information. I had faced difficulty backing up files last few months, and found this alternative solution. However, I do not quite understand this with Copy and paste. By doing this command line, it is faster? but I noticed some (quite a number) files were not copied. Do we have to wait each time the command line is run? because I noticed that as I checked the backup.log it changed after 1 minute I checked it again, more files were copied. Thank you very much. :)

pavel.balek
pavel.balek

I am working with Vista CZ, and the hint how to make the RobocopySwitch.txt works, but the result has unreadable diacritics. I have used another process for those, who want the text readable: -open the Command line window -write "robocopy /?" -do -In the upper left corner open the Icon of Command line window. -chosse Edit -choose select All - do -Switch to the RobocopySwitch.txt -do paste (Ctrl+V) -edit the unnecessary lines -save the file

bob
bob

Great article.

techjim
techjim

XCOPY used to be a powerful copy command, how does robocopy compare in speed? I know it's more powerful in that it has a lot more switches. Thanks, --Jim

aleksacv
aleksacv

Can Robocopy copy opened files? For example .pst file while MS Outlook is opened. Something like shadow copy.

dwang00
dwang00

Use robocopy all the time for work and personal data replication. For end-users I'd agree with the above usage of using Microsoft's free SyncToy tool. A very nice GUI and wizard that makes it easier for end-users to use than robocopy.

Retired007Geek
Retired007Geek

I've had a problem with RoboCopy using a WD-Netcenter NAS. I keep getting Error 5 - when the program tries to Clear the Archive attribute on the source file (Dell E530). I've done some pretty extensive testing on this issue. I've run the cmd file from both the WD-NetCenter and the local hard drive...same problem. I've tried using \\WD-Netcenter\... and mapping it to X:\... same problem. However, if I attach a standard USB 2.0 disk or use the second drive on the Dell box NO PROBLEMO at all! So anyone have an idea as to what gives here? Here's the CMD file I'm using: @echo off rem *** Critical Files Backup w/Shutdown using RoboCopy *** TITLE CRITICAL FILE BACKUP to NETWORK Hard Drive rem *** Updated 10/12/08 BEK *** set RCDIR="E:\Program Files\Windows Resource Kits\Tools\RoboCopy.exe" set WDNC="\\WD-netcenter\shared files\BEKDocs" echo. echo. ------- Updating Crucial Data Files to WD Net Center Drive ------- echo. %RCDIR% %MyDocs% L:\BEKDocs /MIR /XA:SHT /R:1 /W:10 /V /NP /LOG:Backup.log /TEE if errorlevel 16 echo ***FATAL ERROR*** & goto end if errorlevel 8 echo **FAILED COPIES** & goto end if errorlevel 4 echo *MISMATCHES* & goto end if errorlevel 2 echo EXTRA FILES & goto end if errorlevel 1 echo Copy successful & goto end if errorlevel 0 echo --no change-- & goto end :end shutdown -s Bruce

tinyang73
tinyang73

Robocopy 'C:\Users\username' 'J:\TheBackup'? /MIR /XA:SH /XD AppData /XJD /R:5 /W:15 /V /NP /LOG:Backup.log This worked for you? Not working for me on vista business 32 bit. I'm looking at robocopy to replace an old script I have for backups on 2000/XP machines. A couple questions: - Where will it put the log file in your example? - I am logged in as a user with admin permissions, and I right-click on the script and select "Run as administrator". UAC then prompts me, so I tell UAC to allow it, then it flashed by real quick in a cmd window and that's it. (I find it easier to use cron in linux, but I have to support windows machines too) And when I check the destination directory, nothing is backed up. Does anyone have any helpful suggestions what could be wrong or how I could go about troubleshooting this? Thanks.

jsmaccready
jsmaccready

I used RC for years to copy database backup files from SQL Servers onto machines covered by our taping license. It logged, retried, etc, which made it ultra reliable. There is even a(n unsupported) graphical interface for configuring RC jobs built by someone inside MS. HOWEVER, I had to abandon it for these jobs when we started deploying the Windows2003 64bit OS on file servers. RC is not supported in the 64 bit Win2k3 kernal. I don't know about the Vista 64 bit kernal. Something to check out (I don't have it so I can't).

s31064
s31064

I took me longer to read this article than it did to completely automate my backups using SyncToy. One nice feature with SyncToy is the various ways to backup. I use Sync from my laptop to my home drive on a network server, because there are times I'll modify a file from a machine other than my own, so I want the modified file copied to my laptop. Sync does what the name implies, it synchronizes all changes both ways between two folders. For my users, I set up a task using Echo, in which the folders on their machines are duplicated on their home drives, with all renames, deletes, etc. being 'echoed' on the target. There is another option which I rarely use called Contribute, which is similar to Echo with exception that no files are deleted on the target. You can create several different backups, or Folder Pairs as they're called, for different circumstances. I rarely change my music or photo files, so those backups are run once a week, while my data files are backed up at login. (I know, logoff makes more sense, but who wants to wait for 18GB of data to backup after logging off? It's a trade-off between time and security, and in this case, time won.) However, the option exists to have a job run ALL Folder Pairs without creating a new task, so even if you have data all over the place, once you create the Folder Pair, one job can do it all. The only issue I've had with SyncToy (2.0 is a free download from Microsoft) is my own fault. When I upgraded from 1.2 to 2.0, I didn't read the fine print, which basically says the new command line version for Task Scheduler is no longer SyncToy.exe, it's SyncToyCmd.exe. Once I realized what the issue was, it took about 30 seconds to fix my task and haven't had a problem since.

Old Man on the Mountain
Old Man on the Mountain

I've been using it for years now. Added CCleaner to make sure I backup only legitimate items. The 1GB or so fits nicely on my USB drive. So yes, Robocopy is THE tool in my opinion.

stephen.bryant
stephen.bryant

I have Task Scheduler (in XP) run Robocopy to copy key directories several times a day and keep several complete older copies too, numbered 1, 2, 3... First copy n-1 is copied to copy n, then n-2 to n-1, etc., and finally current files to copy 1. All copies are on some other machine, including at least one on a machine outside my own office. I've never had my hard drive actually fail (!) but I've sometimes retrieved a file after making a dumb mistake myself.

ian.chase
ian.chase

I have been running a similar script for ages, but so I dont need to keep remembering to run it I have added the script to the logoff in the Local Computer Policy> User Configuration > Windows Settings > Scripts (Logon/Logoff) I nolonger have to remember to run it as its now run automatically and keeps everything up to date

brian.mills
brian.mills

I haven't used robocopy on Vista yet (I'm still running XP) but I've been using it for all my backup jobs from my XP laptop to my Ubuntu server. My only complaint stems from differences between the two operating systems regarding modified time. About half my files get backed up each time I run my backup script. Oh well, I guess that's better than all of them getting backed up each time...

Mark W. Kaelin
Mark W. Kaelin

Have you used Windows Vista's version of Robocopy? If so, what's your experience? Would you add any additional switches to the script that Greg presented?

kurgbe
kurgbe

/XD dir1 dir2 dir3 You can put as many dir as desired. /xd LocalService NetworkService *temp *"temporary internet files" *cache Warning: dir1 is relative to the source directory of robocopy robocopy source target /XD dir1 will excludes source/dir1 directory

FrankGC
FrankGC

Hi, I can not find the /e:3 ( with operand 3) switch documented I looked here: http://www.mydigitallife.info/2007/05/07/robocopy-syntax-command-line-switches-and-examples/ and here: http://ss64.com/nt/robocopy.html The /MIR will copy sub directories (/e) and /purge - delete dest. files no longer in the source My command line is this: Robocopy C:\Users\Owner\Music\recordings\ \\externaldrive\public\BackUp-Music\recordings\ /MIR /COPY:DT /Log:MusicBkupLog.txt I am backing up my music files only. The /COPY:DT copies Data and Timestamps. Does not copy Attributes thus avoiding error 5 access denied Does this help? Regards Frank C

Retired007Geek
Retired007Geek

Regana, I've found that moving the .pst file to the user's My Documents folder solves this problem. Another advantage of this method is that if you have a dual boot machine you can set up both copies of Outlook to use the same .pst file. I do this with my laptop which dual boots XP & Vista using Office 2003 on XP and Office 2007 on VISTA. Both versions use the same .pst file so no matter which OS I'm using all my emails are saved in one place and accessible from either. Note: the My Documents/Documents folder on both OSes was also moved to the G: drive accessible from both OSes since I hide the drive containing the non-used OS to prevent some conflicts if both OS drives are visible at the same time. Bruce

dwang00
dwang00

The obvious benefit of robocopy over xcopy is that the destination folder is sync'ed to the originating folder - including deletions. As far as speed, it should be faster since when you re-run the same robocopy process using /Z /B or /ZB switches it will only deal with new/delete/modify files and skip files that haven't changed. In that way it works the same way as incremental backups.

dwang00
dwang00

No. It cannot copy open files. That said, there are 2 switches you can use /R:nn and /W:nn to retry and wait between retries. So while you can't copy open files, you can set up the retry and wait. Also since robocopy can do incremental sync, you can re- run the sync after hours and it will only pick up the files that were open during the day

m.finlay
m.finlay

Type the command into a command prompt to see the any errors: Start => Run, type "cmd", OK, and type the command in there. There looks to be a strange character after the 'J:\TheBackup' section. I usually create a .bat file and have that run by the scheduled job. Good luck.

brian.mills
brian.mills

The problem I had with SyncToy which made it unusable for my purposes, was it only works when you're logged in. So I can't set a sync operation to run in the middle of the night when I'm not logged into my machine. It would complain that it couldn't open a window when the user isn't logged in, or something like that. Been too long since I gave up on it. I think they were going to fix that in the new version, but I'd already moved on to Robocopy before it came out.

jwilsonjx
jwilsonjx

We use a hybrid of Robocopy and the User State Migration Tool. We use USMT for profile migration and Robocopy for data migration. USMT comes in handy on systems that have shift workers where there will be multiple profiles (and the users not necessarirly available at the time of migration). Robocopy seems to be faster at doing data migration than USMT.

Mark W. Kaelin
Mark W. Kaelin

Someone also mailed this link to me. I have not tried it yet, but it is a GUI for the Robocopy application.

lesjkizer
lesjkizer

Took two days to find this info.

rasilon
rasilon

I like Robocopy for big, complex file copies. It's command options make it a major, important tool. However, for the simple task of backing up important files, I like "Karen's Replicator" or even SyncToy much better. They have good, simple user interfaces that let you easily construct a job that will, on your schedule, keep your critical files synchronized to a backup location. Hank Arnold (MVP)

Craig_B
Craig_B

I have been using Robocopy for years and found it to be great, especially across WAN connections and it uses very little CPU cycles. I use the /xo (exclude older) switch when combining data from a previous copy. Example: robocopy c:\data \\NetServer\Share /e /r:2 /w:5 /np /log:c:\robolog.txt /xo

steveoh
steveoh

Found it years ago and have used it heaps. To keep a history of the log files I use these set commands to rename the log file. Set mm=%DATE:~3,2% Set dd=%DATE:~0,2% Set yyyy=%DATE:~6,4% rename robo.log %yyyy%-%mm%-%dd%.txt Getting the set's to match my time and date config was a bit tedious! RoBo saved the day once when a HDD had a heat fault. It died after 3min's. I created a robo batch file to get the data and every morning it would skip over the recovered files and take up where it failed the day before. I'd get another few hundred meg each morning. It took a week but I got it all. It's so simple there's very little scope for something unexpected going wrong. It just works.

acole
acole

I can no longer copy large files from Vista to my XP PC since installing Vista SP1 - Robocopy is the only way (can't can;t find network messages etc...). Tried several tweaks - the only option remaining is to reinstall SP1 - can't face that so I'll live with it. Robocopy is good but how that can work and not the usual copy or xcopy is beyond me (not that difficult!)

gstrickland
gstrickland

Xcopy also deals with new and modified files, so the two are pretty similar - so similar infact I think Microsoft have simply used Xcopy code as the basis for Robocopy. I've been using Xcopy on a number of client sites for scheduled nightly backups of their data to external hard drives and to off-site backup facilities. It works a treat. I think Robocopy will prove useful if people finally accept Windows Vista (unlikely!).

onlinerack
onlinerack

@dan_wang  Look at gscopypro from gurusquad. It can copy open files and works way better than robocopy and xcopy.  I am a big fan of robocopy but the open files was really needed.

tinyang73
tinyang73

Thanks, worked a charm. I'm up and running with robocopy now.

ian.chase
ian.chase

Robocopy has been around for years and can be got from the Server 2003 Resource Kit Tools

Rob Kuhn
Rob Kuhn

I started to use a program called Tera Copy; http://www.codesector.com/teracopy.php I use on my XP machines (SP2 and SP3), Vista (32 and 64 bit) and on a couple of my Server 2003 machines. On one of my Server 2003 I move about 2.3 TB of data every weekend. I use to use Robocopy and it would take approximately 13 hours to complete. Using Tera Copy it would take 12 hours. I still like and use Robocopy when I have to move a lot of data where there are permissions (i.e. NTFS security). Robocopy does a great job in copying that info.

techjim
techjim

Thanks for the comparison opinions, and the comments about Tera Copy. I've read about that program too. Definitely 2 tools for the tool bag. --Jim

Editor's Picks