PleinIQ
Menu
Language

Choose your language

This page is only in English for now. Choosing a language opens its home page.

Other languages

Your browser can translate any page into almost any language.

Other browsers: Open the browser menu and look for “Translate”.

Your resume, your downloads and lab commands are never translated.

Roadmaps / Linux / Lab 8

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.

  • Install
    /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-lab and multipass shell sysadmin-lab on your computer, then cd linux-admin-lab on the server.

Step by step

  1. 1 Write the backup script

    Create it with mkdir -p scripts and nano scripts/backup.sh. set -euo pipefail stops the script at the first error instead of carrying on and doing damage.

    File: scripts/backup.sh
    #!/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"
        done
    
  2. 2 Check it with ShellCheck

    ShellCheck finds quoting mistakes and other Bash bugs before they bite.

    Run this on your lab server
    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. 3 Run it and commit the first version

    It needs sudo because it reads a system file.

    Run this on your lab server
    sudo scripts/backup.sh
    ls -lh /var/backups/sysadmin-lab
    git add scripts
    git commit -m "Add backup script"
    git push
    You should see:

    Created /var/backups/sysadmin-lab/sysadmin-lab-....tar.gz and one archive listed.

  4. 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.sh with nano. Accept it only if ShellCheck passes, the script still works, and the diff contains just that change.

    Run this on your lab server
    shellcheck scripts/backup.sh && echo "ShellCheck: no problems"
    sudo scripts/backup.sh
    git diff scripts/backup.sh
    You should see:

    ShellCheck: no problems, a new archive, and a small diff that adds a free-space check.

  5. 5 Describe the backup job

    Create it with sudo nano /etc/systemd/system/backup.service. oneshot means it runs once and exits.

    File: /etc/systemd/system/backup.service
    [Unit]
    Description=Back up the status page
    
    [Service]
    Type=oneshot
    ExecStart=/home/ubuntu/linux-admin-lab/scripts/backup.sh
    
  6. 6 Schedule it with a timer

    Create it with sudo nano /etc/systemd/system/backup.timer. Persistent=true runs a missed backup after the server was off.

    File: /etc/systemd/system/backup.timer
    [Unit]
    Description=Run the status page backup every day
    
    [Timer]
    OnCalendar=daily
    RandomizedDelaySec=15m
    Persistent=true
    
    [Install]
    WantedBy=timers.target
    
  7. 7 Turn on the timer and test it

    Starting the service by hand runs one backup now, so you can see it work.

    Run this on your lab server
    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-pager
    You should see:

    The timer's next run time, and Created ... in the backup's log.

  8. 8 Prove you can restore

    Unpack the newest archive somewhere safe and compare it with the live files.

    Run this on your lab server
    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. 9 Write your backup notes

    Create it with nano notes/08-backups.md.

    File: 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. 10 Commit and push

    Keep the timer and service files with the script.

    Run this on your lab server
    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 push
  11. 11 Stop or delete your lab server

    Type exit to 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.

    Run this in Terminal
    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.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.

Join PleinIQ Updates

What are you interested in?

Read how we handle your information in our privacy notice.