`/srv/app/releases` has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.
BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer
Situation read → Sort by name if names are timestamps, and protect the directory current points to.
What I'd do →
releases=/srv/app/releases
current=$(readlink -f /srv/app/current)
mapfile -t old < <(find "$releases" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort -r | tail -n +6)
for r in "${old[@]}"; do
[[ "$releases/$r" == "$current" ]] && continue
rm -rf -- "${releases:?}/$r"
echo "removed $r"
done
- Release names like
20261109T143000Zsort chronologically as strings. ${releases:?}guards against an empty variable.
Why → Keeping a few releases enables instant rollback; deleting the live one would cause an outage.
Interview tip → Mention that sorting by name is more reliable than by mtime, which changes when someone touches a file.
More on Scenarios: backups, log rotation and housekeeping
Design a nightly backup of `/etc` and application data to S3 with retention.IntermediateWrite the core of a Postgres backup that streams `pg_dump` through gzip to S3 and never uploads an empty dump.IntermediateBackups have "succeeded" every night for a year, but the first real restore failed. How do you make backups trustworthy?Advanced`/var` is 100% full because an app's logs were never rotated, and the app now fails to start. What do you do now, and what do you set up after?BeginnerShould you use `copytruncate` or a `postrotate` signal for an application's logs? How do you decide?IntermediateA Docker host keeps filling its disk. Write a safe cleanup approach.Beginner