TT Lab
Blog Get started
Learn Learning paths Courses

SSH and File Transfer

Synchronising With rsync

Continue in TT Lab

Goal

You confirm rsync's key options by hand. In particular you build three things yourself: the trailing slash rule, the danger and guard of --delete, and a --link-dest generational backup. At the end, you take three mirror jobs that "resend everything every day even though nothing changed" and fix them by cause using the itemize output.

Why it matters

rsync is the tool used most and gotten wrong most. The fact that -a does not include ACLs, extended attributes, or hard links, the fact that one slash at the end of the source path changes the directory depth, and the fact that --delete wipes out the target when the source is empty — all three have led to real incidents. And rsync's efficiency depends on the quick check that looks only at size and modification time and moves on. If you do not preserve times, turn the comparison off, or rewrite the source every time, everything goes again every day even though nothing changed. --link-dest, on the other hand, is rsync's most beautiful feature. Each generation looks like a full snapshot while the disk is occupied only by the changes.

Steps

  1. Create app/main.py, app/util.py, conf/app.yaml, logs/app.log, and logs/error.log under /root/sync/src. Put at least one line of any content in each file, and set the modification time of conf/app.yaml to 2026-01-01 00:00:00 (UTC).
  2. Run a dry-run of synchronizing the contents of /root/sync/src into /root/sync/dst1, with the itemized list of changes shown, and save it to /root/sync/dryrun.txt.
  3. Actually perform the same synchronization over SSH. The target is root@127.0.0.1:/root/sync/dst1/, port 2222, and the key is /root/.ssh/id_labhub. After it completes, /root/sync/dst1/app/main.py must exist.
  4. Demonstrate the trailing slash difference. If you send /root/sync/src (no slash) to /root/sync/dst2/, /root/sync/dst2/src/app/main.py must appear. The whole source tree must arrive with the same content and the same modification times (attributes preserved), and links that point back to the source do not count as copies.
  5. Write the script /root/sync/backup.sh. It must start with set -euo pipefail, include a check that aborts with exit code 1 if the source is empty, and synchronize /root/sync/src/ to /root/sync/dst3/ using --delete. The source path is taken as the first argument, and if there is no argument, it uses /root/sync/src/ — the grader passes an empty directory as the first argument to check that the guard really runs. Run the script to create /root/sync/dst3.
  6. Synchronize /root/sync/src/ to /root/sync/dst4/ excluding what is under logs/. /root/sync/dst4/logs must not exist, and every other file must arrive with the same content and the same modification times (attributes preserved).
  7. Make a generational backup. First do a full synchronization of the source's contents to /root/sync/gen/daily.1, and then, with --link-dest using that generation as the base, create /root/sync/gen/daily.0. For files that did not change, the two generations must share the same inode.
  8. Make /root/sync/final.txt with the following 3 lines. PENDING=<3번과 같은 동기화를 다시 dry-run 했을 때 전송 예정 파일 수> / DST4_HAS_LOGS=no / SHARED_INODE=yes (PENDING= is followed by the number of files that would be transferred when you dry-run the same synchronization as step 3 again) PENDING must be 0.
  9. Incident: incident ss-rsync-resend. Three mirror jobs (app, conf, export) resend everything every night. Leave what the jobs do as it is, fix them so they send nothing when nothing changed, and for each job write a cause code and one line of evidence to /root/incident/ss-rsync-resend/report.txt.

Notes

Creating the source tree

Create the following under /root/sync/src: app/main.py, app/util.py, conf/app.yaml, logs/app.log, and logs/error.log. Put at least one line of any content in each file, and set the modification time of conf/app.yaml to 2026-01-01 00:00:00 (UTC).

If you mix subdirectories and log files, the exclude rule in a later step has meaning. You can set a file's modification time to any moment you want without changing its content (touch's time option). The Pod's time zone is UTC.

Simulate first

Run a dry-run of synchronizing the contents of /root/sync/src into /root/sync/dst1, with the itemized list of changes shown, and save it to /root/sync/dryrun.txt (the slash at the end of the source path decides "the contents" versus "the directory itself").

