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. 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), aflocklock, timestamped logging and deletion of old archives.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 withbash -n.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. Schedule with a timer
Why? The timer logs every run and marks the service failed if the script returns an error.
Createbackup-etc.serviceandbackup-etc.timer(daily at 2:30 with catch-up), trigger a manual run and read the journal.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
Sign in to track your progress and take notes. Sign in
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.