-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxmox_snapshot_report.sh
More file actions
106 lines (89 loc) · 3.51 KB
/
Copy pathproxmox_snapshot_report.sh
File metadata and controls
106 lines (89 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# Ensure cron can find all commands
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
export PATH
# -----------------------------
# Configuration Section
# -----------------------------
REPORT_EMAIL="admin@example.tld"
MIN_SNAPSHOT_AGE_DAYS=0
INCLUDE_VM_SNAPSHOTS=true
INCLUDE_LXC_SNAPSHOTS=true
MAIL_SUBJECT="Proxmox Snapshot Report"
TMPFILE="/tmp/pve_snapshot_report.txt"
ENABLE_LOGGING=true
DATE_NOW=$(date +%s)
# Log function controlled by ENABLE_LOGGING flag
log() {
if [[ "$ENABLE_LOGGING" == "true" ]]; then
echo "[INFO] $*"
fi
}
log "Creating snapshot report at $TMPFILE"
# Write header without snapshot size
echo -e "Type\tVMID\tSnapshot Name\tSnapshot Date\tDescription" > "$TMPFILE"
# Check if snapshot timestamp is older than the minimum age in days
snapshot_is_old_enough() {
local snapshot_ts=$1
if [[ "$snapshot_ts" =~ ^[0-9]+$ ]]; then
local age_days=$(( (DATE_NOW - snapshot_ts) / 86400 ))
[[ $age_days -ge $MIN_SNAPSHOT_AGE_DAYS ]]
else
return 0
fi
}
# Process VM snapshots using qm command
process_vm_snapshots() {
log "Scanning VM snapshots..."
for vmid in $(qm list | awk 'NR>1 {print $1}'); do
log "Processing VM $vmid"
while read -r snapshot; do
[[ -z "$snapshot" ]] && continue
meta=$(qm listsnapshot "$vmid" 2>/dev/null | awk -v snap="$snapshot" '
$1 == snap {found=1; next}
found && $1 ~ /^[^ ]/ {exit}
found {print}
')
description=$(echo "$meta" | grep 'description:' | cut -d: -f2- | xargs)
snapshot_ts=$(echo "$meta" | grep 'snaptime:' | awk '{print $2}')
snapshot_date="N/A"
[[ -n "$snapshot_ts" ]] && snapshot_date=$(date -d "@$snapshot_ts" +"%Y-%m-%d %H:%M")
if snapshot_is_old_enough "$snapshot_ts"; then
# Properly aligned output using printf
printf "VM\t%s\t%s\t%s\t%s\n" "$vmid" "$snapshot" "$snapshot_date" "$description" >> "$TMPFILE"
fi
done < <(qm listsnapshot "$vmid" 2>/dev/null | grep -E '^\s{2}[0-9a-zA-Z]')
done
}
# Process LXC container snapshots using pct command
process_lxc_snapshots() {
log "Scanning LXC snapshots via pct..."
for vmid in $(pct list | awk 'NR>1 {print $1}'); do
log "Processing LXC $vmid"
pct listsnapshot "$vmid" 2>/dev/null | awk '/->/' | while read -r line; do
name=$(echo "$line" | sed -E 's/.*-> ([^ ]+).*/\1/')
if [[ "$name" == "current" ]]; then
continue
fi
date=$(echo "$line" | awk '{print $3, $4}')
desc=$(echo "$line" | cut -d' ' -f5- | sed 's/^[[:space:]]*//')
desc=$(echo "$desc" | sed -E 's/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} ?//')
snapshot_ts=$(date -d "$date" +%s 2>/dev/null)
if snapshot_is_old_enough "$snapshot_ts"; then
# Properly aligned output using printf
printf "LXC\t%s\t%s\t%s\t%s\n" "$vmid" "$name" "$date" "$desc" >> "$TMPFILE"
fi
done
done
}
# Execute processing logic
[[ "$INCLUDE_VM_SNAPSHOTS" == "true" ]] && process_vm_snapshots
[[ "$INCLUDE_LXC_SNAPSHOTS" == "true" ]] && process_lxc_snapshots
# Send report if data exists
if [[ $(wc -l < "$TMPFILE") -gt 1 ]]; then
log "Sending report to $REPORT_EMAIL"
column -t -s $'\t' < "$TMPFILE" | mail -s "$MAIL_SUBJECT" "$REPORT_EMAIL"
else
log "No snapshots found. No mail sent."
fi
rm -f "$TMPFILE"