Skip to content

Commit 3603438

Browse files
authored
Merge pull request #32 from mfa777/dev/enable_ssh_server
Enhance SSH connectivity tests in WAL-G script: add SFTP support for …
2 parents 99b2a5a + eb8e2fb commit 3603438

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

test/test-walg-e2e.sh

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ POSTGRES_SERVICE_NAME="postgres"
1212
BACKUP_SERVICE_NAME="backup"
1313
SSH_SERVICE_NAME="ssh-server"
1414

15+
# Count only pure WAL segment files (exclude .backup history / sentinel files)
16+
get_remote_pure_wal_count() {
17+
local out
18+
if [[ "${ENABLE_SSH_SERVER:-0}" == "1" ]]; then
19+
out=$(docker exec "$SSH_CONTAINER_ID" bash -c "find /backups -type f -name '*.lz4' -o -name '*.br' -o -name '*.gz' -o -name '*.zst' | sed 's|.*/||' | grep -E '^[0-9A-F]{24}\.(lz4|br|gz|zst)$' | wc -l" 2>/dev/null || true)
20+
else
21+
local remote_path="$(get_remote_backup_path)"
22+
out=$(docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"echo 'ls ${remote_path}' | sftp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -P ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 2>/dev/null | grep -E '\\.(lz4|br|gz|zst)$' | sed 's|.*/||' | grep -E '^[0-9A-F]{24}\\.(lz4|br|gz|zst)$' | wc -l\"" 2>/dev/null || true)
23+
fi
24+
out=$(echo "${out:-0}" | tr -d '[:space:]')
25+
[[ -z "$out" || ! "$out" =~ ^[0-9]+$ ]] && echo 0 || echo "$out"
26+
}
27+
1528
# Load environment variables
1629
if [[ -f "$ENV_FILE" ]]; then
1730
set -o allexport
@@ -176,18 +189,17 @@ test_remote_connectivity() {
176189
echof "Testing remote SSH connectivity and wal-g configuration"
177190

178191
# Test SSH connectivity from postgres container
179-
# WALG_SSH_TEST_PORT is set earlier (falls back to derived SSH_PORT)
180-
# Run SSH as the 'postgres' user so it will use the key prepared at
181-
# /var/lib/postgresql/.ssh/walg_key (walg-env-prepare.sh sets WALG_SSH_PRIVATE_KEY_PATH)
182-
if docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -p $WALG_SSH_TEST_PORT ${SSH_USER}@${SSH_HOST} 'echo SSH connection successful'\"" 2>/dev/null; then
183-
pass "SSH connectivity to remote server working"
184-
else
185-
warn "SSH connectivity test failed — collecting verbose SSH output for debugging (as postgres)"
186-
echo "---- SSH verbose debug output start ----"
187-
docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -o PreferredAuthentications=publickey -p $WALG_SSH_TEST_PORT -vvv ${SSH_USER}@${SSH_HOST}\"" || true
188-
echo "---- SSH verbose debug output end ----"
189-
die "Cannot establish SSH connection to remote server"
190-
fi
192+
# For Hetzner Storage Box and similar restricted shells, use SFTP to test connectivity
193+
# SFTP should be available even when shell commands are not
194+
if docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"echo 'ls' | sftp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -P $WALG_SSH_TEST_PORT ${SSH_USER}@${SSH_HOST} 2>/dev/null | grep -q 'sftp>'\"" 2>/dev/null; then
195+
pass "SSH connectivity to remote server working (SFTP)"
196+
else
197+
warn "SSH connectivity test failed — collecting verbose SSH output for debugging (as postgres)"
198+
echo "---- SSH verbose debug output start ----"
199+
docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -o PreferredAuthentications=publickey -p $WALG_SSH_TEST_PORT -vvv ${SSH_USER}@${SSH_HOST}\"" || true
200+
echo "---- SSH verbose debug output end ----"
201+
die "Cannot establish SSH connection to remote server"
202+
fi
191203

192204
# Test wal-g backup-list (should work even if empty)
193205
if docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c 'wal-g backup-list'" >/dev/null 2>&1; then
@@ -216,10 +228,10 @@ get_remote_wal_count() {
216228
# Total count of compressed WAL-related files (segments + backup history markers)
217229
local out
218230
if [[ "${ENABLE_SSH_SERVER:-0}" == "1" ]]; then
219-
out=$(docker exec "$SSH_CONTAINER_ID" bash -c "find /backups -type f \( -name '*.lz4' -o -name '*.br' -o -name '*.gz' -o -name '*.zst' \) 2>/dev/null | wc -l" 2>/dev/null || true)
231+
out=$(docker exec "$SSH_CONTAINER_ID" bash -c "find /backups -type f -name '*.lz4' -o -name '*.br' -o -name '*.gz' -o -name '*.zst' | wc -l" 2>/dev/null || true)
220232
else
221233
local remote_path="$(get_remote_backup_path)"
222-
out=$(docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} \"find ${remote_path} -type f \\\\( -name \\\"*.lz4\\\" -o -name \\\"*.br\\\" -o -name \\\"*.gz\\\" -o -name \\\"*.zst\\\" \\\\) 2>/dev/null | wc -l\"'" 2>/dev/null || true)
234+
out=$(docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"echo 'ls ${remote_path}' | sftp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -P ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 2>/dev/null | grep -c -E '\\.(lz4|br|gz|zst)$'\"" 2>/dev/null || true)
223235
fi
224236
out=$(echo "${out:-0}" | tr -d '[:space:]')
225237
[[ -z "$out" || ! "$out" =~ ^[0-9]+$ ]] && echo 0 || echo "$out"
@@ -268,7 +280,7 @@ test_wal_push_e2e() {
268280
docker exec "$SSH_CONTAINER_ID" bash -c "find /backups -type f 2>/dev/null | head -10" || echo "No files found or directory doesn't exist"
269281
else
270282
remote_path="$(get_remote_backup_path)"
271-
docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} \"find ${remote_path} -type f 2>/dev/null | head -10\"'" || echo "No files found or directory doesn't exist"
283+
docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"echo 'ls ${remote_path}' | sftp -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o BatchMode=yes -P ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 2>/dev/null | head -10\"" || echo "No files found or directory doesn't exist"
272284
fi
273285

274286
# Generate some WAL activity with adaptive polling & forced switches
@@ -350,7 +362,7 @@ test_wal_push_e2e() {
350362
docker exec "$POSTGRES_CONTAINER_ID" bash -c 'seg=$(ls /var/lib/postgresql/data/pg_wal/archive_status/*.ready 2>/dev/null | head -1 || true); if [ -n "$seg" ]; then base=$(basename "$seg" .ready); echo "Found ready segment $base - invoking wal-g wal-push"; wal-g wal-push "/var/lib/postgresql/data/pg_wal/$base" || echo "wal-push failed"; else echo "No .ready segments present"; fi' || true
351363

352364
echo "Checking SSH connectivity from postgres container..."
353-
docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"ssh -o ConnectTimeout=5 -o BatchMode=yes -p ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 'echo SSH test successful'\"" || echo "SSH test failed"
365+
docker exec "$POSTGRES_CONTAINER_ID" bash -c "su - postgres -c \"echo 'ls' | sftp -o StrictHostKeyChecking=no -o ConnectTimeout=5 -o BatchMode=yes -P ${SSH_PORT} ${SSH_USER}@${SSH_HOST} 2>/dev/null | grep -q 'sftp>'\"" || echo "SSH test failed"
354366

355367
echo "Checking backup directory permissions..."
356368
if [[ "${ENABLE_SSH_SERVER:-0}" == "1" ]]; then

0 commit comments

Comments
 (0)