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

You need to tar a directory that the application writes to constantly. `tar` keeps exiting with 1. How do you handle it?

IntermediateScenarioScenarios: backups, log rotation and housekeeping
Answer

Situation read → GNU tar exits 1 when a file changed while it was being read; exit 2 is a fatal error. Under set -e, exit 1 aborts the backup.

What I'd do →

  1. Treat 1 as a warning, above 1 as failure:
    rc=0
    tar --warning=no-file-changed -czf "$tmp" -C / srv/app/data || rc=$?
    (( rc <= 1 )) || die "tar failed with $rc"
    (( rc == 1 )) && log WARN "files changed during backup"
    
  2. For consistency, snapshot first: LVM or ZFS snapshot, EBS snapshot, or the application's own backup command (databases must never be backed up by copying live files).
  3. Or pause writes briefly (maintenance mode) for small datasets.

Why → A file that changes mid-read may be inconsistent; for logs that is fine, for database files it is corruption.

Interview tip → Mention snapshots as the real fix for consistency, not tar flags.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Scenarios: backups, log rotation and housekeeping