Use together the option that only shows what would change without making actual changes and the option that shows the changes one per line.

Actual synchronization over SSH

Actually perform the same synchronization over SSH. The target is root@127.0.0.1:/root/sync/dst1/, port 2222, and the key is /root/.ssh/id_labhub. After it completes, /root/sync/dst1/app/main.py must exist.

rsync has an option that specifies the whole remote shell command. Put the port and key inside it.

Demonstrating the trailing slash difference

Demonstrate the trailing slash difference. If you send /root/sync/src (no slash) to /root/sync/dst2/, /root/sync/dst2/src/app/main.py must appear. The whole source tree must arrive with the same content and the same modification times (attributes preserved), and links that point back to the source do not count as copies.

Send the same source to two targets, changing only the presence of the slash, and compare the resulting structures.

--delete with a guard

Write the script /root/sync/backup.sh. It must start with set -euo pipefail, include a check that aborts with exit code 1 if the source is empty, and synchronize /root/sync/src/ to /root/sync/dst3/ using --delete. The source path is taken as the first argument, and if there is no argument, it uses /root/sync/src/ — the grader passes an empty directory as the first argument to check that the guard really runs. Run the script to create /root/sync/dst3.

Put a check at the front of the script that aborts immediately if the source is empty. And set -euo pipefail too.

Applying an exclude rule

Synchronize /root/sync/src/ to /root/sync/dst4/ excluding what is under logs/. /root/sync/dst4/logs must not exist, and every other file must arrive with the same content and the same modification times (attributes preserved).

You can specify exclude patterns several times or read them from a file. The log files must not be in the target.

Hard-link generational backup

Make a generational backup. First do a full synchronization of the source's contents to /root/sync/gen/daily.1, and then, with --link-dest using that generation as the base, create /root/sync/gen/daily.0. For files that did not change, the two generations must share the same inode.

If you give --link-dest a relative path, it is interpreted relative to the destination. An absolute path is safe. Check with the link count of stat.

Proof that synchronization is complete

Make /root/sync/final.txt with the following 3 lines. PENDING=<3번과 같은 동기화를 다시 dry-run 했을 때 전송 예정 파일 수> / DST4_HAS_LOGS=no / SHARED_INODE=yes (PENDING= is followed by the number of files that would be transferred when you dry-run the same synchronization as step 3 again) PENDING must be 0.

If it was synchronized correctly, running the same command again as a dry-run should leave nothing to transfer.

Incident: nothing changed, yet everything is resent every day

Running incident ss-rsync-resend shows a ticket. Three mirror jobs (app, conf, export) resend all their files every night even though the source has barely changed. Fix them so they send nothing when nothing has changed. What the jobs do (source, destination, remote shell, --stats) and the content the generator produces must stay the same, and on a day when the source really changes, that file must go across. You must not change the mirror server (127.0.0.1:2244) configuration or /srv/ss-sync/common.sh. In /root/incident/ss-rsync-resend/report.txt, write one line per job, such as app: <코드> (code), plus evidence: <관찰한 출력 한 줄> (one line of output you observed). Candidate codes: checksum-scan (-c reads and compares every file every time), ignore-times (the quick check is turned off, so everything is sent every time), no-times (modification times are not preserved, so the next comparison always disagrees), source-rewritten (the source side is rewritten every time with the same content, so its time changes), whole-file (-W sends whole files instead of only the differences).

Direction: look not at how many files were sent (--stats) but at why they were sent; rsync prints the reason for each file as eleven characters. Even the same "everything" has three reasons. Tools: ss-sync (reproduce the symptom), the itemize lines in /var/log/ss-sync/.log, the rsync options in the job scripts, and stat to compare the modification times of the source and the copy. Concretely: look at what the fifth character at the start of the line (t/T) means, why lines with only dots are sent, and when the time of the source-side file changes (when and what the generator writes). After fixing, run it twice in a row and confirm the second run sends 0.