Generational Backups With rsync
Goal
With rsync, set up mirror synchronization and generation backups, and build them into a production-ready form, including a --delete guard and load control.
Why it matters
The --link-dest generation backup is rsync's most elegant feature. Each generation directory looks like a complete snapshot, while the disk is occupied only by the changes. The principle is hard links, which is why measuring with du gives a much smaller number than you would expect — it does not count the same inode twice.
And --delete must always be used with a guard. If it runs while the source is empty or the mount has come undone, it deletes all the data on the target. It is an accident that genuinely keeps recurring in practice.
In the final incident, three mirrors break in one night in the same way: "files that should be there are missing". rsync only did exactly what it was told, so there are no errors either. That is why you must look for the cause not in rsync but in the source and arguments rsync received that night, and after fixing it you must run it yourself to confirm "will the same thing happen tonight?".
Steps
- Under
/root/rs/src, createapp/main.py,conf/app.yaml,logs/app.log, andtmp/cache.bin, and synchronize to/root/rs/dst/. As a result,/root/rs/dst/app/main.pymust exist, anddstmust be a copy that holds the whole source tree with the same content and the same modification times (attributes preserved). - Run it as dry-run plus itemize and save the output to
/root/rs/itemize.txt. The target must be a new directory that has not been synchronized yet — if you aim at thedst/from step 1 again, it is already in the same state, so rsync prints no lines and the file ends up empty. Itemize is a tool for seeing "what changes and why," so there has to be something that will change for there to be something to see. - Write
/root/rs/backup.sh. It must start withset -euo pipefail, abort with exit code 1 if the source is empty, and use--deleteto synchronize/root/rs/src/to/root/rs/mirror/. The source path is taken as the first argument, and if there is no argument it uses/root/rs/src/— the grader passes an empty directory as the first argument to confirm that the guard actually works. Run it to create/root/rs/mirror. - Write the exclusion patterns in
/root/rs/exclude.txt(logs/andtmp/), and use it to synchronize to/root/rs/clean/./root/rs/clean/logsand/root/rs/clean/tmpmust not exist. - Create a generation backup. After a full synchronization to
/root/rs/gen/daily.1/, use--link-destto create/root/rs/gen/daily.0/. Files that did not change must share the same inode between the two generations. - Use
--numeric-idsto synchronize to/root/rs/numeric/, and write the owner UID ofapp/main.pyas a single line of digits in/root/rs/uid.txt. - Write
/root/rs/throttled.sh. It must use an rsync command that includes all three ofionice,nice, and--bwlimit(a nonzero value) to match/root/rs/src/into/root/rs/slow/. Run it to create/root/rs/slow/. The grader runs the script again and records whether the three commands are actually called, so call the commands by name. - Incident: raise
incident bk-mirror-wiped, work out for each mirror why files disappeared overnight from the three mirrors (/backup/mirror/media,docs,conf), then restore the three mirrors to their proper state and fix/opt/bkmirror/nightly.shso it does not do the same thing tonight. The report is/root/incident/bk-mirror-wiped/report.txt(candidate codes and the end state are in the step description).
Notes
- Turning on the dry run and the change itemization together gives you, line by line, "what will change and why". Always look at this first before using
--delete. - Exclude patterns can be passed as a list file. Patterns are matched against the whole name, so adding
*at the end catches more names than you think. - You can tell whether something is a hard link from the inode number and link count of the same file in two generations.
- Load control has three directions: CPU priority, disk priority, and a transfer rate cap (the three press on different resources).
- Common mistake 1: the script in step 3 hardcodes the source path and so fails the empty-directory test. Write it to take the source path as the first argument (use a default when there is no argument).
- Common mistake 2: in step 5, if you give
--link-destas a relative path it is interpreted relative to the destination, and the links are not created. - Incident tools:
incident listshows the list, andincident show <이름>(name) shows the ticket (a colleague's report) again. Raising the same incident again returns it to its original broken state. - Write the report to
/root/incident/<이름>/report.txt(name is the incident name): one line<이름>: <원인 코드>(name: cause code) per target, plusevidence: <관찰한 출력 한 줄>(one line of output you observed). The evidence line is checked against the output actually seen in the incident. - Grading does not look only at the report. The actual state of what you restored, such as hashes, ownership, and modes, must be right, and workarounds that only erase the symptom fail.
Basic mirror synchronization
Under /root/rs/src, create app/main.py, conf/app.yaml, logs/app.log, and tmp/cache.bin, and synchronize to /root/rs/dst/. As a result, /root/rs/dst/app/main.py must exist, and dst must be a copy that holds the whole source tree with the same content and the same modification times (attributes preserved).
Remember the trailing slash rule. The source directory name must not appear inside the destination.
Show the changes
Run it as dry-run plus itemize and save the output to /root/rs/itemize.txt. The target must be a new directory that has not been synchronized yet — if you aim at the dst/ from step 1 again, it is already in the same state, so rsync prints no lines and the file ends up empty. Itemize is a tool for seeing "what changes and why," so there has to be something that will change for there to be something to see.
Using dry-run and itemize together prints, one line at a time, what will change and how.
A backup script with a guard
Write /root/rs/backup.sh. It must start with set -euo pipefail, abort with exit code 1 if the source is empty, and use --delete to synchronize /root/rs/src/ to /root/rs/mirror/. The source path is taken as the first argument, and if there is no argument it uses /root/rs/src/ — the grader passes an empty directory as the first argument to confirm that the guard actually works. Run it to create /root/rs/mirror.
If the source is empty, it must stop immediately. Distinguish the cases by exit code.
Exclusion list file
Write the exclusion patterns in /root/rs/exclude.txt (logs/ and tmp/), and use it to synchronize to /root/rs/clean/. /root/rs/clean/logs and /root/rs/clean/tmp must not exist.
Collecting the patterns in a file makes them easy to manage. One pattern per line.
Generation backup
Create a generation backup. After a full synchronization to /root/rs/gen/daily.1/, use --link-dest to create /root/rs/gen/daily.0/. Files that did not change must share the same inode between the two generations.
It is safer to give --link-dest as an absolute path. Verify by the link count.
Keep numeric owners
Use --numeric-ids to synchronize to /root/rs/numeric/, and write the owner UID of app/main.py as a single line of digits in /root/rs/uid.txt.
There is an option that uses numeric IDs instead of names. It is a safeguard for when the restore target system is different.
Load control
Write /root/rs/throttled.sh. It must use an rsync command that includes all three of ionice, nice, and --bwlimit (a nonzero value) to match /root/rs/src/ into /root/rs/slow/, attributes included. Run it to create /root/rs/slow/. The grader runs this script again and records whether the three commands are actually called, so call the commands by name, without paths.
Use the bandwidth limit option together with the command that lowers disk priority. Save the entire command as a script.
Incident: overnight, files that should be there vanished from three mirrors
Running incident bk-mirror-wiped raises the incident. Symptom: after /opt/bkmirror/nightly.sh (log /var/log/bkmirror/nightly.log) ran overnight, files that should be there are missing from all three mirrors, /backup/mirror/media, docs, and conf. Find the cause for each mirror, restore the three mirrors to their proper state (media to the NAS's contents, docs exactly the same as the current source, conf as the source minus only the temporary files *.tmp), and fix nightly.sh so the same thing does not happen tonight. On a night when the NAS is missing, the fixed script must not touch the media mirror and must exit with a nonzero code, and on a night when the NAS is attached it must match the three mirrors correctly; the grader runs both cases directly, giving temporary trees through the NAS, SRV, and DST variables at the top of the script. Whether the NAS is attached is known from the .nas-volume marker file at the top of the share (this lab environment cannot do a real mount, so it uses a marker instead of a mount point check). It is fine to build tonight's generation from yesterday's. Write nothing to the NAS mount point /mnt/nas/media, and leave yesterday's generation snapshot /backup/snap/daily.1 as it is. In /root/incident/bk-mirror-wiped/report.txt, write three lines, media: <코드>, docs: <코드>, and conf: <코드> (code), plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Each mirror's code is one of the following: broad-exclude (the exclude pattern is broader than intended), source-deleted (actually deleted at the source), trailing-slash (the trailing slash on the source path), unmounted-source (an empty mount point with no source attached).
Direction: --delete only makes the target "the same as the source"; it does not ask whether the source is right. For each mirror, first redraw what the source rsync received that night actually was. Tools: the deleting lines in the log, ls -A, find, rsync -ain (preview what it intends to do), diff -rq. Concretely: for media, check whether the mount point is empty and whether the share's marker (.nas-volume) is there; for docs, whether a layer that is not in the source appeared inside the mirror; for conf, which names the exclude pattern catches. With no NAS right now, the place to restore media from is yesterday's generation, and if you move generations while restoring, the base for tonight's generation disappears.