← System admin on a Raspberry Pi

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.

The Pi's SD card dies on a Friday. What did you lose?

Keeping a system running is half the job. Three habits cover most of it: a backup on a different disk, a schedule so it happens without you, and a test that you can get the files back. Each one protects against a different failure, and none replaces the others.

How this lesson works: Predict, Try, Investigate, Modify, Practise, Make
PredictRead the problem and decide what you think before you try anything. A wrong guess is fine: it shows you what to look for.

The schedule

A cron line starts 30 2 * * *. When does it run?

TryTry it on the board and compare the result with your prediction.

Try: make a backup

Was your prediction right?

Try it yourselfYour edits are saved in this browser.

Back up the Documents folder into /media/usb/docs.tar.gz with tar -czf. Check it is there with ls -l /media/usb.

Use the board. This ticks when you get there.

InvestigateWork out why it happens, one step at a time.

Worked example: Backup, schedule, restore

Worked exampleBefore each step, say what you think comes next.
  1. tar -czf /media/usb/docs.tar.gz Documents\text{tar -czf /media/usb/docs.tar.gz Documents}make a compressed copy of a folder.

Now try it yourself.

What does 0 3 * * 0 mean?

Sam schedules a backup. Click the line where it first goes wrong.

You have made a backup every night for a year, and never opened one. What do you really know?

ModifyChange the problem on purpose and see what happens.

Vary: change one thing

Try it yourselfYour edits are saved in this browser.

Schedule a backup at 02:30 every day. Use echo "30 2 * * * tar -czf /media/usb/docs.tar.gz Documents" | crontab -

Use the board. This ticks when you get there.

Documents/readme.txt was deleted by mistake. Restore it from the backup in /media/usb.

Use the board. This ticks when you get there.

Find the free space on the Pi with df -h, then see which folder is biggest with du -h.

Use the board. This ticks when you get there.

PractiseA few on your own, to check you have it.

Practise

Which cron line runs at 04:15 every day?

Where should the backup of the Pi's files be kept?

Review: the firewall is on and port 8096 is not allowed. Can a TV reach Jellyfin?

MakeMake your own problem using what you have learned. Expect a few tries; that is normal.

A nightly backup

Then: on a real Pi, look at /var/log/syslog and try df -h on the real disk.

Try it yourselfYour edits are saved in this browser.
Need a hint?

Before a hint: what kind of stuck is this? Try a strategy first

Two jobs: make the backup now, schedule it nightly.

Another hint

tar -czf /media/usb/backup.tar.gz Documents

Another hint

echo "30 2 * * * tar -czf /media/usb/backup.tar.gz Documents" | crontab -

Back up Documents to /media/usb/backup.tar.gz and schedule the same backup for 02:30 every night.

Use the board. This ticks when you get there.

Now you can
Next lesson8. Challenge: the class serverSet up the Pi as the class server: a hand-in share, accounts in one group, a firewall that lets only the right ports through, and a nightly backup. Then explain what each part protects, and what it does not.
For teachers

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. About 40 minutes.

Open the teacher guide: answers, solutions, slides and a worksheet.

Students will be able to

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).

The hard part (on purpose)

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

When students get stuck

Stretch and support

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

Support: The five fields written above a cron line.

Where it leads

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

What each step is for