>_ IzzyLab

Hands-on task

This task is step 10 of the course “Archives and backups”

A dated backup of the nginx configs

medium~10 min

Write a script /usr/local/bin/backup-nginx that packs the /etc/nginx directory into the archive /var/backups/nginx/nginx-YYYYMMDD.tgz (today's date, no dashes, e.g. nginx-20260315.tgz). The script creates the /var/backups/nginx directory itself if it's missing. Don't type the date into the script by hand — it has to work tomorrow too. Run the script. It's handy to write the script in the Editor tab above the terminal — the /usr/local/bin directory is open on the left there.

What you'll practise:
  • date +FORMAT
  • command substitution
  • tar
  • mkdir -p
  • config backup

Topic breakdown

A backup script is valuable because every run creates a new archive without overwriting the previous ones — so the date in the name is built at run time, not typed by hand. date prints the current date by a template of directives like %Y, %m, %d, and command substitution $( ) puts that output right into the path. The archive directory is created idempotently (mkdir -p doesn't fail if it already exists), and tar with -C packs the directory with a relative path — unpacking it won't overwrite the live /etc.

What's checked

  1. backup-nginx is executable
  2. an archive with today's date is created
  3. the archive holds the nginx configs
  4. the date is computed, not hard-coded
  5. the script creates the archive directory itself

What it looks like