380 字
2 分鐘
Robocopy Tutorial: A Powerful and Stable Backup Tool
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.
/mirmakes the destination exactly identical to the source (including deletions), suitable for ‘true backup’ rather than simple copying.- The command is as follows:
robocopy "來源資料夾" "目的地資料夾" /mir2. Add Exclusions and Performance Optimization
- Exclude unnecessary files (e.g., .bak, .scc) and folders (bin, obj, node_modules).
/MT:100enables multi-threading for acceleration, which makes a big difference with large numbers of files./xjavoids infinite recursion for Junction points (common in node_modules).
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:7limits synchronization to changes within the last 7 days, reducing unnecessary I/O.- Output results to a log file for easier troubleshooting or auditing.
robocopy "來源資料夾" "目的地資料夾" /mir /xj /MT:100 /xf *.scc *.bak ^ /xd "packages" "bin" "obj" "node_modules" ".vs" "TempFile" "Data" "history" ^ /maxage:7 > .\Log\%dd%.logAdditional Notes
/mirwill delete extra files in the destination. Misuse can directly ‘empty’ your destination.- While
/MTis 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
:: 建立 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%.logWrapping 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
/mirbehavior before deploying to a production environment.
Robocopy Tutorial: A Powerful and Stable Backup Tool
https://joyceowo.github.io/posts/en/23ba78ea09fa81e7a124eac97b80afce/