From d3d73070765f2c6dffa329f3e58ef52b1970e61c Mon Sep 17 00:00:00 2001 From: Tomi Eckert Date: Wed, 24 Sep 2025 21:40:55 +0200 Subject: [PATCH] rewrite aurora --- aurora/aurora.conf | 42 ++++++---- aurora/aurora.sh | 201 ++++++++++++++++++++++----------------------- packages.conf | 2 +- 3 files changed, 125 insertions(+), 120 deletions(-) diff --git a/aurora/aurora.conf b/aurora/aurora.conf index 3dc970f..65fb070 100644 --- a/aurora/aurora.conf +++ b/aurora/aurora.conf @@ -1,31 +1,39 @@ -# Configuration for the HANA Aurora Refresh Script -# Place this file in the same directory as the aurora.sh script. +# Configuration for the Aurora Refresh Script (aurora_refresh.sh) +# Place this file in the same directory as the script. # --- Main Settings --- # The source production schema to be copied. -SCHEMA="SBO_DEMO" +# Example: "SBO_COMPANY_PROD" +SOURCE_SCHEMA="SBODEMOHU" -# The user who will be granted privileges on the new Aurora schema. -AURORA_SCHEMA_USER="B1_53424F5F4348494D5045585F4155524F5241_RW" +# The HANA user that will be granted read/write access to the new Aurora schema. +# This is typically a technical user for the application. +# Example: "B1_..._RW" +AURORA_USER="B1_XXXXXXXXX_RW" -# The database user for performing backup and administrative tasks. -BACKOP_USER="CRONKEY" +# The secure user store key for the HANA database user with privileges to +# perform EXPORT, IMPORT, DROP SCHEMA, and GRANT commands (e.g., SYSTEM). +# Using a key (hdbuserstore) is more secure than hardcoding a password. +# Example: "CRONKEY" +DB_ADMIN_KEY="CRONKEY" -# --- Paths and Files --- +# --- Paths --- -# The base directory for storing the temporary schema export. -BACKUP_DIR="/hana/shared/backup/schema" +# The base directory where the temporary schema export folder will be created. +# Ensure the adm user has write permissions here. +BACKUP_BASE_DIR="/hana/shared/backup/schema" # The full path to the HANA hdbsql executable. HDBSQL="/usr/sap/NDB/HDB00/exe/hdbsql" +# The root directory where post-import SQL scripts are located. +SQL_SCRIPTS_ROOT="/usr/sap/NDB/home/tools/sql" -# --- Post-Import Scripts --- +# --- Post-Import Scripts (Optional) --- -# The root directory where the SQL script and its associated files are located. -SQL_ROOT="/usr/sap/NDB/home/tools" - -# A space-separated list of SQL script files to run after the import is complete. -# These scripts should be located in the SCRIPT_ROOT directory. -POST_SQL="" \ No newline at end of file +# A space-separated list of SQL script filenames to run after the import is complete. +# The script will look for these files inside the SQL_SCRIPTS_ROOT directory. +# Leave empty ("") if no scripts are needed. +# Example: "update_user_emails.sql cleanup_tables.sql" +POST_IMPORT_SQL="" diff --git a/aurora/aurora.sh b/aurora/aurora.sh index f5a04db..4990d17 100644 --- a/aurora/aurora.sh +++ b/aurora/aurora.sh @@ -1,115 +1,112 @@ #!/bin/sh -# Version: 1.2.6 -# Exit immediately if a command exits with a non-zero status. -set -e +# Version: 2.0.0 +# +# Aurora Refresh Script v2.1 +# +# Purpose: Performs an automated refresh of a SAP HANA schema. It exports a +# production schema and re-imports it under a new name ("Aurora") +# to create an up-to-date, non-production environment for testing. +# Designed to be run via cron, typically in the early morning. +# +# ----------------------------------------------------------------------------- -# === SETUP === -# Determine script's directory and source the configuration file. +# --- Basic Setup --- +# Exit immediately if any command fails or if an unset variable is used. +set -eu + +# --- Configuration --- +# Load the configuration file located in the same directory as the script. SCRIPT_DIR=$(dirname "$0") CONFIG_FILE="${SCRIPT_DIR}/aurora.conf" if [ ! -f "$CONFIG_FILE" ]; then - echo "❌ Error: Configuration file not found at '${CONFIG_FILE}'" >&2 + echo "❌ FATAL: Configuration file not found at '${CONFIG_FILE}'" >&2 exit 1 fi # shellcheck source=aurora.conf . "$CONFIG_FILE" -# === DERIVED VARIABLES === -TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") -AURORA="${SCHEMA}_AURORA" -AURORA_TEMP_DIR="${BACKUP_DIR}/${AURORA}" -COMPNYNAME="${SCHEMA#SBO_}" -[[ "$COMPNYNAME" == *_PROD ]] && COMPNYNAME="${COMPNYNAME%_PROD}" # Remove _PROD suffix if it exists - -# === FUNCTIONS === - -run_sql() { - echo "ℹ️ Executing: $1" - "$HDBSQL" -U "${BACKOP_USER}" "$1" >/dev/null -} - -show_info() { - echo "Source Schema: ${SCHEMA}" - echo "Target Schema: ${AURORA}" - echo "Target Schema User: ${AURORA_SCHEMA_USER}" - echo "Company Name: ${COMPNYNAME}" - echo "Export Directory: ${AURORA_TEMP_DIR}" -} - -usage() { - echo "Usage: $0 [--info]" - echo " --info : Show configuration information." - echo " (No argument) : Drop, export, import, grant privileges, and run post-scripts." -} - -export_schema() { - echo "⬇️ Starting schema export for '${SCHEMA}'..." - mkdir -p "$AURORA_TEMP_DIR" - run_sql "EXPORT \"${SCHEMA}\".\"*\" AS BINARY INTO '$AURORA_TEMP_DIR' WITH REPLACE;" - echo "✅ Schema export completed." -} - -import_and_rename() { - echo "⬆️ Starting import and rename to '${AURORA}'..." - run_sql "IMPORT \"${SCHEMA}\".\"*\" FROM '$AURORA_TEMP_DIR' WITH IGNORE EXISTING RENAME SCHEMA \"${SCHEMA}\" TO \"${AURORA}\";" - echo "ℹ️ Updating company name fields..." - "$HDBSQL" -U "${BACKOP_USER}" -c ";" -I - </dev/null || echo "⚠️ Could not drop schema '${AURORA}'. It might not exist." >&2 - echo "✅ Old schema dropped." -} - -run_post_scripts() { - echo "⚙️ Running post-import SQL scripts: ${POST_SQL}..." - for sql_file in $POST_SQL; do - echo " - Running script: ${sql_file}" - "$HDBSQL" -U "${BACKOP_USER}" -I "${SCRIPT_ROOT}/${sql_file}" - done - echo "✅ All post-import scripts completed." -} - -cleanup_exported_files() { - echo "🧹 Cleaning up exported files from '${AURORA_TEMP_DIR}'..." - rm -rf "$AURORA_TEMP_DIR" - echo "✅ Exported files cleaned up." -} - -# === SCRIPT EXECUTION === - -if [ $# -eq 0 ]; then - echo "🚀 Starting 'complete' operation (default)..." - drop_aurora_schema - export_schema - import_and_rename - grant_privileges - run_post_scripts - cleanup_exported_files - echo "🎉 'Complete' operation finished successfully!" - exit 0 +# --- Validate Configuration --- +if [ ! -x "$HDBSQL" ]; then + echo "❌ FATAL: hdbsql is not found or not executable at '${HDBSQL}'" >&2 + exit 1 fi -case "$1" in - --info) - show_info - ;; - *) - echo "❌ Error: Invalid argument '$1'." >&2 - usage - exit 1 - ;; -esac +# --- Derived Variables (Do Not Edit) --- +TIMESTAMP=$(date "+%Y-%m-%d %H:%M:%S") +AURORA_SCHEMA="${SOURCE_SCHEMA}_AURORA" +EXPORT_DIR="${BACKUP_BASE_DIR}/${AURORA_SCHEMA}_TEMP_EXPORT" +COMPANY_NAME_BASE=$(echo "${SOURCE_SCHEMA}" | sed 's/^SBO_//' | sed 's/_PROD$//') + +# --- Main Execution --- +echo +echo "🚀 [$(date "+%T")] Starting Aurora Refresh for '${SOURCE_SCHEMA}'" +echo "--------------------------------------------------------" +echo " Source Schema: ${SOURCE_SCHEMA}" +echo " Target Aurora Schema: ${AURORA_SCHEMA}" +echo " Temp Export Path: ${EXPORT_DIR}" +echo "--------------------------------------------------------" + +# 1. Drop the old Aurora schema if it exists. +echo "🗑️ Dropping old schema '${AURORA_SCHEMA}' (if it exists)..." +"$HDBSQL" -U "$DB_ADMIN_KEY" "DROP SCHEMA \"${AURORA_SCHEMA}\" CASCADE" >/dev/null 2>&1 || echo " -> Schema did not exist. Continuing." + +# 2. Prepare the temporary export directory. +echo "📁 Preparing temporary export directory..." +rm -rf "$EXPORT_DIR" +mkdir -p "$EXPORT_DIR" + +# 3. Export the source schema. +echo "⬇️ Exporting source schema '${SOURCE_SCHEMA}' to binary files..." +"$HDBSQL" -U "$DB_ADMIN_KEY" "EXPORT \"${SOURCE_SCHEMA}\".\"*\" AS BINARY INTO '${EXPORT_DIR}' WITH REPLACE;" >/dev/null +echo " -> Export complete." + +# 4. Import the data into the new Aurora schema. +echo "⬆️ Importing data and renaming schema to '${AURORA_SCHEMA}'..." +"$HDBSQL" -U "$DB_ADMIN_KEY" "IMPORT \"${SOURCE_SCHEMA}\".\"*\" FROM '${EXPORT_DIR}' WITH IGNORE EXISTING RENAME SCHEMA \"${SOURCE_SCHEMA}\" TO \"${AURORA_SCHEMA}\";" >/dev/null +echo " -> Import complete." + +# 5. Update company name in CINF and OADM tables. +echo "✍️ Updating company name fields in the new schema..." +echo " -> Updating CINF table..." +"$HDBSQL" -U "$DB_ADMIN_KEY" "UPDATE \"${AURORA_SCHEMA}\".CINF SET \"CompnyName\" = 'AURORA ${COMPANY_NAME_BASE} ${TIMESTAMP}';" >/dev/null +echo " -> Updating OADM table..." +"$HDBSQL" -U "$DB_ADMIN_KEY" "UPDATE \"${AURORA_SCHEMA}\".OADM SET \"CompnyName\" = 'AURORA ${COMPANY_NAME_BASE} ${TIMESTAMP}', \"PrintHeadr\" = 'AURORA ${COMPANY_NAME_BASE} ${TIMESTAMP}';" >/dev/null +echo " -> Company info updated." + +# 6. Grant privileges to the read/write user. +echo "🔑 Granting ALL privileges on '${AURORA_SCHEMA}' to '${AURORA_USER}'..." +"$HDBSQL" -U "$DB_ADMIN_KEY" "GRANT ALL PRIVILEGES ON SCHEMA \"${AURORA_SCHEMA}\" TO \"${AURORA_USER}\";" >/dev/null +echo " -> Privileges granted." + +# 7. Run post-import SQL scripts, if any are defined. +if [ -n "$POST_IMPORT_SQL" ]; then + echo "⚙️ Running post-import SQL scripts..." + # Use word splitting intentionally here + # shellcheck disable=SC2086 + for sql_file in $POST_IMPORT_SQL; do + full_path="${SQL_SCRIPTS_ROOT}/${sql_file}" + if [ -f "$full_path" ]; then + echo " -> Executing: ${sql_file}" + "$HDBSQL" -U "$DB_ADMIN_KEY" -I "$full_path" + else + echo " -> ⚠️ WARNING: Script not found: ${full_path}" >&2 + fi + done +else + echo "ℹ️ No post-import SQL scripts to run." +fi + +# 8. Clean up the temporary export files. +echo "🧹 Cleaning up temporary directory '${EXPORT_DIR}'..." +rm -rf "$EXPORT_DIR" +echo " -> Cleanup complete." + +echo "--------------------------------------------------------" +echo "✅ [$(date "+%T")] Aurora Refresh finished successfully!" +echo + +exit 0 + + + diff --git a/packages.conf b/packages.conf index c3349b2..ef331d2 100644 --- a/packages.conf +++ b/packages.conf @@ -7,7 +7,7 @@ declare -A SCRIPT_PACKAGES # The version should match the "# Version: x.x.x" line in the main script file. -SCRIPT_PACKAGES["Aurora Suite"]="1.2.6|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["Aurora Suite"]="2.0.0|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"]="1.0.5|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 Suite"]="1.0.5|https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.sh https://git.technopunk.space/tomi/Scripts/raw/branch/main/monitor/monitor.conf" SCRIPT_PACKAGES["Key Manager"]="1.2.1|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hdb_keymanager.sh"