Skip to content

rotate_logs destroys previously archived logs: two rotations in one month write the same tarball, and the originals are already gone #255

Closed

Description

@fdaviddpt

Problem

scripts/log.sh, on main:

:522    archive_month=$(date -v-7d +%Y-%m 2>/dev/null || date -d '7 days ago' +%Y-%m)
:523    local archive="${REMEMBER_LOG_DIR}/logs-${archive_month}.tar.gz"
:532    if tar -czf "$archive" -C "$REMEMBER_LOG_DIR" "${basenames[@]}" 2>/dev/null; then

The archive name carries only a month. Every rotation that runs in the same calendar month targets the same file, and tar -czf truncates rather than appending. The docstring at :507 states the other half: "and removes the originals on success."

So the second rotation of a month silently replaces the first month's archive with its own contents — and the logs that were inside it were deleted from disk when it was created. There is no copy left anywhere.

Demonstrated, not argued

Run 1 archives two June logs and deletes the originals. Run 2, same month, archives two July logs to the same filename. June logs present in the archive afterwards: 0. Their originals: already removed by run 1.

Why this is worse than the bug it was found beside

This is not the Windows defect (#252, fixed in PR #254). It is platform-independent and it is data loss rather than a failure to act. #252's rotation never archived anything — annoying, but every log survived on disk. This one archives, deletes, and then destroys the archive.

It is also silent by the same mechanism #252 exposed: 2>/dev/null, and a successful tar returns 0 whether or not it just overwrote something irreplaceable. Nothing distinguishes "created the month's archive" from "replaced the month's archive".

The logs it destroys are the diagnostic record — the thing you go looking for precisely when something has gone wrong for weeks, which is exactly how #252 was eventually noticed (a directory that grew to 2.3 MB over five weeks).

The judgment call

Fixing it changes archive naming semantics, which is why the #252 agent deliberately left it out of that PR's blast radius — the right call.

Options, none free:

  • Append instead of truncate. tar -rf cannot operate on a compressed archive, so this means decompress-append-recompress, or moving to an uncompressed .tar and compressing separately. Real cost, and a partial failure mid-cycle is a new way to lose data.
  • Make the name unique (add a day, or a counter). Simple and safe, but produces many small tarballs per month and changes what operators see in the directory.
  • Refuse to overwrite — if the target exists, either merge or pick a new name, and say which. Cheapest correct behaviour; the failure mode becomes a disclosed skip rather than silent destruction.

Whichever, the invariant worth pinning is that a rotation must never remove a log whose only remaining copy it is about to overwrite — and per docs/validators.md's three-state contract, a rotation that declines because the target exists must say so rather than returning a bare 0.

Worth also deciding whether the ordering should change: currently originals are deleted after a successful tar, and the deletion is trusted because the archive "succeeded". Verifying the archive contains what was just written, before deleting, would close the general case rather than this instance.

Provenance

Found by the agent implementing #252, outside that PR's scope, and reported rather than folded in. Verified independently on main at scripts/log.sh:522-532 and :507.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions