Loading

Wednesday, November 18, 2009

Robocopy Examples

Examples of Microsoft's Robocopy (robocopy.exe) syntax.

Robocopy Example 1: Copy files from one computer to another, skipping files already in the destination.
robocopy \\Computer1\reports \\Computer2\backup *.doc /S
Robocopy Example 2: List files over 32 MBytes in size.
robocopy C:\xyz /MAX:33554432 /L
Robocopy Example 3: Move files over 14 days old (note the MOVE option will fail if any files are open and locked).
robocopy C:\origin C:\destination /move /minage:14
Robocopy Example 4: /MIR is an option to ROBOCOPY where you mirror a directory tree with all the subfolders including the empty directories and you purge files and folders on the destination server that no longer exists in source.
robocopy \\sourceserver\share\path \\destinationserver\share\path /MIR
Or
robocopy source-drive:\path destination-drive:\path /MIR
Robocopy Example 5: The following command will mirror the directories using robocopy:
robocopy \\SourceServer\Share \\DestinationServer\Share /MIR /FFT /Z /XA:H /W:5
/MIR specifies that robocopy should mirror the source directory and the destination directory. Note that this will delete files at the destination if they were deleted at the source.
/FFT uses fat file timing instead of NTFS. This means the granularity is a bit less precise. For across-network share operations this seems to be much more reliable - just don't rely on the file timings to be completely precise to the second.
/Z ensures robocopy can resume the transfer of a large file in mid-file instead of restarting.
/XA:H makes robocopy ignore hidden files, usually these will be system files that we're not interested in.
/W:5 reduces the wait time between failures to 5 seconds instead of the 30 second default.

Robocopy Example 6: use robocopy to copy all changes to files in a directory called c:\data to a directory that contains the date, like data_20091124.  Create a batch file as follows.
@echo off
set day=%date:~0,2%
set month=%date:~3,2%
set year=%date:~6,4%
robocopy "c:\data" "c:\backup\data\%day%-%month%-%year%\" /MAXAGE:1
Robocopy Example 7: To mirror the directory "C:\directory" to "\\server2\directory" excluding \\server2\directory\dir2" from being deleted (since it isn't present in C:\directory) use the following command:
robocopy "C:\ directory" "\\server2\ directory" /MIR /XD "\\server2\ directory\dir2"
Robocopy Example 8: Copy a single file:
robocopy <Source Dir> <Destination Dir> <File Name> <Switches> 
Robocopy can be setup as a simply Scheduled Task that runs daily, hourly, weekly etc. Note that robocopy also contains a switch that will make robocopy monitor the source for changes and invoke synchronization each time a configurable number of changes has been made. This may work in your scenario, but be aware that robocopy will not just copy the changes, it will scan the complete directory structure just like a normal mirroring procedure. If there are a lot of files & directories, this may hamper performance.

More Robocopy
robocopy commands
robocopy syntax

2 comments:

  1. good morning

    during robocopy work (with /sec option) if i have an access denied error
    the robocopy stop work or the robocopy continue work and this error is registered in log file ?

    other question
    is possible obtain a log file less descriptive ( example only number of file copied and error)?

    thanks Alberto

    ReplyDelete
  2. Note that in example #5, reducing the wait time from the default of 30 seconds to the smaller wait time of 5 seconds will have little effect during the lifespan of human beings because the default number of retries is still 1,000,000 (one million retries). That means that if a file is locked or busy, Robocopy will not move on to the next file until 5,000,000 seconds later - that's about 58 days. Your backup will be pretty stale by that time. Adding the /R: switch with a number of retries to attempt will correct this -- example: /R:5 (five retries). Five retries at /W:5 five second intervals will total 25 seconds of waiting before skipping the file and moving on.

    ReplyDelete