From 1cfca30db0794f137a1a070efed37d46c702b5d5 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Sat, 28 Nov 2026 00:50:19 +0530 Subject: [PATCH] Skip the latest link when pruning tar backups by count --- .github/workflows/verify.yml | 3 +++ scripts/opt/backup-loop.sh | 2 +- tests/test.prune-count.tar.sh | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100755 tests/test.prune-count.tar.sh diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 83d7321..906dc19 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -53,6 +53,9 @@ jobs: PRUNE_BACKUPS_DAYS: 3 run: ./tests/test.simple.tar.sh + - name: Test tar prune count with latest link + run: ./tests/test.prune-count.tar.sh + - name: Test restic handling env: RESTIC_REPOSITORY: /tmp/dest diff --git a/scripts/opt/backup-loop.sh b/scripts/opt/backup-loop.sh index be0f8f9..6120849 100644 --- a/scripts/opt/backup-loop.sh +++ b/scripts/opt/backup-loop.sh @@ -282,7 +282,7 @@ tar() { } _find_extra_backups() { - find "${DEST_DIR}" -maxdepth 1 -name "*.${backup_extension}" -exec ls -1Nt {} \+ | \ + find "${DEST_DIR}" -maxdepth 1 -type f -name "*.${backup_extension}" -exec ls -1Nt {} \+ | \ tail -n +$((PRUNE_BACKUPS_COUNT + 1)) | \ tr '\n' '\0' } diff --git a/tests/test.prune-count.tar.sh b/tests/test.prune-count.tar.sh new file mode 100755 index 0000000..ab9f9c9 --- /dev/null +++ b/tests/test.prune-count.tar.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash +set -euo pipefail + +set -x + +WORKDIR="$(readlink -m "$(dirname "${0}")")" + +cd "${WORKDIR}/.." + +TMP_DIR="$(mktemp --directory)" +trap 'sudo rm -rf "${TMP_DIR}"' EXIT + +: "${SRC_DIR:=/tmp/source}" +: "${DEST_DIR:=/tmp/dest}" +: "${RCON_PATH:=/usr/bin/rcon-cli}" + +export LOCAL_SRC_DIR="${TMP_DIR}/${SRC_DIR}" +export LOCAL_DEST_DIR="${TMP_DIR}/${DEST_DIR}" +export TMP_DIR + +"${WORKDIR}/common.bootstrap.sh" + +# The latest symlink must not take one of the PRUNE_BACKUPS_COUNT slots +for run in 1 2; do + timeout 50 docker run --rm \ + --env SRC_DIR="${SRC_DIR}" \ + --env DEST_DIR="${DEST_DIR}" \ + --env BACKUP_INTERVAL=0 \ + --env INITIAL_DELAY=0s \ + --env LINK_LATEST=true \ + --env PRUNE_BACKUPS_COUNT=1 \ + --mount "type=bind,src=${LOCAL_SRC_DIR},dst=${SRC_DIR}" \ + --mount "type=bind,src=${LOCAL_DEST_DIR},dst=${DEST_DIR}" \ + --mount "type=bind,src=${TMP_DIR}/rcon-cli,dst=${RCON_PATH}" \ + localhost:5000/itzg/mc-backup:latest + sleep 1 +done + +tree "${LOCAL_DEST_DIR}" +[ 1 -eq "$(find "${LOCAL_DEST_DIR}" -maxdepth 1 -type f -name '*.tar.gz' | wc -l)" ] +tar -tzf "${LOCAL_DEST_DIR}/latest.tar.gz" > /dev/null