Robocopy to Copy Files with Permissions and Ownership

Random

If you need to copy files from one storage location to another Robocopy is a great tool, but like every great tool it comes with loads of options. In this scenario I was needing to copy a directory structure (and all the files) from one location to another but preserving all the permissions, ownership and timestamps of the data. Its recommended to copy rather than move just in case something goes wrong.

robocopy source destination /E /ZB /DCOPY:T /COPYALL /R:1 /W:1 /V /TEE /LOG:Robocopy.log

The switches mean the following:

  • source – Source Directory (drive:\path or \\server\share\path)
  • destination – Destination Directory  (drive:\path or \\server\share\path)
  • /E – Copies the subdirectories, including empty ones
  • /ZB – Use restartable mode; if access denied use backup mode
  • /DCOPY:T – Copy the directory timestamps
  • /COPYALL – COPYALL file information (equivalent to /COPY:DATSOU), this copies the Data, Attributes, Timestamps, Owner, Permissions and Auditing information.
  • /R:n – The number of retries on failed copies. By default this is set to 1 million, but normally 1 to 5 seems sensible.
  • /W:n – The wait time between retries. Default is 30 seconds, but 5 seconds might be sensible here.
  • /V – Produce verbose output, showing any skipped files.
  • /TEE – Produces output to console window, as well as to the log file.
  • /LOG:file – Output status to the log file (overwrite existing log), handy to see what actually happened.

You may well require “Administrator” or “Backup Operator” privileges on the source and destination when using the /B switch this ensures you don’t end up “owning” the files at the end.