Created
September 2, 2025 11:00
-
-
Save kmoppel/f5fa85b5c51ebacc76bed674418cda23 to your computer and use it in GitHub Desktop.
A Cron script to report errors when nightly pgbackup backups have failed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# PS Running in CRON mode - meaning output produced only on errors | |
# Expected inputs: | |
# 1) the pgbackrest config file path, e.g. /etc/pgbackrest/15/main/pgbackrest.conf | |
# 2) the stanza name | |
# 3) max age threshold in seconds of last valid backup. | |
# errors if no valid backup within the threshold | |
which pgbackrest &>/dev/null || { echo "pgbackrest command not found"; exit 1; } | |
which jq &>/dev/null || { echo "jq command not found"; exit 1; } | |
if [ -z "$3" ]; then | |
echo "usage: check_backrest.sh $BACKREST_CONFIG_FILE $STANZA_NAME $MAX_BACKUP_AGE_SECONDS" | |
exit 1 | |
fi | |
BACKREST_CONFIG="$1" | |
STANZA="$2" | |
MAX_AGE_SECONDS="$3" | |
set -u | |
STANZA_INFO=$(pgbackrest info --config="$BACKREST_CONFIG" --stanza="$STANZA" --output=json) || "Failed to fetch latest backup info for stanza $STANZA" | |
LAST_BACKUP_COMPLETED_EPOCH=$(echo "$STANZA_INFO" | jq '.[0] | .backup[-1] | .timestamp.stop') || "Failed to extract time label" | |
if [ "$LAST_BACKUP_COMPLETED_EPOCH" == "null" ]; then | |
echo "Missing last backup info for stanza $STANZA" | |
exit 1 | |
fi | |
EPOCH_SECONDS=$(date +%s) | |
MAX_ALLOWED_THRESHOLD_EPOCH=$((EPOCH_SECONDS-MAX_AGE_SECONDS)) | |
if [ "$LAST_BACKUP_COMPLETED_EPOCH" -lt "$MAX_ALLOWED_THRESHOLD_EPOCH" ]; then | |
echo "Last backup too old for stanza $STANZA! LAST_BACKUP_COMPLETED_EPOCH: $LAST_BACKUP_COMPLETED_EPOCH, OK threshold: $MAX_ALLOWED_THRESHOLD_EPOCH" | |
exit 1 | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment