#!/bin/sh # Version: 2.1.0 # # 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. # # ----------------------------------------------------------------------------- # --- 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 "โŒ FATAL: Configuration file not found at '${CONFIG_FILE}'" >&2 exit 1 fi # shellcheck source=aurora.conf . "$CONFIG_FILE" # --- Validate Configuration --- if [ ! -x "$HDBSQL" ]; then echo "โŒ FATAL: hdbsql is not found or not executable at '${HDBSQL}'" >&2 exit 1 fi # --- 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..." # First, get the original company name from the source schema. # The query returns a header and the name in quotes. sed gets the second line, tr removes the quotes, xargs trims whitespace. echo " -> Fetching original company name from '${SOURCE_SCHEMA}'..." ORIGINAL_COMPNY_NAME=$("$HDBSQL" -U "$DB_ADMIN_KEY" "SELECT \"CompnyName\" FROM \"${SOURCE_SCHEMA}\".\"CINF\"" | sed -n '2p' | tr -d '"' | xargs) # Construct the new name in the desired format. DATE_STAMP=$(date "+%Y-%m-%d") NEW_COMPNY_NAME="AURORA - ${ORIGINAL_COMPNY_NAME} - ${DATE_STAMP}" echo " -> New company name set to: '${NEW_COMPNY_NAME}'" echo " -> Updating CINF table..." "$HDBSQL" -U "$DB_ADMIN_KEY" "UPDATE \"${AURORA_SCHEMA}\".CINF SET \"CompnyName\" = '${NEW_COMPNY_NAME}';" >/dev/null echo " -> Updating OADM table..." "$HDBSQL" -U "$DB_ADMIN_KEY" "UPDATE \"${AURORA_SCHEMA}\".OADM SET \"CompnyName\" = '${NEW_COMPNY_NAME}', \"PrintHeadr\" = '${NEW_COMPNY_NAME}';" >/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