TechRepublic : A ZDNet Tech Community

Microsoft Windows

Host: Mark Kaelin
Contact

Create a custom backup tool with Vista's Robocopy

Save yourself future aggravation by creating 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.

Greg ShultzGreg 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. Read his full bio and profile.

Print/View all Posts Comments on this blog

Robocopy to the rescue Mark W. KaelinTechrepublic Moderator | 09/10/08
Its the only way I can copy large files! acole@... | 09/12/08
Tera Copy rkuhn@... | 09/12/08
Excellent comparison techjim@... | 10/29/08
Does this work with XP sp3 as well? BrewBaker | 09/12/08
SP3 ian.chase | 09/14/08
Robocopy & NT? sharontravis@... | 10/10/08
Love RoBoCopy steveoh@... | 09/12/08
Robocopy Rules Craig_B | 09/15/08
There are better & easier tools for this job rasilon | 09/16/08
Thank you - Greg lesjkizer | 09/26/09
RE: Create a custom backup tool with Vista's Robocopy gkeramidas@... | 09/10/08
Thanks for the link Mark W. KaelinTechrepublic Moderator | 09/11/08
Custom Backup jwilsonjx | 09/11/08
Robocopy is Great brian.mills@... | 09/11/08
Schedule to run at Logoff ian.chase | 09/12/08
Robocopy to make backups of backups stephen.bryant@... | 09/15/08
Right on bruddah! Old Man on the Mountain | 09/15/08
RE: Create a custom backup tool with Vista's Robocopy s31064 | 09/15/08
SyncToy problems? john.blight@... | 09/16/08
SyncToy brian.mills@... | 09/18/08
RE: Create a custom backup tool with Vista's Robocopy jsmaccready@... | 09/15/08
RE: Create a custom backup tool with Vista's Robocopy tinyang73 | 09/15/08
cmd m.finlay@... | 09/15/08
re: cmd tinyang73 | 09/22/08
RE: Create a custom backup tool with Vista's Robocopy boffin-98713@... | 10/14/08
RE: Create a custom backup tool with Vista's Robocopy dan_wang@... | 10/28/08
Can Robocopy copy opened files? aleksacv@... | 10/28/08
no, but... dan_wang@... | 10/28/08
Vista's Robocopy: How does it compare to the old dos XCOPY? techjim@... | 10/28/08
incremental sync dan_wang@... | 10/28/08
Re: incremental sync gstrickland@... | 10/29/08
RE: Create a custom backup tool with Vista's Robocopy robertvallerand@... | 10/28/08
RE: Create a custom backup tool with Vista's Robocopy pavel.balek@... | 10/28/08
RE: Create a custom backup tool with Vista's Robocopy sunway.is@... | 11/02/08
What about Outlook data and other data stored in AppData? regana@... | 03/24/09
Try moving your .pst file boffin-98713@... | 09/27/09
RE: Create a custom backup tool with Vista's Robocopy FrankGC | 08/10/09
RE: Create a custom backup tool with Vista's Robocopy FrankGC | 08/18/09
RE: Create a custom backup tool with Vista's Robocopy DPCTutor@... | 10/23/09
Your Switches FrankGC | 10/24/09
RE: Create a custom backup tool with Vista's Robocopy dschweickert | 10/30/09

What do you think?

White Papers, Webcasts, and Downloads

Recent Entries

TR on Twitter

Archives

TechRepublic Blogs



500 Things Every Technology Professional Needs to Know
Did you know Microsoft's RegClean does not work with XP but you can use shareware to clean your registry? Did you know most wireless access points don't have encryption enabled by default? Did you know there are 500 tidbits of information contained in TechRepublic's 500 Things Every Technology Professional Needs to Know that will help you become a successful IT professional.
Buy Now
Administrator's Guide to TCP/IP, Second Edition
Maintain your critical TCP/IP system and ensure reliable, safe remote access. Get the expert advice and solutions to handle Windows networking, Cisco routing, documentation, and troubleshooting.
Buy Now

SmartPlanet

Click Here