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

💬 Questions about this lesson

No questions yet: be the first to ask for help or share your configuration.

Ask a question about this lesson →

Recommended Articles

Bash scripts for administrators: writing robust scripts

Automate Linux administration with Bash: variables and quoting, conditions, loops, functions, exi...

Packages, systemd services, logs and scheduled tasks

Install and update software with apt and dnf, manage services with systemctl, write a systemd uni...

Backup, monitoring and troubleshooting of a Linux server

Operate a Linux server day to day: 3-2-1 backup strategy with rsync, tar, LVM snapshots and resti...

The Bash shell and files: navigation, redirections, pipes and text processing

Master the Linux command line: paths, file handling, wildcards, redirections, pipes, searching wi...

Storage on Linux: disks, partitions, file systems and LVM

Administer Linux storage: lsblk and blkid, GPT partitioning, ext4 and XFS, mounting and /etc/fsta...

Linux vs Unix: the complete guide to commands and distributions

Understand the differences between Linux and Unix, explore distributions (server, desktop, lightw...

Services, scheduled tasks, event logs and performance

Control Windows services and their recovery, automate with Task Scheduler, use event logs with Ge...

Linting Dockerfiles with Hadolint and docker build --check

Spot Dockerfile errors and bad practices before building: Hadolint, its rules, thresholds, and Bu...

Linux server security and hardening

Harden a Linux server: least privilege, updates, secure SSH, firewall, fail2ban, SELinux and AppA...

Support us

♥ SUPPORT US

Support RousselTM Learning

Courses, articles, labs and certificates stay free with no intrusive ads. A voluntary contribution, of any amount you choose, pays for hosting, lab environments and new content.

Contribute with PayPal Any amount · card or PayPal accountLearn more
QR code PayPal
Or scan this QR code with your phone

Thank you, every contribution counts!