[ norboten ]
bash · lab bash-03

The Backup That Skips Files

· about 30 minutes · runs on ubuntu-26.04-devops · unrated

An unrated lab. It runs on your machine with no account and no network, and everything about it — the faults, the checks, the hints and the reference solution — is in the repository. An attempt on it is recorded on your profile and never moves a rating: only rated labs do. Rated and unrated labs.

The briefing

backup-docs copies every file under /srv/docs into /var/backups/docs/<YYYY-MM-DD>/, keeping the relative paths. backup-docs.timer runs it shortly after boot and every night.

Last week legal asked for last month's contract, Contract - ACME.pdf. It was not in any backup. The job's journal says backed up 0 files every night, systemd lists every run as successful, and nobody had looked at it since it was written.

What is expected, and graded — the grader runs backup-docs itself, as an ordinary user, against its own directories, through the DOCS_DIR and BACKUP_DIR variables the script already reads:

  1. Every regular file is copied, byte for byte, to the same relative path under today's directory — whatever its name: spaces, a leading dash, leading blanks, a backslash, even a newline.
  2. The last line of output is backed up N files, where N is the number of files it copied.
  3. When a file cannot be copied, the script names it on standard error, still copies the rest, and exits non-zero.
  4. The run started by backup-docs.timer succeeds and backs up everything in /srv/docs — and still does after a reboot.

You have root through sudo.

What is graded

The machine is checked, rebooted, and checked again. A check passes only if it passes both times.

CheckObjective
01_every_file_is_copiedWalk a directory tree in a shell script without breaking on any file name
02_the_count_is_trueKeep a count across a loop, and report failures with an exit status
03_a_failed_copy_is_reportedKeep a count across a loop, and report failures with an exit status
04_the_nightly_run_succeedsRun a script from a systemd timer and prove the run succeeded

Start it

  1. 2Labs
  2. bash-03select this one
  3. udownload it
  4. sstart the machine

Reading

Where the lab's hints send you, level by level, as you ask for them (h, then l opens a journal section in the TUI).

Theory for this lab

4 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Three of them, to answer here:

Question 1

What does this print?

count=0
printf 'a\nb\nc\n' | while read -r line; do count=$((count + 1)); done
echo "$count"
Question 2

What does this print?

printf '%s\n' 'Q3\forecast.csv' | while read f; do echo "$f"; done
Question 3

A loop reads file names with `find "$dir" -type f | while read f`. Which of these names can reach the loop body changed or split? Select all that apply.