Skip to content

Commit 37daae8

Browse files
committed
more pg backup logic
1 parent a0fc6dd commit 37daae8

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

splunkconf-backup/bin/splunkconf-backup.sh

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,66 @@ if [ "$MODE" == "0" ] || [ "$MODE" == "pg" ]; then
19291929
echo_log "${PG_RESULT}"
19301930
echo_log ""
19311931
echo_log "✅ Postgres connectivity verified"
1932+
PG_API_PORT=$(lsof -i -P | awk '/postgres/ && /LISTEN/ && $9 ~ /localhost/ && $9 !~ /5432/ {split($9, a, ":"); ports[++count] = a[2]} END {if (count >= 2) print ports[2]}')
1933+
if [ -z "$PG_API_PORT" ]; then
1934+
debug_log "ERROR: Could not determine PostgreSQL API port"
1935+
else
1936+
debug_log "PostgreSQL API port: $PG_API_PORT"
1937+
PG_ADMIN_USER=postgres_admin
1938+
1939+
# Retrieve password from PGPASSFILE (extracts password field for postgres_admin)
1940+
PG_ADMIN_PASS=$(awk -F':' -v user="$PG_ADMIN_USER" '$4 == user {print $5; exit}' "$PGPASS_FILE")
1941+
1942+
if [ -z "$PG_ADMIN_PASS" ]; then
1943+
echo_log "ERROR: Could not retrieve password for user '$PG_ADMIN_USER' from $PGPASS_FILE"
1944+
else
1945+
PG_AUTH_BASIC=$(echo -n "$PG_ADMIN_USER:$PG_ADMIN_PASS" | base64)
1946+
debug_log "Using API port : $PG_API_PORT"
1947+
debug_log "Using admin user: $PG_ADMIN_USER"
1948+
# Set the target database (uncomment the one you need)
1949+
# DB_NAME="search_metadata"
1950+
# DB_NAME="kvstore"
1951+
# DB_NAME="acies_config_service"
1952+
DB_NAME="opamp_service"
1953+
1954+
BACKUP_FILE="/tmp/testdump"
1955+
debug_log "Triggering backup for database: $DB_NAME ..."
1956+
1957+
BACKUP_RESPONSE=$(curl -s -X POST "https://localhost:$PG_API_PORT/v1/postgres/recovery/backup" \
1958+
-H "Content-Type: application/json" \
1959+
-H "Authorization: Basic $PG_AUTH_BASIC" \
1960+
-d "{\"database\": \"$DB_NAME\", \"backupFile\": \"$BACKUP_FILE\"}" \
1961+
-k)
1962+
debug_log "Backup response: $BACKUP_RESPONSE"
1963+
# ---------------------------------------------------------------------------
1964+
# --- 4. Extract Backup ID from Response ---
1965+
# ---------------------------------------------------------------------------
1966+
1967+
# Assumes the response is JSON containing an "id" field e.g. {"id":"abc-123",...}
1968+
PG_BACKUP_ID=$(echo "$BACKUP_RESPONSE" | grep -o '"id":"[^"]*"' | cut -d':' -f2 | tr -d '"')
1969+
1970+
if [ -z "$PG_BACKUP_ID" ]; then
1971+
debug_log "ERROR: Could not extract backup ID from response"
1972+
else
1973+
1974+
debug_log "Backup ID: $PG_BACKUP_ID"
1975+
1976+
# ---------------------------------------------------------------------------
1977+
# --- 5. Verify Backup Status ---
1978+
# ---------------------------------------------------------------------------
1979+
debug_log "Checking backup status for ID: $PG_BACKUP_ID ..."
1980+
1981+
BACKUP_STATUS=$(curl -s -X GET "https://localhost:$PG_API_PORT/v1/postgres/recovery/status/$PG_BACKUP_ID" \
1982+
-H "Content-Type: application/json" \
1983+
-H "Authorization: Basic $PG_AUTH_BASIC" \
1984+
-k )
1985+
if [ -z "$BACKUP_STATUS" ]; then
1986+
debug_log "ERROR: Could not get backup status"
1987+
else
1988+
debug_log "Got BACKUP_STATUS=$BACKUP_STATUS PG_BACKUP_ID=$PG_BACKUP_ID"
1989+
fi
1990+
fi
1991+
fi
19321992
fi
19331993
else
19341994
warn_log "❌ ERROR: psql binary not found at ${SPLUNK_HOME}/bin/psql — skipping direct connectivity test"

0 commit comments

Comments
 (0)