>_ IzzyLab

Hands-on task

This task is step 31 of the course “Bash scripts: from a loop to a robust script”

A robust script

hard~25 min

Course finale: build a script you wouldn't be afraid to put in cron. Write an executable /usr/local/bin/snapshot that packs a directory into a tar.gz archive. The script is handy to write in the Editor tab above the terminal: the /usr/local/bin directory is open there next to /root.

Usage: snapshot [-w SECONDS] -o ARCHIVE DIR

  • -o ARCHIVE — required option: where to put the finished archive;
  • -w SECONDS — optional pause before packing (default 0), simulates long-running work;
  • DIR — exactly one positional argument; the path may contain spaces.

Order of work: take a lock on the file /run/snapshot.lock with flock → create a temporary file /tmp/snapshot.XXXXXX with mktemp → wait for the -w pause → pack the directory into the temporary file → move it to ARCHIVE.

Requirements:

  1. Unknown option, option without a value, no -o, no directory or an extra argument — a line containing the word usage on stderr and exit code 2.
  2. If another instance is already running — a message on stderr and exit code 3 right away: create nothing and don't touch the running instance's files.
  3. No temporary file is left behind after success, after an error, or after SIGTERM or SIGINT. On a signal, the script exits with a non-zero code and no archive appears.
  4. Strict mode: a failed command (including inside a pipeline) and a reference to an unset variable stop the script with a non-zero code — for example, for a nonexistent directory there must be no archive.
What you'll practise:
  • getopts
  • trap
  • flock
  • set -euo pipefail
  • mktemp
  • signals
  • exit codes
  • bash scripts

Topic breakdown

A robust script isn't one option but several safeguards, each against its own kind of trouble. Strict mode (set -euo pipefail) stops the script from carrying on in a broken state. getopts parses options uniformly, and exit code 2 for a usage error is a long-standing convention among utilities: "you called me wrong". An flock lock on an open descriptor blocks a parallel run from cron, and the kernel releases it by itself when the process exits. trap cleans up temporary files on any exit; a signal handler needs an explicit exit, otherwise bash continues with the next command after it. Finally, writing to a temporary file followed by mv guarantees that a half-written file never shows up at the ARCHIVE path.

What's checked

  1. a successful run builds an archive of the directory (path with a space)
  2. the temporary file is removed after a successful run
  3. usage errors: usage on stderr and code 2
  4. packing failure: non-zero code, no archive and no temporary file
  5. strict mode: failed commands, unset variables, pipelines
  6. a second concurrent run is rejected with code 3, the first one finishes
  7. SIGTERM mid-run: non-zero code, no archive and no temporary file
  8. SIGINT mid-run: non-zero code, no archive and no temporary file

What it looks like