#!/usr/bin/env bash set -euo pipefail source /etc/restic/restic.env # Metrics file for node_exporter METRICS_DIR="/var/lib/node_exporter/textfile_collector" METRICS_FILE="${METRICS_DIR}/restic_prune.prom" mkdir -p "${METRICS_DIR}" # Temporary file for atomic writes TEMP_FILE=$(mktemp) # Start prune START_TIME=$(date +%s) if restic forget \ --keep-daily {{ restic_retention.daily }} \ --keep-weekly {{ restic_retention.weekly }} \ --keep-monthly {{ restic_retention.monthly }} \ --prune; then # Prune succeeded STATUS=1 echo "# HELP restic_prune_success Whether the last prune succeeded (1=success, 0=failure)" > "${TEMP_FILE}" echo "# TYPE restic_prune_success gauge" >> "${TEMP_FILE}" echo "restic_prune_success ${STATUS}" >> "${TEMP_FILE}" echo "# HELP restic_prune_timestamp_seconds Timestamp of last prune completion" >> "${TEMP_FILE}" echo "# TYPE restic_prune_timestamp_seconds gauge" >> "${TEMP_FILE}" echo "restic_prune_timestamp_seconds $(date +%s)" >> "${TEMP_FILE}" echo "# HELP restic_prune_duration_seconds Duration of last prune in seconds" >> "${TEMP_FILE}" echo "# TYPE restic_prune_duration_seconds gauge" >> "${TEMP_FILE}" echo "restic_prune_duration_seconds $(($(date +%s) - START_TIME))" >> "${TEMP_FILE}" # Move temp file to final location atomically mv "${TEMP_FILE}" "${METRICS_FILE}" exit 0 else # Prune failed STATUS=0 echo "# HELP restic_prune_success Whether the last prune succeeded (1=success, 0=failure)" > "${TEMP_FILE}" echo "# TYPE restic_prune_success gauge" >> "${TEMP_FILE}" echo "restic_prune_success ${STATUS}" >> "${TEMP_FILE}" echo "# HELP restic_prune_timestamp_seconds Timestamp of last prune attempt" >> "${TEMP_FILE}" echo "# TYPE restic_prune_timestamp_seconds gauge" >> "${TEMP_FILE}" echo "restic_prune_timestamp_seconds $(date +%s)" >> "${TEMP_FILE}" # Move temp file to final location atomically mv "${TEMP_FILE}" "${METRICS_FILE}" exit 1 fi