Add KEEP_LAST retention policy for backups
docker-build / Build image (push) Successful in 25s

Add KEEP_LAST environment variable to always preserve the N most recent
backups regardless of age, preventing data loss when backups haven't
been created for extended periods. Defaults to 10 copies.
This commit is contained in:
2026-09-01 12:09:19 +07:00
parent 819635b54d
commit 3183bf5267
5 changed files with 45 additions and 5 deletions
Regular → Executable
+28 -4
View File
@@ -2,14 +2,38 @@
set -u
# Seconds since epoch for current time
# Retention: delete backups older than DELETE_AFTER days, but always keep
# at least KEEP_LAST most recent copies regardless of their age.
DATE_NOW=$(date +%s)
DEST="${MINIO_PATH%/}/"
KEEP_LAST="${KEEP_LAST:-10}"
/scripts/minio_uploader.sh list "$DEST" | grep "sqlite_" | while read -r LINE
case "$KEEP_LAST" in
''|*[!0-9]*) echo "WARN: KEEP_LAST is not a number ('$KEEP_LAST'), falling back to 10"; KEEP_LAST=10 ;;
esac
LIST=$(mktemp)
trap 'rm -f "$LIST"' EXIT
# `rclone ls` prints "<size> <path>". Filenames are sqlite_<YYYY-MM-DD-HHMMSS>.tar.gz,
# so a reverse lexicographic sort is a reverse chronological sort.
/scripts/minio_uploader.sh list "$DEST" \
| awk '{ print $2 }' \
| grep "^sqlite_" \
| sort -r > "$LIST"
TOTAL=$(wc -l < "$LIST" | tr -d ' ')
echo "Found $TOTAL backups, keeping at least $KEEP_LAST most recent, max age $DELETE_AFTER days"
if [ "$TOTAL" -le "$KEEP_LAST" ]; then
echo "Nothing to rotate: $TOTAL backups <= KEEP_LAST=$KEEP_LAST"
exit 0
fi
# Only the tail beyond the protected window is a candidate for deletion.
tail -n "+$((KEEP_LAST + 1))" "$LIST" | while read -r BACKUP_FILENAME
do
# `rclone ls` prints "<size> <path>"
BACKUP_FILENAME=$(echo "$LINE" | awk '{ print $2 }')
[ -n "$BACKUP_FILENAME" ] || continue
BACKUP_DATE=$(echo "$BACKUP_FILENAME" | awk 'BEGIN { FS = "[_-]" } ; { printf "%s-%s-%s",$2,$3,$4 }')