Lab 8 of 8 · Bash automation, scheduled jobs and backups
Automate a tested backup
Write a backup script, check it with ShellCheck, let AI improve it under the same checks, schedule it with a systemd timer, and prove you can restore from it.
- Time
- 60 minutes
- Cost
- Free.
What you learn
Anything you do twice, script. Anything you do on a schedule, automate. And a backup only counts once you have restored from it.
- Safe Bash scripts: set -euo pipefail, quoting and clear messages
- Checking scripts with ShellCheck
- Scheduling jobs with systemd timers
- Backups with retention, and testing a restore
In the AI era: AI writes Bash fast, including bugs that delete the wrong files. Every AI-written script goes through ShellCheck, a test run and a restore test before it runs on a schedule. That is how you use AI on the command line safely.
Get set up
Commands use Homebrew in the Terminal app. Already have a tool? Skip it.
- A terminalOfficial install page ↗
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"Use the Terminal app. The command installs Homebrew, which the other macOS commands use.
Before you start
Finish lab 4 (the backup includes the status page).
Start your server and open your repository: run
multipass start sysadmin-labandmultipass shell sysadmin-labon your computer, thencd linux-admin-labon the server.
Step by step
1 Write the backup script
Create it with
mkdir -p scriptsandnano scripts/backup.sh.set -euo pipefailstops the script at the first error instead of carrying on and doing damage.#!/usr/bin/env bash # Back up the status page and its service file, keeping the newest 7 archives. set -euo pipefail BACKUP_DIR="/var/backups/sysadmin-lab" KEEP=7 STAMP="$(date +%Y%m%d-%H%M%S)" ARCHIVE="$BACKUP_DIR/sysadmin-lab-$STAMP.tar.gz" mkdir -p "$BACKUP_DIR" tar -czf "$ARCHIVE" /srv/status /etc/systemd/system/status-page.service echo "Created $ARCHIVE" # Delete all but the newest $KEEP archives. find "$BACKUP_DIR" -name 'sysadmin-lab-*.tar.gz' -printf '%T@ %p\n' \ | sort -rn \ | tail -n +$((KEEP + 1)) \ | cut -d' ' -f2- \ | while read -r old; do rm -- "$old" echo "Deleted $old" done2 Check it with ShellCheck
ShellCheck finds quoting mistakes and other Bash bugs before they bite.
sudo apt install -y shellcheck chmod +x scripts/backup.sh shellcheck scripts/backup.sh && echo "ShellCheck: no problems"You should see:ShellCheck: no problems.3 Run it and commit the first version
It needs
sudobecause it reads a system file.sudo scripts/backup.sh ls -lh /var/backups/sysadmin-lab git add scripts git commit -m "Add backup script" git pushYou should see:Created /var/backups/sysadmin-lab/sysadmin-lab-....tar.gzand one archive listed.4 Ask AI to improve it, then check it the same way
Ask your AI assistant: "Add a check to this script that stops with a clear error if the backup folder has less than 100 MB free." Paste its version over
scripts/backup.shwithnano. Accept it only if ShellCheck passes, the script still works, and the diff contains just that change.shellcheck scripts/backup.sh && echo "ShellCheck: no problems" sudo scripts/backup.sh git diff scripts/backup.shYou should see:ShellCheck: no problems, a new archive, and a small diff that adds a free-space check.5 Describe the backup job
Create it with
sudo nano /etc/systemd/system/backup.service.oneshotmeans it runs once and exits.[Unit] Description=Back up the status page [Service] Type=oneshot ExecStart=/home/ubuntu/linux-admin-lab/scripts/backup.sh6 Schedule it with a timer
Create it with
sudo nano /etc/systemd/system/backup.timer.Persistent=trueruns a missed backup after the server was off.[Unit] Description=Run the status page backup every day [Timer] OnCalendar=daily RandomizedDelaySec=15m Persistent=true [Install] WantedBy=timers.target7 Turn on the timer and test it
Starting the service by hand runs one backup now, so you can see it work.
sudo systemctl daemon-reload sudo systemctl enable --now backup.timer systemctl list-timers backup.timer --no-pager sudo systemctl start backup.service journalctl -u backup.service -n 5 --no-pagerYou should see:The timer's next run time, and
Created ...in the backup's log.8 Prove you can restore
Unpack the newest archive somewhere safe and compare it with the live files.
LATEST=$(ls -1t /var/backups/sysadmin-lab/*.tar.gz | head -n 1) rm -rf /tmp/restore-test mkdir -p /tmp/restore-test sudo tar -xzf "$LATEST" -C /tmp/restore-test sudo diff -r /srv/status /tmp/restore-test/srv/status && echo "Restore matches the live files"You should see:Restore matches the live files.9 Write your backup notes
Create it with
nano notes/08-backups.md.# Backups - What is backed up: ... - Where the archives go: ... - Schedule: ... - How many are kept: ... - Last restore test (date and result): ... - What the AI changed in the script, and how I checked it: ...10 Commit and push
Keep the timer and service files with the script.
mkdir -p config cp /etc/systemd/system/backup.service /etc/systemd/system/backup.timer config/ git add scripts config notes git commit -m "Schedule tested backups with a systemd timer" git push11 Stop or delete your lab server
Type
exitto leave the server first. Stop it to keep it for later; delete it once you have finished the roadmap. Your work is safe on GitHub either way.multipass stop sysadmin-lab # when you have finished the whole roadmap: multipass delete --purge sysadmin-lab
You are done when
ShellCheck passes on your script, including the AI-suggested change.
A systemd timer runs the backup every day.
You restored an archive and it matched the live files.
We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.