diff --git a/splunkconf-backup/bin/splunkconf-backup.sh b/splunkconf-backup/bin/splunkconf-backup.sh index 7f34bf3..3fc97c2 100755 --- a/splunkconf-backup/bin/splunkconf-backup.sh +++ b/splunkconf-backup/bin/splunkconf-backup.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/bin/bash -x exec > /tmp/splunkconf-backup-debug.log 2>&1 # in normal condition @@ -130,8 +130,9 @@ exec > /tmp/splunkconf-backup-debug.log 2>&1 # 20240610 more splunk_assist support # 20240611 add support to get hostname via env variable for special k8s case and add hostname form checks # 20240620 fix test for splunk_assist and remove stanza instead of removing value when inconsistency detected +# 20240629 replace direct var inclusion with loading function logic -VERSION="20240620a" +VERSION="20240629a" ###### BEGIN default parameters # dont change here, use the configuration file to override them @@ -571,6 +572,33 @@ function checklock() { fi } + +function load_settings_from_file () { + FI=$1 + if [ -e "$FI" ]; then + regclass="([a-zA-Z]+)[[:space:]]*=[[:space:]]*\"{0,1}([a-zA-Z0-9_\-]+)\"{0,1}" + regclass2="^(#|\[)" + # Read the file line by line, remove spaces then create a variable if start by splunk + while read -r line; do + if [[ "${line}" =~ $regclass2 ]]; then + debug_log "comment line or stanza line with line=$line" + elif [ -z "${line-unset}" ]; then + debug_log "empty line" + elif [[ "${line}" =~ $regclass ]]; then + var_name=${BASH_REMATCH[1]} + var_value=${BASH_REMATCH[2]} + # Dynamically create the variable with its value + declare -g "$var_name=$var_value" + debug_log "OK:form ok, start with splunk setting $var_name=$var_value, $var_name=${!var_name}" + else + debug_log "KO:invalid form line=$line" + fi + done < $FI + else + debug_log "file $FI is not present" + fi +} + function check_assist () { # we will increment if needed INCONSISTENT=0 @@ -732,25 +760,30 @@ CPCMD2="" APPDIR=`pwd` debug_log "app=splunkconf-backup result=running SPLUNK_HOME=$SPLUNK_HOME splunkconfappdir=${APPDIR} loading splukconf-backup.conf file" if [[ -f "./default/splunkconf-backup.conf" ]]; then - . ./default/splunkconf-backup.conf +# . ./default/splunkconf-backup.conf + load_settings_from_file ./default/splunkconf-backup.conf debug_log "INFO: splunkconf-backup.conf default succesfully included" else debug_log "INFO: splunkconf-backup.conf default not found or not readable. Using defaults from script " fi if [[ -f "./local/splunkconf-backup.conf" ]]; then - . ./local/splunkconf-backup.conf + #. ./local/splunkconf-backup.conf + load_settings_from_file ./local/splunkconf-backup.conf debug_log "INFO: splunkconf-backup.conf local succesfully included" else debug_log "INFO: splunkconf-backup.conf local not present, using only default" fi # take over over default and local if [[ -f "${SPLUNK_HOME}/system/local/splunkconf-backup.conf" ]]; then - . ${SPLUNK_HOME}/system/local/splunkconf-backup.conf && (echo_log "INFO: splunkconf-backup.conf system local succesfully included") + load_settings_from_file ${SPLUNK_HOME}/system/local/splunkconf-backup.conf + #. ${SPLUNK_HOME}/system/local/splunkconf-backup.conf && (echo_log "INFO: splunkconf-backup.conf system local succesfully included") else debug_log "INFO: splunkconf-backup.conf in system/local not present, no need to include it" fi +#debug_log "INFO: MYVAR=$MYVAR, MYTEST=$MYTEST" + regclass="^[A-Z_]+$" if [[ "${REMOTES3STORAGECLASS}" = "auto" ]]; then REMOTES3STORAGECLASS="STANDARD_IA"