Saturday, August 24, 2013

Robocopy script for copying and synchronizing files and folders when migrating Windows file servers

I’m not sure why I haven’t blogged this Robocopy script that I’ve been using over the last 10 years to migrate servers until today when I had trouble finding it because it was in my old emails.  In any case, the following script will mirror the directories between the source and destination servers.  Note that this script uses the /MIR switch which will ensure that the destination folder is completely the same as the source.  This means that if there is a file on the destination folder that isn’t found on the source, it will get deleted so if you accidentally try to mirror an empty source folder that doesn’t contain files with a destination folder that does, all the contents will get deleted!

With dangerous of this script stated, I typically use this script to continuously synchronize files and folders along with the NTFS permissions from an older production file server and a to become production new file server until the night of the cutover when I perform one final synchronization then migrate over.  For information on the switches that are being used, see the following URL:


FOR /f "tokens=1-4 delims=/ " %%a in ('date /t') do set vl=%%b%%c%%d
FOR /f "tokens=1-4 delims=/: " %%a in ('time /t') do set tl=%%a_%%b%%c%%dm

SET _source="\\sourceServerName\f$"

SET _dest="\\destinationServerName\f$"

:: /COPYALL :: COPY ALL file info
:: /B :: copy files in Backup mode.
:: /SEC :: copy files with SECurity
:: /E :: copy subdirectories, including Empty ones
:: /MIR :: MIRror a directory tree
:: /Z :: Copy files in restartable mode (survive network glitch).

SET _options=/XO /XF *.LCK /R:5 /W:5 /LOG:Log-SourceServertoDestinationServer_%vl%_%tl%.txt /NFL /NDL /IPG:5
:: /XO :: eXclude Older - if destination file exists and is the same date or newer than the source - don't bother to overwrite it.
:: /XF :: eXclude Files matching given names/paths/wildcards.
:: /R:n :: number of Retries
:: /W:n :: Wait time between retries
:: /LOG :: Output log file
:: /NFL :: No file logging
:: /NDL :: No dir logging
:: /IPG:n :: Inter-Packet Gap (ms), to free bandwidth on slow lines
:: /MON:n :: minimum number of changes that must occur before Robocopy runs again
:: /MOT:m :: minimum time, in minutes, that must elapse before Robocopy runs again

ROBOCOPY %_source% %_dest% %_what% %_options%


One of the other switches I commonly use that is not included in the script is the:

/xd <Directory>[ ...]

… that is used to exclude full directories.  This switch comes in handy when you’re trying to copy contents between DFS shares where there a DFS system subfolders handling conflicts.


Unknown said...
This comment has been removed by a blog administrator.
Unknown said...

Nice post.Another software alternative that works is "LongPathTool" for error messages: Cannot read from source file or disk, there has been a sharing violation, cannot delete file or folder, the file name you specified is not valid or too long, the source or destination file may be in use and many other file managing errors.

Mr.john Elder said...

Nicely presented information in this post, I prefer to read this kind of stuff. The quality of content is fine and the conclusion is good. Thanks for the post.
data replication

Brian Klish said...

This doesn't seem to work for me when trying to copy folder redirection folders. It runs, but doesn't seem to respect the /B option to perform backup mode. This results in a whole lot of access denied in the log because only the person that owns the content has access by default. When I opened the log file I saw these are the only options being specified:

Options : *.* /NDL /NFL /S /E /DCOPY:DA /COPY:DATS /PURGE /MIR /Z /XO /IPG:5 /R:5 /W:5

Somehow the batch file logic is losing some arguments when it passes it to Robocopy.

Brian Klish said...

I'm using these options and it seems to work better. It also does multi-threading so it goes faster.


Anonymous said...

use robocopy

Unknown said...

Copying or keeping the file permission after the transfer is a great feature which the windows does not provide. After searching for software to do this task, I found GS Richcopy 360 which does this task effectively and also maintains the timestamps of the original file system. Hope this comment helps someone!