Linux Labs

Bash: text processing and scripts / Final lab

Cleaning up exports on a schedule

hard ~40 min
Not running Starting usually ~20 s
Report a problem

Situation

The reporting service puts client exports into /srv/exports — one directory per client, *.csv files. An export is kept for 14 days. For that the previous admin wrote the /usr/local/bin/clean-exports script and runs it from cron every night at 03:30.

Complaints over the month:

  • the client acme cannot download yesterday's report — it is already gone;
  • the client blue river still has exports older than two months;
  • the nightly backup of the exports (the backup-exports service, from 03:00) writes tar: exports/…: File removed before we read it;
  • nobody knows when the cleanup ran or what it removed.

Your manager asks you to move the cleanup to a systemd timer: there every run shows up in systemctl and in the journal.

Task

Fix the cleanup and move it from cron to a systemd timer:

  1. The clean-exports.timer timer is enabled and running, the schedule is up to you; cron no longer starts the cleanup.
  2. The clean-exports.service service, started by hand, finishes successfully.
  3. A run removes *.csv exports older than 14 days from /srv/exports — in any subdirectory and with any name.
  4. Exports younger than 14 days and files outside /srv/exports stay in place.
  5. Every run writes to the service journal how many exports it removed.
  6. The cleanup does not overlap: while another process holds the backup lock — the backup itself or a second cleanup run — it removes nothing.

The check starts the service itself on its own exports in /srv/exports, reads the journal of that run and tries a run while the lock is busy. It removes its own files afterwards.

The machine has no Internet; everything you need is already installed.

Visible only to you. Saved as you type.

All notes

Lab goals

0 of 6
  • The clean-exports.timer timer is enabled and running, cron does not start the cleanup — not checked yet
  • A manual start of clean-exports.service finishes successfully — not checked yet
  • A run removes *.csv exports older than 14 days in any subdirectory of /srv/exports — not checked yet
  • Exports younger than 14 days and files outside /srv/exports stay in place — not checked yet
  • A run writes to the service journal how many exports it removed — not checked yet
  • While another process holds the backup lock, the cleanup removes nothing — not checked yet

Hints

0 of 3 open · they open one at a time
Hint 1
Show the solution after 3 failed checks

Leave the lab?

The machine keeps running for 10 more minutes, then it is removed. Files are lost; completed goals stay.

Loading…