From 010404227371923a3a25f8f18f3af5ec6b05532c Mon Sep 17 00:00:00 2001 From: Tomi Eckert Date: Tue, 9 Sep 2025 10:56:09 +0200 Subject: [PATCH] add backup suite --- backup/backup.conf | 33 +++++++++++ backup/backup.sh | 136 +++++++++++++++++++++++++++++++++++++++++++++ install.sh | 1 + 3 files changed, 170 insertions(+) create mode 100644 backup/backup.conf create mode 100644 backup/backup.sh diff --git a/backup/backup.conf b/backup/backup.conf new file mode 100644 index 0000000..407bfa3 --- /dev/null +++ b/backup/backup.conf @@ -0,0 +1,33 @@ +# ============================================================================== +# Configuration for HANA Backup Script (backup.sh) +# ============================================================================== + +# --- Connection Settings --- + +# Full path to the SAP HANA hdbsql executable. +HDBSQL_PATH="/usr/sap/hdbclient/hdbsql" + +# User key name from the hdbuserstore. +# This key should be configured to connect to the target tenant database. +USER_KEY="CRONKEY" + + +# --- Backup Settings --- + +# The base directory where all backup files and directories will be stored. +# Ensure this directory exists and that the OS user running the script has +# write permissions to it. +BACKUP_BASE_DIR="/hana/backups/automated" + +# Specify the type of backup to perform on script execution. +# Options are: +# 'schema' - Performs only the schema export. +# 'tenant' - Performs only the tenant data backup. +# 'all' - Performs both the schema export and the tenant backup. +BACKUP_TYPE="all" + + +# --- Target Identifiers --- + +# The name of the schema to be exported when BACKUP_TYPE is 'schema' or 'all'. +SCHEMA_NAME="MYSCHEMA" diff --git a/backup/backup.sh b/backup/backup.sh new file mode 100644 index 0000000..94695e3 --- /dev/null +++ b/backup/backup.sh @@ -0,0 +1,136 @@ +#!/bin/bash + +# ============================================================================== +# SAP HANA Backup Script +# +# Performs schema exports and/or tenant backups for a SAP HANA database. +# Designed to be executed via a cronjob. +# Reads all settings from the backup.conf file in the same directory. +# ============================================================================== + +# --- Configuration and Setup --- + +# Find the script's own directory to locate the config file +SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) +CONFIG_FILE="${SCRIPT_DIR}/backup.conf" + +# Check for config file and source it +if [[ -f "$CONFIG_FILE" ]]; then + source "$CONFIG_FILE" +else + echo "❌ Error: Configuration file not found at '${CONFIG_FILE}'" + exit 1 +fi + +# Check if hdbsql executable exists +if [[ ! -x "$HDBSQL_PATH" ]]; then + echo "❌ Error: hdbsql not found or not executable at '${HDBSQL_PATH}'" + exit 1 +fi + +# Calculate threads to use (half of the available cores, but at least 1) +TOTAL_THREADS=$(nproc --all) +THREADS=$((TOTAL_THREADS / 2)) +if [[ "$THREADS" -eq 0 ]]; then + THREADS=1 +fi + +# --- Functions --- + +# Performs a binary export of a specific schema and compresses it. +perform_schema_export() { + echo "⬇️ Starting schema export for '${SCHEMA_NAME}'..." + + local timestamp + timestamp=$(date +%Y%m%d_%H%M%S) + local export_dir="${BACKUP_BASE_DIR}/schema_exports" + local export_path="${export_dir}/${SCHEMA_NAME}_${timestamp}" + local archive_file="${export_path}.tar.gz" + + # Create the target directory if it doesn't exist + mkdir -p "$export_dir" + + # Construct and execute the EXPORT query + local query="EXPORT \"${SCHEMA_NAME}\".\"*\" AS BINARY INTO '${export_path}' WITH REPLACE THREADS ${THREADS};" + + # We redirect stdout and stderr to /dev/null for cleaner cron logs. + # Remove "> /dev/null 2>&1" if you need to debug connection issues. + "$HDBSQL_PATH" -U "$USER_KEY" "$query" > /dev/null 2>&1 + local exit_code=$? + + if [[ "$exit_code" -eq 0 ]]; then + echo " ✅ Successfully exported schema '${SCHEMA_NAME}'." + + # Compress the exported directory + echo " 🗜️ Compressing exported files..." + tar -czf "$archive_file" -C "$export_dir" "$(basename "$export_path")" + local tar_exit_code=$? + + if [[ "$tar_exit_code" -eq 0 ]]; then + echo " ✅ Successfully created archive '${archive_file}'." + echo " 🧹 Cleaning up temporary directory..." + rm -rf "$export_path" + echo " ✨ Cleanup complete." + else + echo " ❌ Error: Failed to compress '${export_path}'." + fi + else + echo " ❌ Error: Failed to export schema '${SCHEMA_NAME}' (hdbsql exit code: ${exit_code})." + fi +} + +# Performs a full backup of the tenant database. +perform_tenant_backup() { + echo "⬇️ Starting tenant backup..." + + local timestamp + timestamp=$(date +%Y%m%d_%H%M%S) + local backup_dir="${BACKUP_BASE_DIR}/tenant_backups" + # HANA will create the final file; we just provide the location and prefix. + local backup_path_prefix="${backup_dir}/backup_${timestamp}" + + # Create the target directory if it doesn't exist + mkdir -p "$backup_dir" + + # The USER_KEY must be configured to connect to the desired tenant database. + local query="BACKUP DATA USING FILE ('${backup_path_prefix}')" + + # We redirect stdout and stderr to /dev/null for cleaner cron logs. + # Remove "> /dev/null 2>&1" if you need to debug connection issues. + "$HDBSQL_PATH" -U "$USER_KEY" "$query" > /dev/null 2>&1 + local exit_code=$? + + if [[ "$exit_code" -eq 0 ]]; then + echo " ✅ Successfully initiated tenant backup with prefix '${backup_path_prefix}'." + else + echo " ❌ Error: Failed to initiate tenant backup (hdbsql exit code: ${exit_code})." + fi +} + +# --- Main Execution --- + +echo "⚙️ Starting HANA backup process..." + +# Ensure the base directory exists +mkdir -p "$BACKUP_BASE_DIR" + +case "$BACKUP_TYPE" in + schema) + perform_schema_export + ;; + tenant) + perform_tenant_backup + ;; + all) + perform_schema_export + echo "" # Add a newline for better readability + perform_tenant_backup + ;; + *) + echo " ❌ Error: Invalid BACKUP_TYPE '${BACKUP_TYPE}' in config. Use 'schema', 'tenant', or 'all'." + ;; +esac + +echo "📦 Backup process complete." +echo "👋 Exiting." + diff --git a/install.sh b/install.sh index 199913a..8b1d20f 100644 --- a/install.sh +++ b/install.sh @@ -6,6 +6,7 @@ declare -A SCRIPT_PACKAGES SCRIPT_PACKAGES["Aurora Suite"]="https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/aurora/aurora.conf" +SCRIPT_PACKAGES["Backup Suite"]="https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/backup/backup.conf" SCRIPT_PACKAGES["Userstore Key Manager"]="https://git.technopunk.space/tomi/Scripts/raw/branch/main/hdb_keymanager.sh" # Example: To add another single script later, just add a new line: # SCRIPT_PACKAGES["My Other Script"]="https://path/to/my-other-script.sh"