simplifying automated nfs backup

This commit is contained in:
2026-07-04 20:21:10 -03:00
parent 2ce71f4fc1
commit 94deda4372

View File

@@ -237,79 +237,6 @@ restore_all_remaining() {
fi fi
} }
resource_replicas() {
local namespace="${1:?namespace required}"
local kind="${2:?kind required}"
local name="${3:?name required}"
local json_path="${4:?jsonpath required}"
_kubectl -n "$namespace" get "${kind}/${name}" -o "jsonpath=${json_path}" 2>/dev/null || true
}
wait_for_scale_state() {
local namespace="${1:?namespace is required}"
local state_file="${2:?state file is required}"
local direction="${3:?direction is required}" # down|up
local start_ts
local now_ts
local elapsed
local unresolved
local kind
local name
local target
local spec
local status
local ready
start_ts="$(date +%s)"
while true; do
unresolved=0
while IFS=$'\t' read -r kind name target; do
[[ -z "$kind" || -z "$name" ]] && continue
spec="$(resource_replicas "$namespace" "$kind" "$name" '{.spec.replicas}')"
status="$(resource_replicas "$namespace" "$kind" "$name" '{.status.replicas}')"
ready="$(resource_replicas "$namespace" "$kind" "$name" '{.status.readyReplicas}')"
# If all three values are empty the resource was likely deleted
# between capture and now — nothing to wait for.
if [[ -z "$spec" && -z "$status" && -z "$ready" ]]; then
continue
fi
[[ -z "$spec" ]] && spec="1"
[[ -z "$status" ]] && status="0"
[[ -z "$ready" ]] && ready="0"
if [[ "$direction" == "down" ]]; then
if [[ "$spec" != "0" || "$status" != "0" ]]; then
unresolved=$((unresolved + 1))
fi
else
if [[ "$target" == "0" ]]; then
if [[ "$spec" != "0" || "$status" != "0" ]]; then
unresolved=$((unresolved + 1))
fi
else
if [[ "$spec" != "$target" || "$ready" != "$target" ]]; then
unresolved=$((unresolved + 1))
fi
fi
fi
done < "$state_file"
if [[ "$unresolved" -eq 0 ]]; then
return 0
fi
now_ts="$(date +%s)"
elapsed=$((now_ts - start_ts))
if (( elapsed >= SCALE_TIMEOUT_SECONDS )); then
return 1
fi
sleep 3
done
}
archive_path_for_run() { archive_path_for_run() {
local ts local ts
ts="$(date +"$ARCHIVE_TS_FORMAT")" ts="$(date +"$ARCHIVE_TS_FORMAT")"
@@ -476,12 +403,8 @@ process_folder() {
capture_replicas_state "$namespace" "$state_file" capture_replicas_state "$namespace" "$state_file"
scale_namespace_to_zero "$namespace" "$state_file" scale_namespace_to_zero "$namespace" "$state_file"
if wait_for_scale_state "$namespace" "$state_file" "down"; then log_info "Waiting 30 seconds for namespace '${namespace}' to scale down..."
log_info "Namespace '${namespace}' reached scale-down target" sleep 30
else
log_warn "Timeout waiting for namespace '${namespace}' to fully scale down; continuing by policy"
scale_warnings=$((scale_warnings + 1))
fi
else else
unmapped_folders=$((unmapped_folders + 1)) unmapped_folders=$((unmapped_folders + 1))
log_warn "No namespace matched folder '${folder_name}'. Running backup only." log_warn "No namespace matched folder '${folder_name}'. Running backup only."
@@ -497,12 +420,8 @@ process_folder() {
if [[ "$has_mapping" -eq 1 ]]; then if [[ "$has_mapping" -eq 1 ]]; then
restore_namespace_replicas "$namespace" "$state_file" restore_namespace_replicas "$namespace" "$state_file"
if wait_for_scale_state "$namespace" "$state_file" "up"; then log_info "Waiting 30 seconds for namespace '${namespace}' to restore replicas..."
log_info "Namespace '${namespace}' reached restore target" sleep 30
else
log_warn "Timeout waiting for namespace '${namespace}' to fully restore replicas"
restore_warnings=$((restore_warnings + 1))
fi
rm -f "$state_file" rm -f "$state_file"
fi fi
} }