[ norboten ]
linux · lab linux-03

The Backup Script That Lies

· about 30 minutes · runs on ubuntu-26.04, alpine · 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

/usr/local/bin/backup-data archives /srv/data into /var/backups every night and writes "backup OK" to /var/log/backup-data.log. It has said "backup OK" every night for a year.

Last week someone needed a restore. The newest archive held a fraction of the files. Nobody can say when that started. Looking at /var/backups, there have been no new archives for days — and the log still says "backup OK" whenever someone runs it by hand.

The script honours three variables, so you can test it safely: BACKUP_SRC (default /srv/data), BACKUP_DEST (default /var/backups) and BACKUP_LOG (default /var/log/backup-data.log).

What is expected, and graded — the script is run against test data you will not see:

  1. Every file under the source ends up in the archive, whatever its name — spaces included.
  2. If the archive cannot be written, the script exits non-zero, says why on stderr, and does not log "backup OK".
  3. If the source directory does not exist, the same: non-zero exit, a message, no "backup OK".
  4. The nightly cron job can actually run the script.

Keep the script in Bash.

What is graded

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

CheckObjective
01_archives_names_with_spacesQuote variables so file names with spaces survive
02_fails_when_destination_unwritableMake a script fail loudly: exit status, error messages, no false success
03_fails_when_source_missingMake a script fail loudly: exit status, error messages, no false success
04_cron_can_run_itRun a script from cron with a working environment

Start it

  1. 2Labs
  2. linux-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

3 questions on the same topic, in the lab's Theory tab. They never affect the lab's grade. Here they are, to answer in place:

Question 1

What does `set -e` NOT catch?

Question 2

What PATH does a cron job get when the crontab sets none?

Question 3

What does this print?

cd /nonexistent-dir 2>/dev/null
echo "status=$?"