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

Linux administration best practices

Checklist for administering Linux servers: accounts and sudo, SSH, updates, systemd services, sto...

Backup, Hyper-V and Windows troubleshooting

Back up and restore with Windows Server Backup (including system state and Active Directory), vir...

Users, groups, sudo and permissions on Linux

Manage Linux accounts (useradd, usermod, passwd, chage), groups, sudo and visudo, rwx and octal p...

Networking on Linux: configuration, diagnosis, firewall and SSH

Configure and troubleshoot Linux networking: ip, ss, netplan and NetworkManager, DNS, layered dia...

Linux system architecture: kernel, boot and directory tree

Understand how a Linux server works: kernel and user space, the UEFI, GRUB, initramfs and systemd...

PowerShell for Windows administrators

Master PowerShell to administer Windows: cmdlets and help, object pipeline, filters and formats, ...

Processes, memory and performance: monitoring and troubleshooting a Linux server

Understand and control Linux processes (ps, top, signals, nice, jobs), interpret load, memory and...

Writing k6 scripts: requests, checks, scenarios and executors

The structure of a k6 script (init, setup, default, teardown), HTTP requests, checks, groups and ...

Ansible best practices: structure, idempotence, security and quality

Ansible best practices: project organization, naming, FQCN, idempotence, modules rather than shel...

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!