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

Design a nightly backup of `/etc` and application data to S3 with retention.

IntermediateScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → A backup is only useful if it is complete, verifiable, off-host, retained correctly and alerts on failure.

What I'd do →

  1. Archive with a UTC timestamp and the hostname: tar -czf "$dir/$(hostname -s)-$(date -u +%Y%m%dT%H%M%SZ).tar.gz" -C / etc srv/app/shared.
  2. Write to a temp file and mv into place; generate a .sha256 next to it.
  3. Upload with retries and server-side encryption: aws s3 cp --sse AES256 "$f" "s3://$bucket/servers/$host/", then confirm with aws s3api head-object that the size matches.
  4. Local retention: keep the newest N archives; S3 retention: a bucket lifecycle rule (expire after 35 days, or transition to Glacier), plus versioning and Object Lock if ransomware is in scope.
  5. Run it from a systemd timer with Persistent=true, flock, and OnFailure= alerting.

Why → Each step closes a known failure mode: partial files, silent corruption, local-only copies, unbounded growth and unnoticed failures.

Interview tip → Say retention in S3 belongs in a lifecycle policy, not in a script that deletes objects with broad permissions.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping