M45 - Scripting Lab: Backup Automation
Scripting Lab: Backup Automation
Use robocopy or rsync to synchronize a practice folder, log the result, and understand why mirror operations need caution.
- Use robocopy or rsync to synchronize a practice folder, log the result, and understand why mirror operations need caution.
The Goal: A Repeatable Backup Workflow
This lab introduces a simple but important idea:
- copying a folder once is not the same as maintaining a synchronized backup
Backup tools such as robocopy and rsync exist because repeated copy jobs need better behavior:
- skip unchanged files
- preserve structure
- log what happened
We will keep the learning scenario small. Use practice folders first, not your real system data.
1. Start with Two Practice Folders
Set up a source folder and a destination folder you can safely inspect.
Examples:
- Windows:
C:\Users\You\Documents\backup-sourceandD:\backup-dest - Linux:
$HOME/backup-sourceand$HOME/backup-dest
Add a few small test files to the source so you can clearly see what changes after each sync.
2. Windows with robocopy
Robocopy is Windows’ standard folder synchronization tool. It is useful because it handles repeated copy work more deliberately than basic file-copy commands.
robocopy “$HOME\Documents\backup-source” “D:\backup-dest” /E /LOG+:“$HOME\Documents\backup-audit.log”
This first pattern is intentionally conservative:
/Ecopies subfolders, including empty ones/LOG+appends results to a log file
Mirror mode exists, but treat it carefully:
robocopy “$HOME\Documents\backup-source” “D:\backup-dest” /MIR /LOG+:“$HOME\Documents\backup-audit.log”
The Linux equivalent below uses rsync.
/MIR can delete files from the destination if they no longer exist in the source. That can be exactly what you want, but only once you are certain your source and destination are correct.
3. Linux with rsync
rsync is a standard Linux sync tool for local and remote copies.
The Windows example above shows the same workflow with robocopy.
rsync -av “$HOME/backup-source/” “$HOME/backup-dest/” >> “$HOME/backup-audit.log” 2>&1
This is a good first learning pattern:
-apreserves useful file metadata-vshows what was copied- the output is saved to a log file
Delete synchronization is powerful and should be tested first:
rsync -av —delete “$HOME/backup-source/” “$HOME/backup-dest/” >> “$HOME/backup-audit.log” 2>&1
On Linux, the trailing slash matters:
- source folder without trailing slash means copy the folder itself
- source folder with trailing slash means copy the folder contents
4. Log the Result and Compare the Folders
After each run:
- inspect the destination folder
- inspect the log file
- change one source file
- run the sync again
- confirm the tool copied only what actually changed
That feedback loop teaches more than memorizing flags.
Practice Before Mirror
Delete-style sync flags such as /MIR and —delete are not “advanced because they are cool.” They are advanced because they can remove data in the destination. Use them only after you have confirmed the source, destination, and expected behavior on practice folders.
What You Just Learned
- Backup automation is really about repeated synchronization, not just one-time copying.
robocopyandrsyncare better tools than basic copy commands for repeated folder sync work.- Logs matter because unattended backup jobs need evidence.
- Mirror and delete flags are useful but must be tested carefully.
- Practice folders are the right place to learn backup logic before touching real data.
Next, the curriculum shifts back into system administration topics such as logs, services, and startup behavior.