RousselTMLEARNING

TP · Linux administration: from discovery to expertise

Lab: A robust, scheduled backup script

What you will learn in this TP
  • Write a script with set -euo pipefail, trap and getopts
  • Check quality with ShellCheck
  • Schedule and restore

Introduction #

What you will learn in this section
  • Write a script with set -euo pipefail, trap and getopts
  • Check quality with ShellCheck
  • Schedule and restore

Context. Nobody backs up the server configuration. You write a reliable script that can be re-run safely and reports its failures.

Prerequisites: a disposable Ubuntu 24.04 virtual machine (Multipass, Vagrant or cloud) with a sudo account  ·  Estimated time: 50 min

A verified, restorable daily backup of /etc.

Hands-on #

Guided steps, with their solution.

  1. 1. Write the script

    Why? Strict mode, automatic cleanup, a lock and options make the script safe to re-run and schedule.

    Create /usr/local/bin/backup-etc.sh: a verified archive of /etc, options -n (dry run) and -r (retention in days), a flock lock, timestamped logging and deletion of old archives.
  2. 2. Check with ShellCheck

    Why? ShellCheck catches missing quotes, undefined variables and classic pitfalls before they break production.

    Install ShellCheck, analyse the script and fix any warnings. Check the syntax with bash -n.
  3. 3. Test normal and error cases

    Why? A script is also tested on its failures: the exit code is what systemd or CI will see.

    Run a dry run, a real backup, then trigger three errors (non-root, invalid retention, two simultaneous runs) showing the exit code.
  4. 4. Schedule with a timer

    Why? The timer logs every run and marks the service failed if the script returns an error.

    Create backup-etc.service and backup-etc.timer (daily at 2:30 with catch-up), trigger a manual run and read the journal.
  5. 5. Restore a file

    Why? A backup is only worth something if you can restore it.

    Modify /etc/motd, then restore the backed-up version from the latest archive to a temporary directory and compare before putting it back.

Difficulty level : ●●●○○ (3/5)

My progress

  • ⏱ Estimated time : ≈ 50 min

Sign in to track your progress and take notes. Sign in

✉️ Ask a question

Recommended Articles

Scripts Bash pour l'administrateur : écrire des scripts robustes

Automatiser l'administration Linux avec Bash : variables et guillemets, conditions, boucles, fonc...

Paquets, services systemd, journaux et tâches planifiées

Installer et mettre à jour des logiciels avec apt et dnf, piloter les services avec systemctl, éc...

Package managers

The different package managers for Kubernetes and their advantages.