forked from infra/ansible
move file-retention-iso-date to files/scripts
Signed-off-by: Kevin Fenzi <kevin@scrye.com>
This commit is contained in:
parent
2db3130404
commit
8fa423e72a
1 changed files with 0 additions and 0 deletions
|
|
@ -1,94 +0,0 @@
|
|||
#!/bin/bash
|
||||
# vim: ts=4:sw=4:expandtab:tw=100
|
||||
|
||||
# This script manages a set of backup files which are prefixed with ISO dates (YYYY-MM-DD).
|
||||
# - As new files come in, we want to delete the old ones.
|
||||
# - RETENTION_DAYS decides how old is "old".
|
||||
# - But if no new files come in (e.g. the backup process is broken), we don't want to
|
||||
# end up deleting _all_ the files, just because they're old.
|
||||
# - MIN_KEEP decides how many files to keep.
|
||||
#
|
||||
# Usage: ./retain.sh --dry-run <directory>
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
DRY_RUN=false
|
||||
DIR=""
|
||||
|
||||
# Parse arguments (extremely verbosely to satisfy the LLM reviewing this PR)
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case "$1" in
|
||||
--dry-run)
|
||||
DRY_RUN=true
|
||||
shift
|
||||
;;
|
||||
-*)
|
||||
echo "Error: Unknown flag '$1'" >&2
|
||||
exit 1
|
||||
;;
|
||||
*)
|
||||
if [[ -z "$DIR" ]]; then
|
||||
DIR="$1"
|
||||
shift
|
||||
else
|
||||
echo "Error: Multiple positional arguments provided. Expected only DIR." >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Validate required argument
|
||||
if [[ -z "$DIR" ]]; then
|
||||
echo "Error: DIR argument is required." >&2
|
||||
echo "Usage: $0 [--dry-run] DIR" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
RETENTION_DAYS="${RETENTION_DAYS:-31}"
|
||||
MIN_KEEP="${MIN_KEEP:-31}"
|
||||
CUTOFF=$(date -d "${RETENTION_DAYS} days ago" +%Y-%m-%d)
|
||||
|
||||
# Collect all dated files (basename matches YYYY-MM-DD-*)
|
||||
mapfile -t DATED_FILES < <(
|
||||
find "$DIR" -maxdepth 1 -type f -name "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-*" \
|
||||
| sort -r # sort descending by filename (date first)
|
||||
)
|
||||
|
||||
total=${#DATED_FILES[@]}
|
||||
|
||||
$DRY_RUN && echo "*** DRY RUN — no files will be deleted ***"
|
||||
echo ""
|
||||
|
||||
if [[ $total -eq 0 ]]; then
|
||||
echo "No dated files found in '$DIR'."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
deleted=0
|
||||
skipped_retention=0
|
||||
|
||||
for i in "${!DATED_FILES[@]}"; do
|
||||
filepath="${DATED_FILES[$i]}"
|
||||
filename=$(basename "$filepath")
|
||||
file_date="${filename:0:10}" # extract YYYY-MM-DD
|
||||
|
||||
# Always keep the MIN_KEEP most-recent files (indices 0..MIN_KEEP-1)
|
||||
if [[ $i -lt $MIN_KEEP ]]; then
|
||||
echo "KEEP (newest ${MIN_KEEP}): $filename"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Delete if the file's date is before the cutoff
|
||||
if [[ "$file_date" < "$CUTOFF" ]]; then
|
||||
echo "DELETE (expired): $filename"
|
||||
$DRY_RUN || rm -- "$filepath"
|
||||
deleted="$((deleted + 1))"
|
||||
else
|
||||
echo "KEEP (within retention): $filename"
|
||||
skipped_retention="$((skipped_retention + 1))"
|
||||
fi
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo "Deleted: $deleted files (dry-run=$DRY_RUN). Kept by min-count: $(( total < MIN_KEEP ? total : MIN_KEEP )). Kept by date window: $skipped_retention."
|
||||
Loading…
Add table
Add a link
Reference in a new issue