Bash: text processing and scripts / Final lab
Cleaning up exports on a schedule
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-exportsservice, from 03:00) writestar: 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:
- The
clean-exports.timertimer is enabled and running, the schedule is up to you; cron no longer starts the cleanup. - The
clean-exports.serviceservice, started by hand, finishes successfully. - A run removes
*.csvexports older than 14 days from/srv/exports— in any subdirectory and with any name. - Exports younger than 14 days and files outside
/srv/exportsstay in place. - Every run writes to the service journal how many exports it removed.
- 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.
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