Friday, July 24, 2009

Create backup with Vista's Robocopy

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.

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.

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.


Author : Greg Shultz

No comments: