HomeHowTo › Data Backups With Robocopy

Data Backups With Robocopy

Robocopy (Robust File Copy for Windows) is a nice command-line utility for file-based backup creation and restoration. The cool thing about it is that it is small (just one .exe file), it is included in Windows 7 and it offers many options to meet virtually any requirement. If you still use Windows XP, you can download Robocopy from Microsoft here – it’s part of the Windows Server 2003 Resource Kit Tools.

The fact that it is a command-line utility might deter some people from using it. But basically you only need to construct one command and copy it for every folder you want to back-up, so it’s not that bad. I have been using Robocopy for a while now and it is working perfectly fine.

 

Basic Usage

You can use Robocopy directly in the Windows command-shell or in a batch script (.bat file). The first parameter has to be the source directory. The second one would have to be the destination directory. Then several options can be added to customize the copying process.

[CC lang=”bash” line_numbers=”false”]
robocopy “X:\Sourcepath” “Y:\Destinationpath” /option_1 /option_n
[/CC]

 

Options

To find out all the possible options, type [cci lang=”bash” theme=”twitlight”]robocopy /?[/cci] into your command-shell or check the Technet Documentation. If you just want to create a simple backup script, have a look at the following table with the most useful options for regular backups:

Parameter Description
/MIR Mirrors a directory tree (equivalent to /E plus /PURGE)
/E Copies all subfolders (including empty ones)
/PURGE Deletes files and folders in the destination that no longer exist in the source
/R:n Specifies the number of retries on failed copies
/W:n Specifies the wait time between retries in seconds
/ETA Shows the estimated time of arrival for each file
/LOG:file Redirects the output to the specified logfile (overwriting)
/LOG+:file Redirects the output to the specified logfile (appending)
/TEE Displays the output in the log file and also in the command-shell window
/NDL Do not log directory names
/NFL Do not log file names (errors and deleted files will still be logged)
/NP Do not log progress information
/XF “file1” “file2” Excludes files with the specified names
/XD “dir1” “dir2” Excludes directories with the specified names
/L Simulates the copy process (no files are copied, but the log file is written)

By default, attributes and timestamps will be copied along with each file (parameter /COPY:DAT). Security information (ACLs) however, will not be included. Use the following commands to control which properties should be copied:

Parameter Description
/COPY:<Flags> Select the file properties to be copied: D (Data) A (Attributes) T (Timestamps)
S (NTFS Access Control Lists) O (Owner) U (Auditing Information)
/COPYALL Same as /COPY:DATSOU
/DCOPY:T Copies directory timestamps

 

Example Backup Script

To simply mirror one folder, you could use this command:

[CC lang=”bash” line_numbers=”false”]
robocopy “X:\Sourcepath” “Y:\Destinationpath” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:logfile.txt /NDL /NP
[/CC]

If you don’t have all the data that you want to back-up in one drive or folder, just take this line and copy it for every folder. Then you might end up with a little script like this one:

[CC lang=”bash” lines=”-1″]
REM ===== Set destination drive for backups =====
set dest=M:

REM ===== Backup media files =====
robocopy “D:\Docs” “%dest%\Media\Docs” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\docs.log /NDL /NP
robocopy “D:\Music” “%dest%\Media\Music” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\music.log /NDL /NP
robocopy “D:\Pics” “%dest%\Media\Pics” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\pics.log /NDL /NP
robocopy “D:\Videos” “%dest%\Media\Videos” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\videos.log /NDL /NP

REM ===== Backup some more stuff, excluding specific folders =====
robocopy “E:\Misc” “%dest%\Misc” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\misc.log /NDL /NP /XD “Temp” “Log” “Stuff”

REM ===== Backup Opera profile (v12.x) =====
robocopy “C:\Users\Acidx\AppData\Roaming\Opera\Opera” “%dest%\Opera\Roaming” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\opera.log /NDL /NP
robocopy “C:\Users\Acidx\AppData\Local\Opera\Opera” “%dest%\Opera\Local” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG+:%dest%\opera.log /NDL /NP

REM ===== Backup Miranda messenger =====
robocopy “C:\Program Files\Miranda” “%dest%\Miranda” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\miranda.log /NDL /NP

REM ===== Backup Tools, excluding shortcuts =====
robocopy “C:\Users\Acidx\Desktop\Tools” “%dest%\Tools” /MIR /R:2 /W:2 /DCOPY:T /ETA /TEE /LOG:%dest%\tools.log /NDL /NP /XF *.lnk

REM ===== Finished =====
pause
[/CC]

Caution: make sure that the source and destination directories are correct before you execute your backup script. If your data structure has changed, i.e. if you have moved some subfolders to another location, all these folders will be deleted in your backup. If you use an external hard disk for your backups, make sure that it has the correct drive letter or change the dest variable at the beginning of the script.

 

Restoration

If you have lost some files, you can use Robocopy to restore these from the backup. In this case you have to use the appropiate file selection options. Here are a few examples:

Parameter Description
/XN Excludes newer files
/XO Excludes older files (the newer copy will survive)
/MAXAGE:<N> Excludes files older than N days or date
/MINAGE:<N> Excludes files newer than N days or date

Caution: Automated selective file restoration is a critical process, so you should put a lot of thought in it to prevent loss of data. It is highly recommended to run some simulations first in order to see if everything works as desired. Add the /L option to run a simulation and remove /NFL and /NDL to see which files will be touched.

 

Example Restoration Script

Let’s take the following situation as an example:

  • C:\Test\Data contains your important data
  • C:\Test\Backup contains a backup of C:\Test\Data created by Robocopy with the /MIR option
  • File A.PDF accidentally gets deleted from C:\Test\Data, file B.DOCX has been changed recently and file C.TXT has been added

[CC lang=”bash” line_numbers=”false”]
robocopy.exe “C:\Test\Backup” “C:\Test\Data” /E /R:2 /W:2 /DCOPY:T /TEE /LOG:C:\Test\log.txt /NP /XO /XN
[/CC]

What happens if the above script is executed?

  • A.PDF will be restored because it is present in the source (Backup) and missing in the destination (Data)
  • B.DOCX will not be touched because the version in the source (Backup) is older than the one in the destination (Data) [without the /XO option it would be restored to the old version]
  • C.TXT will not be touched because it is an “extra” file that only exists in the destination [with the /MIR or /PURGE option it would be deleted]
  • The /XN option is not implicitly necessary in this case; it would prevent files in the destination (Data) from being overwritten by manipulated files from the source (Backup)

Disclaimer: this worked for me, but you should test this for yourself before you restore any backups. I can’t be held responsible for any loss of data!

 

Understanding The Log Files

The logfile starts with some basic information: start time, source, destination and the used options. Then there’s a list of files/folders that have been processed (unless you used /NFL and/or /NDL) and at the end you’ll find the end time and a summary that looks like this:

[CC]
.           Total    Copied   Skipped  Mismatch    FAILED    Extras
Dirs  :        34         9        25         0         0         3
Files :       601       344       257         0         0       153
Bytes :   5.299 g   4.454 g  866.12 m         0         0   58.46 m
Times :   0:01:45   0:01:33                       0:00:00   0:00:12

Speed :            51057852 Bytes/sec.
Speed :            2921.553 MegaBytes/min.
[/CC]

What does this mean?

  • Total: The backup consists of 34 folders, 601 files and has a size of 5.299 GB
  • Copied: 9 new folders and 344 files with a size of 4.454 GB have been added
  • Skipped: 25 folders and 257 files were skipped because they already existed in the backup
  • Mismatch: No idea 😀
  • Failed: 0 files and 0 folders were skipped due to errors
  • Extras: 3 folders and 153 files with a size of 58.46 MB have been deleted from the backup because they no longer exist in the source

3 Comments.[ Leave a comment ]

  1. Excellent. I have read many articles on Robocopy but none of them explain how to restore properly as you mentioned, for example when I add a file or update a file.

    Thanks.

  2. Thanks for explaining the restore part. Most of the articles out there only talk about backup and none of them even mention anything about restore.

  3. Not a good idea to use robocopy for backup. I know it is free tool but it lead to missing files like in my case it crashed a lot which lead to files missing. Try using GS Richcopy 360 instead, its incremental backup feature makes the process even faster plus it has an easy to use GUI. Try it, thanks!

Leave a Comment