From 3355727a9b65fb23828aa1ae7fd58622f51e839c Mon Sep 17 00:00:00 2001 From: Tomi Eckert Date: Wed, 8 Oct 2025 21:14:02 +0200 Subject: [PATCH] refactor(backup): Rewrite backup tool to use hanatool instead of its own implementation. --- backup/backup.sh | 221 ++++++++++++++++------------------------------- packages.conf | 2 +- 2 files changed, 75 insertions(+), 148 deletions(-) diff --git a/backup/backup.sh b/backup/backup.sh index 0ae1ff3..d3da7a1 100644 --- a/backup/backup.sh +++ b/backup/backup.sh @@ -1,18 +1,19 @@ #!/bin/bash -# Version: 1.0.5 +# Version: 1.0.6 # ============================================================================== # SAP HANA Backup Script # # Performs schema exports for one or more schemas and/or tenant backups for a -# SAP HANA database. Designed to be executed via a cronjob. +# SAP HANA database using hanatool.sh. 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 +# Find the script's own directory to locate the config file and hanatool.sh SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd) CONFIG_FILE="${SCRIPT_DIR}/backup.conf" +HANATOOL_PATH="${SCRIPT_DIR}/../hanatool.sh" # Assuming hanatool.sh is in the parent directory # Check for config file and source it if [[ -f "$CONFIG_FILE" ]]; then @@ -22,176 +23,102 @@ else exit 1 fi -# Check if hdbsql executable exists -if [[ ! -x "$HDBSQL_PATH" ]]; then - echo "❌ Error: hdbsql not found or not executable at '${HDBSQL_PATH}'" +# Check if hanatool.sh executable exists +if [[ ! -x "$HANATOOL_PATH" ]]; then + echo "❌ Error: hanatool.sh not found or not executable at '${HANATOOL_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. -# Accepts the schema name as its first argument. -perform_schema_export() { - local schema_name="$1" - if [[ -z "$schema_name" ]]; then - echo " ❌ Error: No schema name provided to perform_schema_export function." - return 1 - fi - - echo "⬇️ Starting schema export for '${schema_name}'..." - - local timestamp - timestamp=$(date +%Y%m%d_%H%M%S) - local export_base_dir="${BACKUP_BASE_DIR}/schema" - local export_path="${export_base_dir}/${schema_name}_${timestamp}" - local query_export_path="$export_path" - - if [[ "$COMPRESS_SCHEMA" == "true" ]]; then - export_path="${export_base_dir}/tmp/${schema_name}_${timestamp}" - query_export_path="$export_path" - echo " ℹ️ Compression enabled. Using temporary export path: ${export_path}" - fi - - local archive_file="${export_base_dir}/${schema_name}_${timestamp}.tar.gz" - - mkdir -p "$(dirname "$export_path")" - - local query="EXPORT \"${schema_name}\".\"*\" AS BINARY INTO '${query_export_path}' WITH REPLACE THREADS ${THREADS};" - - "$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}'." - - if [[ "$COMPRESS_SCHEMA" == "true" ]]; then - echo " 🗜️ Compressing exported files..." - tar -czf "$archive_file" -C "$(dirname "$export_path")" "$(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" - rmdir --ignore-fail-on-non-empty "$(dirname "$export_path")" - echo " ✨ Cleanup complete." - else - echo " ❌ Error: Failed to compress '${export_path}'." - fi - else - echo " ℹ️ Compression disabled. Raw export files are located at '${export_path}'." - fi - else - echo " ❌ Error: Failed to export schema '${schema_name}' (hdbsql exit code: ${exit_code})." - fi -} - -# Loops through the schemas in the config file and runs an export for each. -run_all_schema_exports() { - if [[ -z "$SCHEMA_NAMES" ]]; then - echo " ⚠️ Warning: SCHEMA_NAMES variable is not set in config. Skipping schema export." - return - fi - - echo "🔎 Found schemas to export: ${SCHEMA_NAMES}" - for schema in $SCHEMA_NAMES; do - perform_schema_export "$schema" - echo "--------------------------------------------------" - done -} - -# REFACTORED: Generic function to back up any database (Tenant or SYSTEMDB). -# Arguments: 1:Backup Name (for logging), 2:User Key, 3:Base Directory, 4:Compression Flag -perform_database_backup() { - local backup_name="$1" - local user_key="$2" - local backup_base_dir="$3" - local compress_enabled="$4" - - echo "⬇️ Starting ${backup_name} backup..." - - local timestamp - timestamp=$(date +%Y%m%d_%H%M%S) - local backup_path_prefix - local backup_target_dir - - if [[ "$compress_enabled" == "true" ]]; then - backup_target_dir="${backup_base_dir}/tmp" - backup_path_prefix="${backup_target_dir}/backup_${timestamp}" - echo " ℹ️ Compression enabled. Using temporary backup path: ${backup_path_prefix}" - else - backup_target_dir="$backup_base_dir" - backup_path_prefix="${backup_target_dir}/backup_${timestamp}" - fi - - mkdir -p "$backup_target_dir" - - local query="BACKUP DATA USING FILE ('${backup_path_prefix}')" - - "$HDBSQL_PATH" -U "$user_key" "$query" > /dev/null 2>&1 - local exit_code=$? - - if [[ "$exit_code" -eq 0 ]]; then - echo " ✅ Successfully initiated ${backup_name} backup with prefix '${backup_path_prefix}'." - - if [[ "$compress_enabled" == "true" ]]; then - local archive_file="${backup_base_dir}/backup_${timestamp}.tar.gz" - echo " 🗜️ Compressing backup files..." - tar -czf "$archive_file" -C "$backup_target_dir" . - local tar_exit_code=$? - - if [[ "$tar_exit_code" -eq 0 ]]; then - echo " ✅ Successfully created archive '${archive_file}'." - echo " 🧹 Cleaning up temporary directory..." - rm -rf "$backup_target_dir" - echo " ✨ Cleanup complete." - else - echo " ❌ Error: Failed to compress backup files in '${backup_target_dir}'." - fi - fi - else - echo " ❌ Error: Failed to initiate ${backup_name} backup (hdbsql exit code: ${exit_code})." - fi -} - # --- Main Execution --- -echo "⚙️ Starting HANA backup process..." +echo "⚙️ Starting HANA backup process using hanatool.sh..." mkdir -p "$BACKUP_BASE_DIR" +# Common options for hanatool.sh +COMMON_OPTIONS="--hdbsql \"$HDBSQL_PATH\"" + case "$BACKUP_TYPE" in schema) - run_all_schema_exports + if [[ -z "$SCHEMA_NAMES" ]]; then + echo " ⚠️ Warning: SCHEMA_NAMES variable is not set in config. Skipping schema export." + else + echo "🔎 Found schemas to export: ${SCHEMA_NAMES}" + for schema in $SCHEMA_NAMES; do + echo "⬇️ Starting schema export for '${schema}'..." + SCHEMA_EXPORT_OPTIONS="$COMMON_OPTIONS" + if [[ "$COMPRESS_SCHEMA" == "true" ]]; then + SCHEMA_EXPORT_OPTIONS+=" --compress" + fi + "$HANATOOL_PATH" "$USER_KEY" export "$schema" "${BACKUP_BASE_DIR}/schema" $SCHEMA_EXPORT_OPTIONS + if [[ $? -ne 0 ]]; then + echo "❌ Error: Schema export for '${schema}' failed." + fi + echo "--------------------------------------------------" + done + fi ;; tenant) - perform_database_backup "Tenant" "$USER_KEY" "${BACKUP_BASE_DIR}/tenant" "$COMPRESS_TENANT" + echo "⬇️ Starting Tenant backup..." + TENANT_BACKUP_OPTIONS="$COMMON_OPTIONS" + if [[ "$COMPRESS_TENANT" == "true" ]]; then + TENANT_BACKUP_OPTIONS+=" --compress" + fi + "$HANATOOL_PATH" "$USER_KEY" backup "${BACKUP_BASE_DIR}/tenant" $TENANT_BACKUP_OPTIONS + if [[ $? -ne 0 ]]; then + echo "❌ Error: Tenant backup failed." + fi ;; all) - run_all_schema_exports - perform_database_backup "Tenant" "$USER_KEY" "${BACKUP_BASE_DIR}/tenant" "$COMPRESS_TENANT" + if [[ -z "$SCHEMA_NAMES" ]]; then + echo " ⚠️ Warning: SCHEMA_NAMES variable is not set in config. Skipping schema export." + else + echo "🔎 Found schemas to export: ${SCHEMA_NAMES}" + for schema in $SCHEMA_NAMES; do + echo "⬇️ Starting schema export for '${schema}'..." + SCHEMA_EXPORT_OPTIONS="$COMMON_OPTIONS" + if [[ "$COMPRESS_SCHEMA" == "true" ]]; then + SCHEMA_EXPORT_OPTIONS+=" --compress" + fi + "$HANATOOL_PATH" "$USER_KEY" export "$schema" "${BACKUP_BASE_DIR}/schema" $SCHEMA_EXPORT_OPTIONS + if [[ $? -ne 0 ]]; then + echo "❌ Error: Schema export for '${schema}' failed." + fi + echo "--------------------------------------------------" + done + fi + + echo "⬇️ Starting Tenant backup..." + TENANT_BACKUP_OPTIONS="$COMMON_OPTIONS" + if [[ "$COMPRESS_TENANT" == "true" ]]; then + TENANT_BACKUP_OPTIONS+=" --compress" + fi + "$HANATOOL_PATH" "$USER_KEY" backup "${BACKUP_BASE_DIR}/tenant" $TENANT_BACKUP_OPTIONS + if [[ $? -ne 0 ]]; then + echo "❌ Error: Tenant backup failed." + fi ;; *) echo " ❌ Error: Invalid BACKUP_TYPE '${BACKUP_TYPE}' in config. Use 'schema', 'tenant', or 'all'." ;; esac -# NEW: Check if SYSTEMDB backup is enabled, regardless of BACKUP_TYPE (as long as it's not 'schema' only) +# Check if SYSTEMDB backup is enabled, regardless of BACKUP_TYPE (as long as it's not 'schema' only) if [[ "$BACKUP_TYPE" == "tenant" || "$BACKUP_TYPE" == "all" ]]; then if [[ "$BACKUP_SYSTEMDB" == "true" ]]; then echo "--------------------------------------------------" if [[ -z "$SYSTEMDB_USER_KEY" ]]; then echo " ❌ Error: BACKUP_SYSTEMDB is true, but SYSTEMDB_USER_KEY is not set in config." else - perform_database_backup "SYSTEMDB" "$SYSTEMDB_USER_KEY" "${BACKUP_BASE_DIR}/systemdb" "$COMPRESS_TENANT" + echo "⬇️ Starting SYSTEMDB backup..." + SYSTEMDB_BACKUP_OPTIONS="$COMMON_OPTIONS" + if [[ "$COMPRESS_TENANT" == "true" ]]; then # SYSTEMDB compression uses COMPRESS_TENANT setting + SYSTEMDB_BACKUP_OPTIONS+=" --compress" + fi + "$HANATOOL_PATH" "$SYSTEMDB_USER_KEY" backup "${BACKUP_BASE_DIR}/systemdb" $SYSTEMDB_BACKUP_OPTIONS + if [[ $? -ne 0 ]]; then + echo "❌ Error: SYSTEMDB backup failed." + fi fi fi fi diff --git a/packages.conf b/packages.conf index 58d66ce..168f826 100644 --- a/packages.conf +++ b/packages.conf @@ -10,7 +10,7 @@ declare -A SCRIPT_PACKAGES # Format: short_name="Display Name|Version|Description|URL1 URL2..." SCRIPT_PACKAGES["aurora"]="Aurora Suite|2.1.0|A collection of scripts for managing Aurora database instances.|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"]="Backup Suite|1.0.5|A comprehensive script for backing up system files and databases.|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["backup"]="Backup Suite|1.0.6|A comprehensive script for backing up system files and databases.|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["monitor"]="Monitor Suite|1.3.1|Scripts for monitoring system health and performance metrics.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.conf|https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.hook.sh" SCRIPT_PACKAGES["keymanager"]="Key Manager|1.2.2|A utility for managing HDB user keys for SAP HANA.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/keymanager.sh" SCRIPT_PACKAGES["cleaner"]="File Cleaner|1.1.0|A simple script to clean up temporary files and logs.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/cleaner.sh"