← Teacher guide

Keep it running: backups and schedules

A server that nobody looks after fails. Back up what matters with tar to a separate disk, schedule the job with cron, watch the disk space, and test the restore: a backup you have never restored is only a hope.

System admin on a Raspberry Pi · Keep it running · about 40 minutes · Open the student lesson

Classroom resources

Open these in Waypoints Prepare with one click. To share a file another way, copy its address and paste it into Load URL, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.

Students will be able to

Timing

Predict and Try 8 min · Worked example 10 min · Vary 10 min · Practise and Make 12 min

Before class

Try the boards yourselves.

The one idea: running a system means planning for failure: a copy on a different disk, a schedule that runs without you, and a restore you have tested. This is the overall picture of securing a system (AC9TDI10K01 node 5): backups, firewall, accounts and updates each cover a different risk.

Simplifications: tar paths are shown as typed; the pretend /media/usb is a USB drive. crontab -e opens an editor on a real Pi (nano); the board refuses it and takes echo "..." | crontab -, which replaces your whole schedule. Disk sizes are made up.

Other options: rsync copies only what changed and suits big folders; rsnapshot and borg keep many versions.

With internet: off-site backups (rclone to cloud storage, or another Pi at home) protect against fire and theft; automatic security updates (unattended-upgrades) keep the Pi patched; monitoring can email you when the disk is nearly full.

Running the lesson

  1. Predict: wrong answers read the fields as 'every 30 minutes'.
  2. Try: ask what would be lost if the card failed now.
  3. Worked example: stop at the untested backup and ask how you would know.
  4. Vary: after the restore, ask how often it should be tested.
  5. Make: ask what the 3-2-1 rule is (three copies, two kinds of media, one off-site).

In class

On the slides at these points. Students can do the whole lesson on their own; these are for the room. In the deck, the Run, Modify and Make slides have the live playground in them (the tutorial link is the backup), and the notes above are in each slide's speaker notes.

The hard part (on purpose)

The five cron fields. Write them above the line: minute hour day month weekday.

When students get stuck

What you will seeA question to askThe fix
A cron line does not run when expected.Which field is the minute? The hour?Minute, hour, day, month, weekday, in that order.
tar says it cannot find the folder.Where are you? Does the path start from there?cd to the parent, or give the whole path.

Answers to the checks

  1. the schedule: A cron line starts 30 2 * * *. When does it run?
    At 02:30 every day. The fields are minute, hour, day of month, month, day of week; a star means every.
  2. Try: make a backup: Back up the Documents folder into /media/usb/docs.tar.gz with tar -czf. Check it is there with ls -l /media/usb.

    tar -czf /media/usb/docs.tar.gz Documents: c create, z compress, f file name.
  3. Backup, schedule, restore: What does 0 3 * * 0 mean?
    At 03:00 on Sundays
    Minute 0, hour 3, any day, any month, weekday 0 (Sunday).
  4. Find the mistake: Sam schedules a backup. Click the line where it first goes wrong.
    line 2
    Line 2: 15 in the minute field means at minute 15 of every hour, once an hour. Every 15 minutes is */15 * * * *.
  5. A backup you never tested: You have made a backup every night for a year, and never opened one. What do you really know?
    Nothing about whether it restores
    A backup is only proved by restoring from it. Test it, and keep one copy somewhere else.
  6. Schedule it: Schedule a backup at 02:30 every day. Use echo "30 2 * * * tar -czf /media/usb/docs.tar.gz Documents" | crontab -

    echo "30 2 * * * ..." | crontab - loads one line as your schedule. crontab -l shows it.
  7. Restore: Documents/readme.txt was deleted by mistake. Restore it from the backup in /media/usb.

    tar -xzf /media/usb/docs.tar.gz, from your home folder: it unpacks Documents again.
  8. Watch the space: Find the free space on the Pi with df -h, then see which folder is biggest with du -h.

    df shows each disk's space; du shows what a folder takes.
  9. 04:15: Which cron line runs at 04:15 every day?
    15 4 * * *
    Minute first, then hour: 15 4.
  10. Where: Where should the backup of the Pi's files be kept?
    On a different disk, ideally in another place
    A copy on the failing disk is not a backup.
  11. Review: firewall: Review: the firewall is on and port 8096 is not allowed. Can a TV reach Jellyfin?
    No
    Blocked until allowed.
  12. a nightly backup: Back up Documents to /media/usb/backup.tar.gz and schedule the same backup for 02:30 every night.

    Make the archive, then load the cron line that makes it again each night. Then restore from it once, to be sure.

Solutions

One possible solution for each task. Other correct programs are fine.

Vary: change one thing › Schedule it

Open task: any program that does what the step asks.

Vary: change one thing › Restore

Open task: any program that does what the step asks.

Vary: change one thing › Watch the space

Open task: any program that does what the step asks.

a nightly backup

Open task: any program that does what the step asks.

Stretch and support

Stretch: Find out what the 3-2-1 backup rule says.

Support: The five fields written above a cron line.

Exit ticket

What are two reasons a backup might fail you?

It is on the same disk; it has never been tested.

What each step is for

Where it leads

Links: Cyber safety and privacy › Threat models; Security and privacy; next: The class server.