An app writes `app-YYYY-MM-DD.log` files daily. There is no logrotate. Write the housekeeping.
BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer
Situation read → Compress yesterday's and older files, delete old compressed files, and never touch today's file.
What I'd do →
dir=/var/log/app
today="app-$(date +%F).log"
find "$dir" -maxdepth 1 -type f -name 'app-*.log' ! -name "$today" -mmin +60 -exec gzip -9 -- {} +
find "$dir" -maxdepth 1 -type f -name 'app-*.log.gz' -mtime +30 -delete
! -name "$today"and-mmin +60protect the active file.- Schedule daily at 00:15 via a timer.
Why → Date-named files need no rename or signal; compression and deletion by age are enough.
Interview tip → Mention that logrotate can still do this with dateext patterns, but a two-line find is often clearer.
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?Intermediate`/srv/app/releases` has 300 old release directories. Write a cleanup that keeps the newest 5 and never deletes the current release.Beginner