Interview Prep/Shell Scripting/Scenarios: backups, log rotation and housekeeping

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
  1. GLACIER_IR (Instant Retrieval) or DEEP_ARCHIVE depending on retrieval needs and cost.
  2. Better: upload to STANDARD and 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.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping