380 字
2 分鐘
瀏覽次數
Robocopy Tutorial: A Powerful and Stable Backup Tool
2025-07-25
2026-04-10

Conclusion#

  • A single Robocopy command can achieve ‘mirror backup + exclusion + Log + acceleration’, suitable for daily automated backups.

Where It’s Suitable For#

  • Regular backups (daily / pre-CI backups)
  • Large file synchronization (project folders)
  • Scenarios requiring exclusion of temporary data like node_modules / bin
  • Scenarios requiring preservation of permissions and timestamps

Process Steps#

1. Create Basic Backup Command#

  • Use robocopy to specify source and destination, and add mirror mode to synchronize the entire data.
  • /mir makes the destination exactly identical to the source (including deletions), suitable for ‘true backup’ rather than simple copying.
  • The command is as follows:
Terminal window
robocopy "來源資料夾" "目的地資料夾" /mir

2. Add Exclusions and Performance Optimization#

  • Exclude unnecessary files (e.g., .bak, .scc) and folders (bin, obj, node_modules).
  • /MT:100 enables multi-threading for acceleration, which makes a big difference with large numbers of files.
  • /xj avoids infinite recursion for Junction points (common in node_modules).
Terminal window
robocopy "來源資料夾" "目的地資料夾" /mir /xj /MT:100 /xf *.scc *.bak ^
/xd "packages" "bin" "obj" "node_modules" ".vs" "TempFile" "Data" "history"

3. Control Sync Scope + Output Log#

  • /maxage:7 limits synchronization to changes within the last 7 days, reducing unnecessary I/O.
  • Output results to a log file for easier troubleshooting or auditing.
Terminal window
robocopy "來源資料夾" "目的地資料夾" /mir /xj /MT:100 /xf *.scc *.bak ^
/xd "packages" "bin" "obj" "node_modules" ".vs" "TempFile" "Data" "history" ^
/maxage:7 > .\Log\%dd%.log

Additional Notes#

  • /mir will delete extra files in the destination. Misuse can directly ‘empty’ your destination.
  • While /MT is fast, setting it too high might max out CPU or I/O. It’s recommended to start testing from 16-64.
  • The %DATE% format is affected by system locale and may need adjustment across different machines.

Command / Example Summary#

Click to expend
Terminal window
:: 建立 Log 資料夾
if not exist ".\Log" mkdir ".\Log"
:: 產生日期(YYYYMMDD)
set dd=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%
:: 完整備份指令
robocopy "來源資料夾" "目的地資料夾" /mir /xj /MT:100 /xf *.scc *.bak ^
/xd "packages" "bin" "obj" "node_modules" ".vs" "TempFile" "Data" "history" ^
/maxage:7 > .\Log\%dd%.log

Wrapping Up#

  • Robocopy isn’t difficult; the challenge lies in ‘parameter combinations’. This set of commands should cover 90% of backup scenarios.
  • It’s recommended to run it in a test folder first to confirm the /mir behavior before deploying to a production environment.
Robocopy Tutorial: A Powerful and Stable Backup Tool
https://joyceowo.github.io/posts/en/23ba78ea09fa81e7a124eac97b80afce/
作者
JoyceOwO
發佈於
2025-07-25
許可協議
CC BY-NC-SA 4.0