chore: improve collectors and pins
This commit is contained in:
@@ -4,8 +4,36 @@ set -euo pipefail
|
||||
OUT_DIR="${1:?usage: collect_backup_restore_drill.sh <out_dir>}"
|
||||
mkdir -p "$OUT_DIR"
|
||||
|
||||
ROOT="../vm-skills"
|
||||
LATEST="$(find "$ROOT" -type f -name "*restore*drill*.json" 2>/dev/null | sort | tail -n 1 || true)"
|
||||
DEFAULT_GLOB="*restore*drill*.json"
|
||||
DEFAULT_ROOTS=(
|
||||
"../vm-ops/60-backups"
|
||||
"../vm-skills"
|
||||
)
|
||||
|
||||
matches=()
|
||||
if [[ -n "${VMCC_RESTORE_DRILL_GLOB:-}" ]]; then
|
||||
if [[ -f "$VMCC_RESTORE_DRILL_GLOB" ]]; then
|
||||
matches+=("$VMCC_RESTORE_DRILL_GLOB")
|
||||
elif [[ -d "$VMCC_RESTORE_DRILL_GLOB" ]]; then
|
||||
while IFS= read -r -d '' file; do
|
||||
matches+=("$file")
|
||||
done < <(find "$VMCC_RESTORE_DRILL_GLOB" -type f -name "$DEFAULT_GLOB" -print0 2>/dev/null)
|
||||
else
|
||||
while IFS= read -r file; do
|
||||
matches+=("$file")
|
||||
done < <(compgen -G "$VMCC_RESTORE_DRILL_GLOB" || true)
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ ${#matches[@]} -eq 0 ]]; then
|
||||
for root in "${DEFAULT_ROOTS[@]}"; do
|
||||
if [[ -d "$root" ]]; then
|
||||
while IFS= read -r -d '' file; do
|
||||
matches+=("$file")
|
||||
done < <(find "$root" -type f -name "$DEFAULT_GLOB" -print0 2>/dev/null)
|
||||
fi
|
||||
done
|
||||
fi
|
||||
|
||||
file_mtime_epoch() {
|
||||
local file="$1"
|
||||
@@ -20,8 +48,11 @@ file_mtime_iso() {
|
||||
local file="$1"
|
||||
local mtime
|
||||
mtime="$(file_mtime_epoch "$file")"
|
||||
if date -u -r "$file" "+%Y-%m-%dT%H:%M:%SZ" >/dev/null 2>&1; then
|
||||
date -u -r "$file" "+%Y-%m-%dT%H:%M:%SZ"
|
||||
|
||||
# BSD/macOS: date -r <epoch>
|
||||
if date -u -r "$mtime" "+%Y-%m-%dT%H:%M:%SZ" >/dev/null 2>&1; then
|
||||
date -u -r "$mtime" "+%Y-%m-%dT%H:%M:%SZ"
|
||||
# GNU: date -d "@<epoch>"
|
||||
elif date -u -d "@${mtime}" "+%Y-%m-%dT%H:%M:%SZ" >/dev/null 2>&1; then
|
||||
date -u -d "@${mtime}" "+%Y-%m-%dT%H:%M:%SZ"
|
||||
else
|
||||
@@ -29,13 +60,26 @@ file_mtime_iso() {
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ -n "$LATEST" && -f "$LATEST" ]]; then
|
||||
LATEST=""
|
||||
LATEST_TS=0
|
||||
for file in "${matches[@]}"; do
|
||||
if [[ -f "$file" ]]; then
|
||||
ts="$(file_mtime_epoch "$file")"
|
||||
if [[ "$ts" -gt "$LATEST_TS" || ( "$ts" -eq "$LATEST_TS" && ( -z "$LATEST" || "$file" > "$LATEST" ) ) ]]; then
|
||||
LATEST_TS="$ts"
|
||||
LATEST="$file"
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ -n "$LATEST" ]]; then
|
||||
TS="$(file_mtime_iso "$LATEST")"
|
||||
cat > "$OUT_DIR/backup_restore_drill.json" <<JSON
|
||||
{"collected": true, "path": "$LATEST", "observed_at": "$TS"}
|
||||
JSON
|
||||
jq -n \
|
||||
--arg path "$LATEST" \
|
||||
--arg ts "$TS" \
|
||||
'{collected:true, path:$path, observed_at:$ts}' \
|
||||
> "$OUT_DIR/backup_restore_drill.json"
|
||||
else
|
||||
cat > "$OUT_DIR/backup_restore_drill.json" <<'JSON'
|
||||
{"collected": false, "reason": "no restore drill artifacts found"}
|
||||
JSON
|
||||
jq -n '{collected:false, reason:"no restore drill artifacts found"}' \
|
||||
> "$OUT_DIR/backup_restore_drill.json"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user