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
48if [[ -f /etc/environment ]]; then
59 # Safely source environment variables
1216REMOTE_NAME=" "
1317RCLONE_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
1657log () {
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
3189debug_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
641704upload_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() {
729799check_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
746822perform_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
9311013perform_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
9921080perform_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
0 commit comments