File-level backup in Windows Server 2008 R2 by using robocopy command

Posted on January 1, 2011


Microsoft has added better support for both block-level backup and file-level backup in Windows Server 2008 R2 comparing to its predecessor Windows Server 2008. The new features including:

  • Better support for file level backup: you can now choose folders and files to be included or excluded without backup the whole partition which makes the backup plan more flexible.
  • Better support for network backup: now it’s possible to backup to NAS running Samba file service for both block-level backup (file-level support is not directly supported due to the problem with sparse file, see article here). Incremental backup is also supported, but you won’t get versioning control due to the lack of VVS in destination.

The file-level backup of my documents, photos and videos to NAS is still problematic for me not only due to the problem of mounting VHD file when it’s created as sparse file by Samba, but also because I’d like to share the backup to other computers in the same folder structure, so my requirement is to synchronize/mirror backup files in local hard-drive to my NAS, and I use robocopy to achieve this.

Backup by using robocopy

Robocopy stands for “Robust File Copy”, and it is a command-line directory replication command similar to Unix xcopy command. The big advantage of this command is that it only copies/moves files that are modified which suites incremental backup perfectly.

To see the syntax and options of this command, open a prompt windows and type the following command:

robocopy /?

A detailed description of syntax and options can be also found here. Essentially we need to specify the source folder, the destination folder and any specific options. In our backup scenario, we’d use the following command to backup all photos to NAS:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /MIR

The option “/MIR” makes sure the backup mirrors the source, so if I delete a photo from my source later on, it will delete it from the backup.

Another thing robocopy does is wait and try again if it can’t do what it’s supposed to (why it’s called Robust File Copy). By default, it will wait a long time (30 seconds) and try a lot of times (1 million) if something’s up, to avoid waiting for 30 million seconds,  let’s change these to retry 5 times and wait 15 seconds in between:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /MIR /R:5 /W:15

We can even specify more options:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /Z /MIR /R:5 /W:15 /V /TS /FP /NP /LOG+:"C:\NASSync.log"

Which tells the command to produce verbose (/V) output with timestamp (/TS), full pathname (/FP)  and no progress (/NP) options as well as log file (/LOG+ for appending). For NAS which doesn’t run NTFS, we probably also need /FFT option, see more discussion here.

Schedule the backup

To schedule the backup task to run regularly, we open the Windows Task Scheduler. For our purpose, try looking on the right for an item called Create Basic Task which will launch the Create Basic Task Wizard, now give this task a name (for example: backup) and click Next. This goes to the Trigger screen, which dictates how often the backup will be done. Click next and now we’re at the Action screen and we want to start a program, so click Next. Here’s the important part, we need to select the program to run, which is robocopy, we can either just type robocopy or click browse and find C:\Windows\System32\robocopy.exe. Now we need to enter the parameters into the Add arguments (optional) textbox. These will be whatever we’ve decided based on the previous section:


Click Next and then Finish and it’s all setup.

Now if we want to see if it will all work, we should be able to find our backup task, right-click it and choose Run. After the task is completed, we can check the Last Run Result, which will be the exit code robocopy gives. The explanations of the exit code can be found here.

More examples

More advanced features of robocopy are illustrated as examples below:

1. Copy files that are modified within 5 days (except today) in source folder to destination folder:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /maxage:5 /minage:1

2. Move files from source folder to destination folder:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /move /e

3. Copy files except hidden files (/xa:h) from source folder to destination folder:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /e /xa:h

Or copy only read-only system (/ia:rs) files from source folder to destination folder:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /ia:rs 

We can even modify file attributes when copying by using A+ (add attributes) or A- (remove attributes):

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /e /A+:r

The example above shows copy from source folder to destination folder and make sure all files are read-only afterwards.

4. Copy files with certain pattern from source folder to destination folder, empty folders won’t be copied (/s):

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" *.doc *.ppt /s

5. Copy files with size between 1000 bytes and 60000 bytes from source folder to destination folder:

robocopy "E:\Documents" "\\NAS\BACKUP\Documents" /e /max:60000 /min:1000

6. Exclude the entire AppData folder(/XD AppData) and all the junction points (/XJD):

Robocopy "C:\Users\Administrator" "\\NAS\BACKUP\Administrator" 
         /MIR /XA:SH /XD AppData /XJD

Windows 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. We normally don’t want them in our backup.

7. Create 32 threads (/MT:32) to do the copy task:

Robocopy "C:\Users\Administrator" "\\NAS\BACKUP\Administrator" 
         /MIR /XA:SH /XD AppData /XJD /R:5 /W:15 /MT:32

The above example uses the option /MT[:n], where n is a number from 1 to 128 which indicates the number of threads to be used. Keep in mind that n is optional and that by default, the /MT option will use 8 threads.