>_ IzzyLab

Hands-on task

This task is step 9 of the course “Dates and times in the terminal”

Yesterday's logs archive

medium~10 min

The shop's logs for the past day have to be packed every night. Write a script /usr/local/bin/archive-yesterday that packs the /var/log/shop directory into /srv/archive/shop-YYYY-MM-DD.tar.gz, where the date is yesterday's (a run at 00:05 belongs to the day that just ended). The script creates the /srv/archive directory itself. Don't type the date by hand. 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 -d
  • relative dates
  • command substitution
  • tar
  • mkdir -p

Topic breakdown

GNU date can do more than print the current date — it can compute others: the -d flag takes a description like yesterday, 2 days ago or next monday, and the output format is set as usual (+%F is the ISO date YYYY-MM-DD). That's easier and more reliable than doing the arithmetic yourself: date crosses month and year boundaries on its own. Such a call, substituted through $( ) into the archive name, gives a script that creates a file dated with the past day every night — that's how nightly log archives are usually named.

What's checked

  1. archive-yesterday is executable
  2. an archive with yesterday's date is created
  3. the name doesn't have today's date
  4. the archive holds the shop's logs
  5. the date is computed, not hard-coded

What it looks like