From 7ac6d16180c44b0aa1dfb56fee18c475a7118105 Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sun, 21 Jun 2026 12:09:14 -0300 Subject: [PATCH] fixes and improves --- k8s/README.md | 4 +-- k8s/automated-nfs-backup.sh | 28 ++++++------------- k8s/examples/automated-nfs-backup.env.example | 2 +- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/k8s/README.md b/k8s/README.md index 5d94839..d68e0b5 100644 --- a/k8s/README.md +++ b/k8s/README.md @@ -42,7 +42,7 @@ Optional: - `NOTIFY_FAILURE_URL` (default: empty, disabled) - `NOTIFY_TITLE` (default: `Kubernetes`) - `NOTIFY_ASSET` (default: `kube config`) -- `CLEANUP_DELETE_COUNT` (default: `5`) +- `CLEANUP_KEEP_COUNT` (default: `4`) Notification payload (success and failure): @@ -69,7 +69,7 @@ Notification payload (success and failure): - Restore warnings/timeouts: logged and counted, script does not fail solely because of restore warnings. - If `NOTIFY_SUCCESS_URL` is set, success notification is sent at the end of a successful run. - If `NOTIFY_FAILURE_URL` is set, failure notification is sent when backup failures are detected. -- On successful runs, cleanup removes the last `CLEANUP_DELETE_COUNT` archives by date ordering (oldest side), without deleting the final remaining archive. +- On successful runs, cleanup removes the last `CLEANUP_KEEP_COUNT` archives by date ordering (oldest side), without deleting the final remaining archive. - Final summary always logs: - `processed` - `mapped` diff --git a/k8s/automated-nfs-backup.sh b/k8s/automated-nfs-backup.sh index 542db5e..596cfcf 100644 --- a/k8s/automated-nfs-backup.sh +++ b/k8s/automated-nfs-backup.sh @@ -33,7 +33,7 @@ NOTIFY_SUCCESS_URL="${NOTIFY_SUCCESS_URL:-}" NOTIFY_FAILURE_URL="${NOTIFY_FAILURE_URL:-}" NOTIFY_TITLE="${NOTIFY_TITLE:-Kubernetes}" NOTIFY_ASSET="${NOTIFY_ASSET:-kube config}" -CLEANUP_DELETE_COUNT="${CLEANUP_DELETE_COUNT:-5}" +CLEANUP_KEEP_COUNT="${CLEANUP_KEEP_COUNT:-4}" KUBECTL_ARGS=() if [[ -n "$KUBE_CONTEXT" ]]; then @@ -83,8 +83,8 @@ validate_inputs() { die "NFS_SOURCE_PATH does not exist or is not a directory: ${NFS_SOURCE_PATH}" fi - if [[ ! "$CLEANUP_DELETE_COUNT" =~ ^[0-9]+$ ]]; then - die "CLEANUP_DELETE_COUNT must be an integer >= 0" + if [[ ! "$CLEANUP_KEEP_COUNT" =~ ^[0-9]+$ ]]; then + die "CLEANUP_KEEP_COUNT must be an integer >= 0" fi if [[ -z "$BACKUP_PASSWORD" ]]; then @@ -379,8 +379,8 @@ backup_folder() { } cleanup_archives() { - if (( CLEANUP_DELETE_COUNT == 0 )); then - log_info "Cleanup disabled (CLEANUP_DELETE_COUNT=0)" + if (( CLEANUP_KEEP_COUNT == 0 )); then + log_info "Cleanup disabled (CLEANUP_KEEP_COUNT=0)" return 0 fi @@ -398,24 +398,14 @@ cleanup_archives() { mapfile -t sorted_archives < <(ls -1t -- "${archive_candidates[@]}") local total_sorted="${#sorted_archives[@]}" - if (( total_sorted <= 1 )); then - log_info "Cleanup skipped: only one archive present" + if (( total_sorted <= CLEANUP_KEEP_COUNT )); then + log_info "Cleanup skipped: ${total_sorted} archive(s) present, keeping ${CLEANUP_KEEP_COUNT}" return 0 fi - local delete_count="$CLEANUP_DELETE_COUNT" - local max_deletable=$((total_sorted - 1)) - if (( delete_count > max_deletable )); then - delete_count="$max_deletable" - fi - - if (( delete_count == 0 )); then - return 0 - fi - - local start_index=$((total_sorted - delete_count)) + local keep_count="$CLEANUP_KEEP_COUNT" local i - for ((i = start_index; i < total_sorted; i++)); do + for ((i = keep_count; i < total_sorted; i++)); do local archive_path="${sorted_archives[$i]}" rm -f -- "$archive_path" log_info "Cleanup deleted archive: ${archive_path}" diff --git a/k8s/examples/automated-nfs-backup.env.example b/k8s/examples/automated-nfs-backup.env.example index e516b36..78f3741 100644 --- a/k8s/examples/automated-nfs-backup.env.example +++ b/k8s/examples/automated-nfs-backup.env.example @@ -19,4 +19,4 @@ NOTIFY_SUCCESS_URL= NOTIFY_FAILURE_URL= NOTIFY_TITLE=Kubernetes NOTIFY_ASSET=kube config -CLEANUP_DELETE_COUNT=5 +CLEANUP_KEEP_COUNT=5