ZNHOO Whatever you are, be a good one!


Robust File Copy for Windows command robocopy can mirror or incrementally backup files/directories, with versitle parameters.

The basic usage of robocopy is like:

robocopy <source-dir> <dest-dir> [file [file] ...] [options]

# robocopy d:\work e:\back *.txt *.doc *.bmp *.tif /s

Attention please: Windows does not differentiate letter case. Hence, /s and /S have the same effect.

By default, file/directory to copy can be wildcards like * (default to *.*; omitted usually). Read Robocopy 指令範例參考 (MIR問題很多,小心使用) or check robocopy /?.

robocopy will copy any name where the Timestamp or file size differs:

  1. Timestamp refers to any of Creation Date, Last Modified Date or Last Access Date.
  2. It does not care about whether the source or the dest is newer.
  3. It does not count on any hash functions.
  4. In order to avoid unnecessary copy, we should tune arguments accordingly.

The following is an example that mirrors a disk parittion:

# /MIR = /E /PURGE
F:\>robocopy D:\ E:\ /MIR /XO

F:\>robocopy D:\ E:\ /MIR /XO /COPY:DAT /DCOPY:DAT [/MINAGE:1] /MAXLAD:7 /XA:SH /XD "System Volume Information" "$RECYCLE.BIN" /R:3 /W:10 /MT:16 /UNILOG+:F:\robocopy.log /TEE /Z
  1. /MIR mirrors a directory tree, equivalent to /E /PURGE.
  2. /XO skips older files from the source.
  3. /COPY:DATS copies Data, Attributes, Timestamp and NTFS Security ACLs fo files.

    By default, Security info of NTFS is not copied. However, the system may deny this for security considerations.

  4. /DCOPY:DAT copies Data, Atrributes and Timestamp of directories.

    By default, Timestamp is not copied.

  5. /MINAGE:n and /MAXAGE:n exclude files according to their Last Modified Date.

    MINAGE:1 guarantees that files being updated today won't copied. On the contrary, MAXAGE skip files old enough.

    n can be the number of days or date in YYYYMMDD format. It should adapt to the bakcup cron job.

  6. /MINLAD:n and /MAXLAD:n exclude files according to their Last Access Date.
  7. /XA:SH excludes files with Hidden or System attributes.

    In return, we have /IA: to copy only files with the specified attributes.

  8. /XD <dir> excludes specific directories.

    In return, we have /XF to exclude specific files.

  9. /R:3 retries 3 times on failed copies. By default, it is 1 million.

    /W:10 wait for 10 seconds between retries.

  10. /MT:16 creats 16 concurrent threads to copy files.
  11. /UNILOG+:F:\robocopy.log redirects log to file with UNICODE encoding.
  12. /Z runs in restartable mode such that we can resume the transfer in case it is interrupted.

The Linux alternative of robocopy is rsync. Please check fmt.pdf.