Home‎ > ‎

Using robocopy to backup all files from a disk

posted Mar 14, 2011, 2:51 AM by Pieter-Bas IJdens
Recently I got handed a Windows Vista laptop that would not boot properly into Windows anymore. After careful examination, I decided that cleanly re-installing the laptop was the best option. However, the laptop not being mine I decided to first check with the owner if they had a recent backup. Obviously I ended up backing it up myself. Not being familiar with the places that the owner stored their data, I decided to run a full filesystem backup to a spare disk.

Having heard 'robocopy' is an excellent tool for this, I decided to give it a try.

Naturally, due to inexperience with it, I ran into all kinds of problems with the tool. Here is a quick summary for those of you who are also new to robocopy and need to solve a similar problem.
  1. While working, robocopy creates a hidden directory on the target disk. If you abort the process, this hidden directory will not be deleted, and will keep consuming space until you delete it yourself.

  2. You'll find that deleting the directory will be very difficult from Explorer, especially if you also copy permissions. Also rmdir /s/q will fail to delete the directory. Instead:

    Suppose your hidden target directory is P:\BACKUP:

    Create an empty directory named P:\EMPTY and then run:

    robocopy P:\EMPTY P:\BACKUP /PURGE

    This will cause robocopy to remove all files it created, even if they violate normal filesystem rules.

  3. On Windows Vista and Windows 7 there is a concept of Junctions. These are special folders that are created by the operating system, and act as redirection points to other directories. The idea is that software written for previous versions of Windows, and that expects directories like C:\Documents and Settings, and "My Documents" in the user directory will still work on the new structure (that uses C:\Users, and uses a Documents directory instead of "My Documents").

    These junctions are protected on the FS with special permissions causing normal user access to them to be denied. However, robocopy run in administrator mode will be able to open them, iterate over them, and copy them.

    This is NOT what I wanted in this scenario, as it causes robocopy to endlessly loop over "Application Data", as the junctions cause an endless loop in the file system.

    The /XJ option for robocopy causes it to exclude junctions from the copy process.

  4. Robocopy can resume an interrupted copy if you use the /Z option, or the /ZB option.

  5. Using /TEE and /LOG: options are useful. /LOG allows robocopy to create a logfile. This causes it to stop outputting anything. However, specifying /TEE will cause it to also log to the console.

  6. /E will cause robocopy to also copy empty subdirectories
    /ETA will cause it to log a remaining time on the console for each file
    /R:<n> can be used to limit the number of retries when copy fails (default is 1000000)
    /W:<n> can be used to limit the amount of time waited before retrying.

I ended up creating a batch file with the following content:

robocopy G:\ P:\BACKUP *.* /XJ /SL /E /ETA /TEE /R:5 /W:5 /ZB /LOG:D:\mariska.all\c-disk-copylog.txt

G:\ was the source disk, and P:\BACKUP the target, ending up with a nice copy of all data. 

I intentionally didn't include /COPYALL in this case becuase the purpose was to backup the files, not the permissions and security attributes. These are not relevant to the user in this case.

Lessons learned: Robocopy is a really powerful tool for copying lots of data. The resume method is really useful. However, when anything goes wrong while copying the data some google skills are needed to clean up after it.
 
Comments