Skip to content

Commit 108b96d

Browse files
committed
Fix restore path detection in Azure pgBackRest test script
1 parent 717c8a8 commit 108b96d

File tree

1 file changed

+58
-11
lines changed

1 file changed

+58
-11
lines changed

azure-pgbackrest.sh

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -450,25 +450,36 @@ test_local() {
450450
docker exec "$CONTAINER" psql -U postgres -d postgres -c "DROP TABLE restore_test;" >/dev/null
451451
print_success "Test table dropped"
452452

453+
# Get the actual PostgreSQL data directory before stopping container
454+
PGDATA_PATH=$(docker exec "$CONTAINER" bash -c 'psql -U postgres -d postgres -t -c "SHOW data_directory;" 2>/dev/null | xargs' || echo "")
455+
if [ -z "$PGDATA_PATH" ]; then
456+
# Fallback: try to find it from the config
457+
PGDATA_PATH=$(docker exec "$CONTAINER" bash -c 'grep "pg1-path" /etc/pgbackrest/pgbackrest.conf | tail -1 | cut -d= -f2 | xargs' || echo "")
458+
fi
459+
if [ -z "$PGDATA_PATH" ]; then
460+
# Fallback: try to find PG_VERSION file
461+
PGDATA_PATH=$(docker exec "$CONTAINER" find /var/lib/postgresql -name "PG_VERSION" -type f 2>/dev/null | head -1 | xargs dirname 2>/dev/null || echo "")
462+
fi
463+
if [ -z "$PGDATA_PATH" ]; then
464+
# Final fallback: use default
465+
PGDATA_PATH="/var/lib/postgresql/data"
466+
print_warning "Could not detect PostgreSQL data directory, using default: $PGDATA_PATH"
467+
else
468+
print_success "Detected PostgreSQL data directory: $PGDATA_PATH"
469+
fi
470+
453471
# Stop container
454472
docker stop "$CONTAINER"
455473

456474
# Restore from local
457475
print_header "Restore from Local (repo1)"
458-
# Get the actual PGDATA path from the container
459-
PGDATA_PATH=$(docker exec "$CONTAINER" bash -c 'echo $PGDATA' 2>/dev/null || echo "/var/lib/postgresql/18/docker")
460-
if [ -z "$PGDATA_PATH" ] || [ "$PGDATA_PATH" = "/var/lib/postgresql/data" ]; then
461-
# Try to detect the actual path
462-
PGDATA_PATH=$(docker exec "$CONTAINER" find /var/lib/postgresql -name "PG_VERSION" -type f 2>/dev/null | head -1 | xargs dirname 2>/dev/null || echo "/var/lib/postgresql/18/docker")
463-
fi
464-
465476
echo "Restoring to PGDATA: $PGDATA_PATH"
466477
docker run --rm \
467478
--entrypoint bash \
468479
-v pgdata:/var/lib/postgresql \
469480
-v pgrepo:/var/lib/pgbackrest \
470481
"$IMAGE" \
471-
-lc "rm -rf $PGDATA_PATH/* && pgbackrest --stanza=demo restore --set='$BACKUP_LABEL_LOCAL' --type=immediate --pg1-path=$PGDATA_PATH"
482+
-lc "rm -rf \"$PGDATA_PATH\"/* && pgbackrest --stanza=demo restore --set='$BACKUP_LABEL_LOCAL' --type=immediate --pg1-path=\"$PGDATA_PATH\""
472483

473484
print_success "Restore complete"
474485

@@ -685,6 +696,20 @@ test_azure_blob() {
685696
fi
686697
print_success "Test data dropped"
687698

699+
# Get the actual PostgreSQL data directory before stopping container
700+
ACTUAL_DATA_DIR=$(docker exec "$CONTAINER" bash -c 'psql -U postgres -d postgres -t -c "SHOW data_directory;" 2>/dev/null | xargs' || echo "")
701+
if [ -z "$ACTUAL_DATA_DIR" ]; then
702+
# Fallback: try to find it from the config
703+
ACTUAL_DATA_DIR=$(docker exec "$CONTAINER" bash -c 'grep "pg1-path" /etc/pgbackrest/pgbackrest.conf | tail -1 | cut -d= -f2 | xargs' || echo "")
704+
fi
705+
if [ -z "$ACTUAL_DATA_DIR" ]; then
706+
# Final fallback: use default
707+
ACTUAL_DATA_DIR="/var/lib/postgresql/data"
708+
print_warning "Could not detect PostgreSQL data directory, using default: $ACTUAL_DATA_DIR"
709+
else
710+
print_success "Detected PostgreSQL data directory: $ACTUAL_DATA_DIR"
711+
fi
712+
688713
# Stop container
689714
docker stop "$CONTAINER"
690715

@@ -697,10 +722,15 @@ test_azure_blob() {
697722
-e AZURE_KEY="$AZURE_SAS_TOKEN" \
698723
-e AZURE_KEY_TYPE="$AZURE_KEY_TYPE" \
699724
-e AZURE_REPO_PATH="$AZURE_REPO_PATH" \
725+
-e ACTUAL_DATA_DIR="$ACTUAL_DATA_DIR" \
700726
-v pgdata:/var/lib/postgresql \
701727
-v pgrepo:/var/lib/pgbackrest \
702728
"$IMAGE" \
703-
-lc "/usr/local/bin/configure-azure.sh && rm -rf /var/lib/postgresql/data/* && pgbackrest --stanza=demo restore --set='$BACKUP_LABEL_AZURE' --type=immediate"
729+
-lc "/usr/local/bin/configure-azure.sh || true; \
730+
DATA_DIR=\${ACTUAL_DATA_DIR:-/var/lib/postgresql/data}; \
731+
echo \"Restoring to data directory: \$DATA_DIR\"; \
732+
rm -rf \"\$DATA_DIR\"/* && \
733+
pgbackrest --stanza=demo restore --set='$BACKUP_LABEL_AZURE' --type=immediate --pg1-path=\"\$DATA_DIR\""
704734

705735
print_success "Restore complete"
706736

@@ -1013,6 +1043,20 @@ PYEOF
10131043
fi
10141044
print_success "Test data dropped"
10151045

1046+
# Get the actual PostgreSQL data directory before stopping container
1047+
ACTUAL_DATA_DIR=$(docker exec "$CONTAINER" bash -c 'psql -U postgres -d postgres -t -c "SHOW data_directory;" 2>/dev/null | xargs' || echo "")
1048+
if [ -z "$ACTUAL_DATA_DIR" ]; then
1049+
# Fallback: try to find it from the config
1050+
ACTUAL_DATA_DIR=$(docker exec "$CONTAINER" bash -c 'grep "pg1-path" /etc/pgbackrest/pgbackrest.conf | tail -1 | cut -d= -f2 | xargs' || echo "")
1051+
fi
1052+
if [ -z "$ACTUAL_DATA_DIR" ]; then
1053+
# Final fallback: use default
1054+
ACTUAL_DATA_DIR="/var/lib/postgresql/data"
1055+
print_warning "Could not detect PostgreSQL data directory, using default: $ACTUAL_DATA_DIR"
1056+
else
1057+
print_success "Detected PostgreSQL data directory: $ACTUAL_DATA_DIR"
1058+
fi
1059+
10161060
# Stop container
10171061
docker stop "$CONTAINER"
10181062

@@ -1024,6 +1068,7 @@ PYEOF
10241068
-e AZURE_CONTAINER="$AZURE_CONTAINER" \
10251069
-e AZURE_KEY_TYPE="$AZURE_KEY_TYPE" \
10261070
-e AZURE_REPO_PATH="$AZURE_REPO_PATH" \
1071+
-e ACTUAL_DATA_DIR="$ACTUAL_DATA_DIR" \
10271072
-v pgdata:/var/lib/postgresql \
10281073
-v pgrepo:/var/lib/pgbackrest \
10291074
"$IMAGE" \
@@ -1044,8 +1089,10 @@ PYEOF
10441089
chmod 640 /etc/pgbackrest/pgbackrest.conf; \
10451090
rm -f \$TMP_FILE; \
10461091
fi; \
1047-
rm -rf /var/lib/postgresql/data/* && \
1048-
pgbackrest --repo=2 --stanza=demo restore --set='$BACKUP_LABEL_AMI' --type=immediate"
1092+
DATA_DIR=\${ACTUAL_DATA_DIR:-/var/lib/postgresql/data}; \
1093+
echo \"Restoring to data directory: \$DATA_DIR\"; \
1094+
rm -rf \"\$DATA_DIR\"/* && \
1095+
pgbackrest --repo=2 --stanza=demo restore --set='$BACKUP_LABEL_AMI' --type=immediate --pg1-path=\"\$DATA_DIR\""
10491096

10501097
print_success "Restore complete"
10511098

0 commit comments

Comments
 (0)