Files
hana-scripts/hana_backup.sh
2026-03-12 20:12:20 +01:00

84 lines
3.6 KiB
Bash

#!/bin/bash
# =============================================================================
# SAP HANA Backup Status Monitoring Script
# Checks last successful backup age
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
SCRIPT_NAME="hana_backup"
# Load configuration
source "${SCRIPT_DIR}/hana.conf"
source "${SCRIPT_DIR}/hana_lib.sh"
# Acquire lock
LOCK_FILE=$(acquire_lock "$SCRIPT_NAME")
if [ $? -ne 0 ]; then
exit 1
fi
trap 'release_lock "$LOCK_FILE"' EXIT
log_message "$SCRIPT_NAME" "Starting backup status check..."
# Check if hdbsql is available
if [ ! -x "$HDBSQL_PATH" ]; then
log_message "$SCRIPT_NAME" "ERROR: hdbsql not found or not executable at ${HDBSQL_PATH}"
send_notification_if_changed "$SCRIPT_NAME" "hana_hdbsql_path_backup" "HANA Monitor Error" "hdbsql not found or not executable at ${HDBSQL_PATH}" "true" "HDBSQL_ERROR"
exit 1
fi
# SQL Query for last successful backup
BACKUP_SQL="SELECT TOP 1 SYS_START_TIME FROM M_BACKUP_CATALOG WHERE ENTRY_TYPE_NAME = 'complete data backup' AND STATE_NAME = 'successful' ORDER BY SYS_START_TIME DESC"
# Execute SQL query as HANA user with improved error handling
backup_result=$(su - "$HANA_USER" -c "$HDBSQL_PATH -U $HANA_USER_KEY -j -a -x \"$BACKUP_SQL\"" 2>&1)
sql_status=$?
if [ $sql_status -ne 0 ]; then
log_message "$SCRIPT_NAME" "ERROR: Failed to execute backup query. Exit code: ${sql_status}"
send_notification_if_changed "$SCRIPT_NAME" "hana_backup_query_error" "HANA Backup Error" "Failed to execute backup query. Exit code: ${sql_status}" "true" "QUERY_ERROR"
exit 1
fi
last_backup_date=$(echo "$backup_result" | tr -d '"' | sed 's/\..*//')
if [[ -z "$last_backup_date" || "$last_backup_date" == *"error"* || "$last_backup_date" == *"Error"* ]]; then
message="No successful complete data backup found for ${COMPANY_NAME} HANA."
log_message "$SCRIPT_NAME" "CRITICAL: ${message}"
send_notification_if_changed "$SCRIPT_NAME" "hana_backup_status" "HANA Backup" "$message" "true" "NO_BACKUP"
exit 1
fi
# Clear any previous query error state
send_notification_if_changed "$SCRIPT_NAME" "hana_backup_query_error" "HANA Backup" "Backup query successful." "false" "OK"
# Calculate backup age
last_backup_epoch=$(date -d "$last_backup_date" +%s 2>/dev/null)
if [ $? -ne 0 ]; then
log_message "$SCRIPT_NAME" "ERROR: Failed to parse backup date: ${last_backup_date}"
send_notification_if_changed "$SCRIPT_NAME" "hana_backup_status" "HANA Backup" "Failed to parse backup date: ${last_backup_date}" "true" "DATE_PARSE_ERROR"
exit 1
fi
current_epoch=$(date +%s)
threshold_seconds=$((BACKUP_THRESHOLD_HOURS * 3600))
age_seconds=$((current_epoch - last_backup_epoch))
age_hours=$((age_seconds / 3600))
if [ $age_seconds -lt 0 ]; then
log_message "$SCRIPT_NAME" "WARNING: Backup timestamp is in the future. Possible clock skew."
age_hours=0
fi
if [ $age_seconds -gt $threshold_seconds ]; then
message="Last successful HANA backup for ${COMPANY_NAME} is ${age_hours} hours old, which exceeds the threshold of ${BACKUP_THRESHOLD_HOURS} hours. Last backup was on: ${last_backup_date}."
log_message "$SCRIPT_NAME" "CRITICAL: ${message}"
send_notification_if_changed "$SCRIPT_NAME" "hana_backup_status" "HANA Backup" "$message" "true" "${age_hours}h"
else
message="Last successful backup is ${age_hours} hours old (Threshold: ${BACKUP_THRESHOLD_HOURS} hours)."
log_message "$SCRIPT_NAME" "SUCCESS: ${message}"
send_notification_if_changed "$SCRIPT_NAME" "hana_backup_status" "HANA Backup" "$message" "false" "OK"
fi
log_message "$SCRIPT_NAME" "Backup check complete."