Skip to content

Commit 2a0ae82

Browse files
committed
Consolidate environment initialization into unified function across scripts
1 parent 429fccd commit 2a0ae82

5 files changed

Lines changed: 122 additions & 29 deletions

File tree

scripts/backup-functions.sh

Lines changed: 98 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#!/bin/bash
22

3+
# Global variables for environment initialization
4+
ENVIRONMENT_INITIALIZED=false
5+
RCLONE_INITIALIZED=false
6+
37
# Source environment variables if available
48
if [[ -f /etc/environment ]]; then
59
# Safely source environment variables
@@ -12,6 +16,43 @@ fi
1216
REMOTE_NAME=""
1317
RCLONE_CONFIG_PATH="/root/.config/rclone/rclone.conf"
1418

19+
# Initialize environment - this function should be called at the start of any script
20+
initialize_environment() {
21+
if [[ "$ENVIRONMENT_INITIALIZED" == "true" ]]; then
22+
return 0
23+
fi
24+
25+
log "INFO" "Initializing environment..."
26+
27+
# Source environment variables from /etc/environment if available
28+
if [[ -f /etc/environment ]]; then
29+
log "INFO" "Loading environment variables from /etc/environment"
30+
set -a
31+
source /etc/environment 2>/dev/null || true
32+
set +a
33+
fi
34+
35+
# Check required environment variables
36+
if ! check_env; then
37+
log "ERROR" "Environment check failed"
38+
return 1
39+
fi
40+
41+
# Initialize rclone if not already done
42+
if [[ "$RCLONE_INITIALIZED" != "true" ]]; then
43+
log "INFO" "Initializing rclone configuration..."
44+
if ! setup_rclone; then
45+
log "ERROR" "Failed to setup rclone"
46+
return 1
47+
fi
48+
RCLONE_INITIALIZED=true
49+
fi
50+
51+
ENVIRONMENT_INITIALIZED=true
52+
log "INFO" "Environment initialized successfully"
53+
return 0
54+
}
55+
1556
# Logging function
1657
log() {
1758
local level="$1"
@@ -27,6 +68,23 @@ get_first_remote_name() {
2768
grep -m 1 "^\[.*\]" "$config_file" | sed 's/^\[\(.*\)\]/\1/'
2869
}
2970

71+
# Ensure REMOTE_NAME is set (call this before any rclone operations)
72+
ensure_remote_name() {
73+
# First ensure environment is initialized
74+
if ! initialize_environment; then
75+
log "ERROR" "Failed to initialize environment"
76+
return 1
77+
fi
78+
79+
if [[ -z "$REMOTE_NAME" ]]; then
80+
log "ERROR" "REMOTE_NAME is not set after environment initialization"
81+
log "ERROR" "Please ensure rclone is properly configured"
82+
return 1
83+
fi
84+
85+
return 0
86+
}
87+
3088
# Debug function to show environment variables
3189
debug_env() {
3290
log "DEBUG" "Environment variable status:"
@@ -221,10 +279,9 @@ compress_and_upload() {
221279
local remote_filename="$3"
222280
local temp_compressed_file
223281

224-
# Check if REMOTE_NAME is set
225-
if [[ -z "$REMOTE_NAME" ]]; then
226-
log "ERROR" "REMOTE_NAME is not set. Cannot upload file."
227-
log "ERROR" "Please ensure rclone is properly configured."
282+
# Ensure REMOTE_NAME is set
283+
if ! ensure_remote_name; then
284+
log "ERROR" "Failed to determine rclone remote name. Cannot upload file."
228285
return 1
229286
fi
230287

@@ -274,6 +331,12 @@ cleanup_old_backups() {
274331
local retention_days="${BACKUP_RETENTION_DAYS:-3}"
275332
local cutoff_date=$(date -d "$retention_days days ago" +%Y%m%d)
276333

334+
# Ensure REMOTE_NAME is set
335+
if ! ensure_remote_name; then
336+
log "ERROR" "Failed to determine rclone remote name. Cannot cleanup old backups."
337+
return 1
338+
fi
339+
277340
local db_identifier=$(get_database_identifier)
278341
local remote_base_path="${RCLONE_REMOTE_PATH:-postgres-backups}/${db_identifier}"
279342

@@ -640,6 +703,13 @@ perform_pgbackrest_backup() {
640703
# Upload pgBackRest repository to remote storage
641704
upload_pgbackrest_repository() {
642705
local backup_type="$1"
706+
707+
# Ensure REMOTE_NAME is set
708+
if ! ensure_remote_name; then
709+
log "ERROR" "Failed to determine rclone remote name. Cannot upload repository."
710+
return 1
711+
fi
712+
643713
local db_identifier=$(get_database_identifier)
644714
local repo_remote_path="${RCLONE_REMOTE_PATH:-postgres-backups}/${db_identifier}/repository"
645715

@@ -729,6 +799,12 @@ create_backup_archive() {
729799
check_and_perform_daily_backup() {
730800
log "INFO" "Checking for today's full base backup..."
731801

802+
# Ensure REMOTE_NAME is set
803+
if ! ensure_remote_name; then
804+
log "ERROR" "Failed to determine rclone remote name. Cannot check for existing backups."
805+
return 1
806+
fi
807+
732808
local today=$(date '+%Y%m%d')
733809
local db_identifier=$(get_database_identifier)
734810
local remote_base_path="${RCLONE_REMOTE_PATH:-postgres-backups}/${db_identifier}/base"
@@ -744,6 +820,12 @@ check_and_perform_daily_backup() {
744820

745821
# Perform complete backup process
746822
perform_full_backup() {
823+
# Initialize environment first
824+
if ! initialize_environment; then
825+
log "ERROR" "Failed to initialize environment"
826+
return 1
827+
fi
828+
747829
log "INFO" "Starting complete backup process..."
748830

749831
# Configure pgbackrest stanza if not already done
@@ -929,6 +1011,12 @@ check_full_backup_exists() {
9291011

9301012
# Perform incremental backup
9311013
perform_incremental_backup() {
1014+
# Initialize environment first
1015+
if ! initialize_environment; then
1016+
log "ERROR" "Failed to initialize environment"
1017+
return 1
1018+
fi
1019+
9321020
log "INFO" "Starting incremental backup process..."
9331021

9341022
# Configure pgbackrest stanza if not already done
@@ -990,6 +1078,12 @@ perform_incremental_backup() {
9901078

9911079
# Perform differential backup
9921080
perform_differential_backup() {
1081+
# Initialize environment first
1082+
if ! initialize_environment; then
1083+
log "ERROR" "Failed to initialize environment"
1084+
return 1
1085+
fi
1086+
9931087
log "INFO" "Starting differential backup process..."
9941088

9951089
# Configure pgbackrest stanza if not already done

scripts/docker-entrypoint.sh

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,10 @@ initialize_backup_system() {
2323
done > /etc/environment
2424
log "INFO" "Environment variables exported to /etc/environment"
2525

26-
# Check environment variables
27-
log "INFO" "Checking environment variables..."
28-
if ! check_env; then
29-
log "ERROR" "Environment check failed"
30-
return 1
31-
fi
32-
33-
# Setup rclone
34-
log "INFO" "Setting up rclone..."
35-
if ! setup_rclone; then
36-
log "ERROR" "Rclone setup failed"
26+
# Initialize environment using the unified function
27+
log "INFO" "Initializing environment..."
28+
if ! initialize_environment; then
29+
log "ERROR" "Environment initialization failed"
3730
return 1
3831
fi
3932

scripts/incremental-backup.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,6 @@ should_run_incremental_backup() {
126126
perform_scheduled_incremental_backup() {
127127
incremental_log "INFO" "Starting scheduled incremental backup..."
128128

129-
# Setup rclone if needed
130-
if ! setup_rclone; then
131-
incremental_log "ERROR" "Failed to setup rclone for incremental backup"
132-
return 1
133-
fi
134-
135129
# Perform incremental backup (this will auto-create full backup if needed)
136130
if perform_incremental_backup; then
137131
incremental_log "INFO" "Scheduled incremental backup completed successfully"
@@ -197,6 +191,12 @@ main() {
197191

198192
incremental_log "INFO" "=== Starting Scheduled Incremental Backup ==="
199193

194+
# Initialize environment first
195+
if ! initialize_environment; then
196+
incremental_log "ERROR" "Failed to initialize environment"
197+
exit 1
198+
fi
199+
200200
# Check if backup should run
201201
if ! should_run_incremental_backup; then
202202
incremental_log "INFO" "Incremental backup skipped"

scripts/manual-backup.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,14 @@ fi
8484
check_backup_config() {
8585
log "INFO" "=== Checking Backup Configuration ==="
8686

87-
# Check environment variables
88-
if ! check_env; then
89-
log "ERROR" "Environment check failed"
90-
return 1
91-
fi
92-
9387
# Check PostgreSQL connection
9488
if ! wait_for_postgres 30; then
9589
log "ERROR" "PostgreSQL is not available"
9690
return 1
9791
fi
9892

9993
# Check pgBackRest configuration
100-
local stanza=$(get_stanza_name)
94+
local stanza="${PGBACKREST_STANZA:-main}"
10195
log "INFO" "Using stanza: $stanza"
10296

10397
# Check if stanza exists
@@ -128,7 +122,7 @@ check_backup_config() {
128122
list_backups() {
129123
log "INFO" "=== Available Backups ==="
130124

131-
local stanza=$(get_stanza_name)
125+
local stanza="${PGBACKREST_STANZA:-main}"
132126

133127
# List pgBackRest backups
134128
log "INFO" "pgBackRest backups:"
@@ -197,6 +191,12 @@ perform_manual_backup() {
197191
main() {
198192
log "INFO" "=== Manual Backup Script Started ==="
199193

194+
# Initialize environment first
195+
if ! initialize_environment; then
196+
log "ERROR" "Failed to initialize environment"
197+
exit 1
198+
fi
199+
200200
if [ "$CHECK_ONLY" = true ]; then
201201
check_backup_config
202202
exit $?

scripts/wal-monitor.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,12 @@ trap cleanup EXIT
264264

265265
# Main function
266266
main() {
267+
# Initialize environment first
268+
if ! initialize_environment; then
269+
wal_log "ERROR" "Failed to initialize environment"
270+
exit 1
271+
fi
272+
267273
# Ensure required tools are available
268274
if ! command -v bc >/dev/null 2>&1; then
269275
wal_log "ERROR" "bc command not found. Please install bc package."

0 commit comments

Comments
 (0)