Project: a backup script

The backup script is a handful of decisions rather than a lot of code: build a filename from the date, create the destination safely, refuse to run twice on the same day, and report the outcome with a status code another program can read.

4 questions 1 task Read the lesson

Questions

Each one is marked as you go, and the explanation arrives straight after, which is the part worth reading.

4 questions, marked as you go.

1 What does mkdir -p "$HOME/backups" do when the directory already exists?
2 In tar -czf "$ARCHIVE" -C "$SRC" ., what is the -C "$SRC" for?
3 The script tests [ -f "$ARCHIVE" ] and exits 0 when the archive already exists. Why exit 0 and not 1?
4 Why does the script test [ ! -d "$SRC" ] before it runs tar?

Tasks

These need your own device. Run the command in Termux, then paste what it printed. The marking looks at what the output means, so spacing, version numbers and ordering do not matter. If you have not installed anything yet, the lesson covers the setup.

Build and list a dated archive

This makes a small source folder, packs it into a dated archive the way the script does, then lists the archive back to confirm what went in. The date in the filename is today.

Run this in Termux On your own device. Nothing on this page executes it. The command runs in your terminal, and this box only reads what you paste back.

mkdir -p "$HOME/notes" "$HOME/backups"; printf 'call the plumber\n' > "$HOME/notes/todo.txt"; STAMP=$(date +%Y-%m-%d); ARCHIVE="$HOME/backups/notes-$STAMP.tar.gz"; tar -czf "$ARCHIVE" -C "$HOME/notes" .; echo "wrote $ARCHIVE"; tar -tzf "$ARCHIVE"
Marking your own work Both halves of this are checked in your browser, with no server involved, so nothing stops you from pasting output you did not run. The tasks are written so that the easiest way to pass one is to actually do it and read the result, which is the whole intention.

All sets Read the lesson