Files
proxmox/scripts/maintenance/prune-proxmox-vzdump-dump.sh
defiQUG b8613905bd
Some checks failed
Deploy to Phoenix / validate (push) Failing after 15s
Deploy to Phoenix / deploy (push) Has been skipped
chore: sync workspace — configs, docs, scripts, CI, pnpm, submodules
- Submodule pins: dbis_core, cross-chain-pmm-lps, mcp-proxmox (local, push may be pending), metamask-integration, smom-dbis-138
- Atomic swap + cross-chain-pmm-lops-publish, deploy-portal workflow, phoenix deploy-targets, routing/aggregator matrices
- Docs, token-lists, forge proxy, phoenix API, runbooks, verify scripts

Made-with: Cursor
2026-04-21 22:01:33 -07:00

128 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
# Prune old Proxmox vzdump archives under /var/lib/vz/dump (the "local" dir storage).
# Keeps the N newest archive per VMID for each family:
# - vzdump-lxc-<vmid>-*.(tar.gz|tar.zst|vma.zst|vma.gz)
# - vzdump-qemu-<vmid>-*.(tar.gz|tar.zst|vma.zst|vma.gz)
# - vzdump-<vmid>-<epoch>.(tar.gz|tar.zst) (legacy naming without lxc/qemu)
# Removes matching .log / .notes sidecars when removing an archive.
#
# Run ON the Proxmox node as root, or via SSH:
# ssh root@192.168.11.11 'bash -s' < scripts/maintenance/prune-proxmox-vzdump-dump.sh -- 2
#
# Args: [KEEP] (default 2). Env: VZDUMP_DIR=/var/lib/vz/dump
#
set -euo pipefail
KEEP="${1:-2}"
DUMP="${VZDUMP_DIR:-/var/lib/vz/dump}"
if ! [[ "$KEEP" =~ ^[0-9]+$ ]] || ((KEEP < 1)); then
echo "Usage: $0 [KEEP>=1]" >&2
exit 1
fi
cd "$DUMP" || {
echo "Cannot cd to $DUMP" >&2
exit 1
}
shopt -s nullglob
removed=0
# Sidecars for archive basename $1 (path without extension chain handled per type)
remove_sidecars() {
local base="$1"
rm -f -- "${base}.log" "${base}.notes" "${base}.notes.zst" 2>/dev/null || true
}
# $1 = prefix e.g. vzdump-lxc, $2 = vmid
prune_family_globs() {
local prefix="$1"
local vmid="$2"
local -a archives=()
local f n i base
for f in \
"${prefix}-${vmid}-"*.tar.gz \
"${prefix}-${vmid}-"*.tar.zst \
"${prefix}-${vmid}-"*.vma.zst \
"${prefix}-${vmid}-"*.vma.gz; do
[[ -f "$f" ]] || continue
archives+=("$f")
done
((${#archives[@]} == 0)) && return 0
mapfile -t sorted < <(ls -t "${archives[@]}" 2>/dev/null)
n=${#sorted[@]}
((n > KEEP)) || return 0
for ((i = KEEP; i < n; i++)); do
f="${sorted[i]}"
base="${f%.tar.gz}"
base="${base%.tar.zst}"
base="${base%.vma.zst}"
base="${base%.vma.gz}"
rm -f -- "$f"
remove_sidecars "$base"
((removed += 1)) || true
done
}
# Legacy: vzdump-<vmid>-<digits>.tar.*
prune_legacy_vmid() {
local vmid="$1"
local -a archives=()
local f n i base
for f in vzdump-"${vmid}"-*.tar.gz vzdump-"${vmid}"-*.tar.zst vzdump-"${vmid}"-*.vma.zst vzdump-"${vmid}"-*.vma.gz; do
[[ -f "$f" ]] || continue
[[ "$f" == vzdump-lxc-* || "$f" == vzdump-qemu-* ]] && continue
archives+=("$f")
done
((${#archives[@]} == 0)) && return 0
mapfile -t sorted < <(ls -t "${archives[@]}" 2>/dev/null)
n=${#sorted[@]}
((n > KEEP)) || return 0
for ((i = KEEP; i < n; i++)); do
f="${sorted[i]}"
base="${f%.tar.gz}"
base="${base%.tar.zst}"
base="${base%.vma.zst}"
base="${base%.vma.gz}"
rm -f -- "$f"
remove_sidecars "$base"
((removed += 1)) || true
done
}
declare -A vmid_lxc=()
declare -A vmid_qemu=()
declare -A vmid_legacy=()
for f in vzdump-lxc-*; do
[[ -f "$f" ]] || continue
[[ "$f" =~ ^vzdump-lxc-([0-9]+)- ]] || continue
vmid_lxc["${BASH_REMATCH[1]}"]=1
done
for f in vzdump-qemu-*; do
[[ -f "$f" ]] || continue
[[ "$f" =~ ^vzdump-qemu-([0-9]+)- ]] || continue
vmid_qemu["${BASH_REMATCH[1]}"]=1
done
for f in vzdump-[0-9]*-*; do
[[ -f "$f" ]] || continue
[[ "$f" == vzdump-lxc-* || "$f" == vzdump-qemu-* ]] && continue
[[ "$f" =~ \.(tar\.gz|tar\.zst|vma\.zst|vma\.gz)$ ]] || continue
[[ "$f" =~ ^vzdump-([0-9]+)-[0-9_]+ ]] || continue
vmid_legacy["${BASH_REMATCH[1]}"]=1
done
for vmid in "${!vmid_lxc[@]}"; do
prune_family_globs vzdump-lxc "$vmid"
done
for vmid in "${!vmid_qemu[@]}"; do
prune_family_globs vzdump-qemu "$vmid"
done
for vmid in "${!vmid_legacy[@]}"; do
prune_legacy_vmid "$vmid"
done
echo "prune-proxmox-vzdump-dump: removed ${removed} archive(s); keep=${KEEP} newest per VMID in ${DUMP}"
df -h "$DUMP" 2>/dev/null || df -h /var/lib/vz