Archive application logs older than 30 days to S3 Glacier and remove them locally.
BeginnerScenarioScenarios: backups, log rotation and housekeeping
Answer
Situation read → Upload first, verify, then delete; never delete on a failed upload.
What I'd do →
find /var/log/app -type f -name '*.gz' -mtime +30 -print0 |
while IFS= read -r -d '' f; do
key="logs/$(hostname -s)/${f#/var/log/app/}"
if aws s3 cp --only-show-errors --storage-class GLACIER_IR -- "$f" "s3://$BUCKET/$key"; then
rm -f -- "$f"
else
echo "upload failed, keeping $f" >&2
fi
done
GLACIER_IR(Instant Retrieval) orDEEP_ARCHIVEdepending on retrieval needs and cost.- Better: upload to
STANDARDand let a lifecycle rule transition objects, which keeps the script simple.
Why → The if upload; then delete ordering makes data loss impossible on network errors.
Interview tip → Mention retrieval costs and delays for Glacier classes, because cheap storage is not cheap restores.
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