Skip to content

Commit 423a3f5

Browse files
stuggiclaude
andcommitted
Fix glob pattern expansion in oc exec commands
Glob patterns were being expanded by the local shell instead of the remote shell in the pod, causing "No backup files found" error. Problem: oc exec pod -- ls /backup/data/*_backup_*.sql.gz # Pattern expanded locally (fails - files don't exist locally) Solution: oc exec pod -- sh -c 'ls /backup/data/*_backup_*.sql.gz' # Pattern expanded remotely (works - files exist in pod) Changes: - Wrap ls commands with sh -c to ensure glob expansion in pod - List backup files: sh -c 'ls -1 /backup/data/*_backup_*.sql.gz | grep -v grants' - Verify matched files: sh -c "ls -1 ${RESTORE_PATTERN}" - Apply to both playbook and helper script This fixes the "No backup files found" error when files actually exist. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
1 parent 71a0883 commit 423a3f5

2 files changed

Lines changed: 60 additions & 30 deletions

File tree

docs/dev/playbooks/restore-openstack-ctlplane.yaml

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,26 +1034,36 @@
10341034

10351035
- name: Wait for GaleraRestore pods to be ready
10361036
ansible.builtin.shell: |
1037-
RESTORE_NAME="{{ item }}restore"
1037+
BACKUP_NAME="{{ item }}"
1038+
RESTORE_NAME="${BACKUP_NAME}restore"
10381039
10391040
echo "Waiting for GaleraRestore pod: ${RESTORE_NAME}"
10401041
1041-
# Wait for pod to exist (find by label selector)
1042-
for i in {1..60}; do
1043-
POD_NAME=$(oc get pod -n {{ openstack_namespace }} \
1044-
-l cr=${RESTORE_NAME} \
1045-
-o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
1042+
# Get backupSource from GaleraRestore CR spec
1043+
BACKUP_SOURCE=$(oc get galerarestore "${RESTORE_NAME}" -n {{ openstack_namespace }} \
1044+
-o jsonpath='{.spec.backupSource}' 2>/dev/null || echo "")
1045+
1046+
if [ -z "${BACKUP_SOURCE}" ]; then
1047+
echo "ERROR: Could not get backupSource from GaleraRestore ${RESTORE_NAME}"
1048+
exit 1
1049+
fi
1050+
1051+
# Construct pod name: <backupSource>-restore-<galerarestore-name>
1052+
POD_NAME="${BACKUP_SOURCE}-restore-${RESTORE_NAME}"
1053+
echo "Expected pod name: ${POD_NAME}"
10461054
1047-
if [ -n "${POD_NAME}" ]; then
1055+
# Wait for pod to exist
1056+
for i in {1..60}; do
1057+
if oc get pod "${POD_NAME}" -n {{ openstack_namespace }} &>/dev/null; then
10481058
echo "Pod found: ${POD_NAME}"
10491059
break
10501060
fi
10511061
echo "Waiting for pod to be created... ($i/60)"
10521062
sleep 5
10531063
done
10541064
1055-
if [ -z "${POD_NAME}" ]; then
1056-
echo "ERROR: Pod for GaleraRestore ${RESTORE_NAME} not found after 5 minutes"
1065+
if ! oc get pod "${POD_NAME}" -n {{ openstack_namespace }} &>/dev/null; then
1066+
echo "ERROR: Pod ${POD_NAME} not found after 5 minutes"
10571067
exit 1
10581068
fi
10591069
@@ -1132,19 +1142,25 @@
11321142
echo "Processing restore for backup: ${BACKUP_NAME}"
11331143
echo "Restore CR: ${RESTORE_NAME}"
11341144
1135-
# Discover pod name using label selector
1136-
POD_NAME=$(oc get pod -n {{ openstack_namespace }} \
1137-
-l cr=${RESTORE_NAME} \
1138-
-o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
1145+
# Get backupSource from GaleraRestore CR spec
1146+
BACKUP_SOURCE=$(oc get galerarestore "${RESTORE_NAME}" -n {{ openstack_namespace }} \
1147+
-o jsonpath='{.spec.backupSource}' 2>/dev/null || echo "")
11391148
1140-
if [ -z "${POD_NAME}" ]; then
1141-
echo "ERROR: Restore pod not found for GaleraRestore: ${RESTORE_NAME}"
1149+
if [ -z "${BACKUP_SOURCE}" ]; then
1150+
echo "ERROR: Could not get backupSource from GaleraRestore ${RESTORE_NAME}"
11421151
exit 1
11431152
fi
11441153
1145-
echo "Found pod: ${POD_NAME}"
1154+
# Construct pod name: <backupSource>-restore-<galerarestore-name>
1155+
POD_NAME="${BACKUP_SOURCE}-restore-${RESTORE_NAME}"
1156+
echo "Pod name: ${POD_NAME}"
1157+
1158+
# Verify pod exists and is running
1159+
if ! oc get pod "${POD_NAME}" -n {{ openstack_namespace }} &>/dev/null; then
1160+
echo "ERROR: Restore pod not found: ${POD_NAME}"
1161+
exit 1
1162+
fi
11461163
1147-
# Verify pod is running
11481164
POD_PHASE=$(oc get pod "${POD_NAME}" -n {{ openstack_namespace }} -o jsonpath='{.status.phase}')
11491165
if [ "$POD_PHASE" != "Running" ]; then
11501166
echo "ERROR: Restore pod is not running (phase: ${POD_PHASE})"
@@ -1154,7 +1170,8 @@
11541170
echo "Pod is running, finding latest backup..."
11551171
11561172
# List backup files (excluding grants)
1157-
BACKUP_FILES=$(oc exec -n {{ openstack_namespace }} "${POD_NAME}" -- ls -1 /backup/data/*_backup_*.sql.gz 2>/dev/null | grep -v grants || true)
1173+
# Use sh -c to ensure glob expansion happens in the pod
1174+
BACKUP_FILES=$(oc exec -n {{ openstack_namespace }} "${POD_NAME}" -- sh -c 'ls -1 /backup/data/*_backup_*.sql.gz 2>/dev/null | grep -v grants' || true)
11581175
11591176
if [ -z "${BACKUP_FILES}" ]; then
11601177
echo "ERROR: No backup files found in /backup/data/"
@@ -1180,7 +1197,8 @@
11801197
echo "Restore pattern: ${RESTORE_PATTERN}"
11811198
11821199
# Verify files exist
1183-
MATCHED_FILES=$(oc exec -n {{ openstack_namespace }} "${POD_NAME}" -- ls -1 "${RESTORE_PATTERN}" 2>/dev/null || true)
1200+
# Use sh -c to ensure glob expansion happens in the pod
1201+
MATCHED_FILES=$(oc exec -n {{ openstack_namespace }} "${POD_NAME}" -- sh -c "ls -1 ${RESTORE_PATTERN} 2>/dev/null" || true)
11841202
11851203
if [ -z "${MATCHED_FILES}" ]; then
11861204
echo "ERROR: No files match pattern: ${RESTORE_PATTERN}"

docs/dev/scripts/restore-galera-latest.sh

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,28 @@ print_header "Restore Galera Database from Latest Backup"
7070
echo "GaleraRestore CR: $RESTORE_NAME"
7171
echo "Namespace: $OPENSTACK_NAMESPACE"
7272

73-
# Discover pod name using label selector
74-
print_info "Discovering restore pod using label selector..."
75-
POD_NAME=$(oc get pod -n "$OPENSTACK_NAMESPACE" \
76-
-l cr="${RESTORE_NAME}" \
77-
-o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
78-
79-
if [ -z "$POD_NAME" ]; then
80-
print_error "Restore pod not found for GaleraRestore: $RESTORE_NAME"
73+
# Get backupSource from GaleraRestore CR spec
74+
print_info "Reading GaleraRestore CR to get backupSource..."
75+
BACKUP_SOURCE=$(oc get galerarestore "$RESTORE_NAME" -n "$OPENSTACK_NAMESPACE" \
76+
-o jsonpath='{.spec.backupSource}' 2>/dev/null || echo "")
77+
78+
if [ -z "$BACKUP_SOURCE" ]; then
79+
print_error "Could not get backupSource from GaleraRestore: $RESTORE_NAME"
80+
print_info "Make sure the GaleraRestore CR exists"
81+
exit 1
82+
fi
83+
84+
print_info "Backup source: $BACKUP_SOURCE"
85+
86+
# Construct pod name: <backupSource>-restore-<galerarestore-name>
87+
POD_NAME="${BACKUP_SOURCE}-restore-${RESTORE_NAME}"
88+
print_info "Expected pod name: $POD_NAME"
89+
90+
# Check if pod exists
91+
print_info "Checking if restore pod exists..."
92+
if ! oc get pod "$POD_NAME" -n "$OPENSTACK_NAMESPACE" &>/dev/null; then
93+
print_error "Restore pod not found: $POD_NAME"
8194
print_info "Make sure the GaleraRestore CR has been created and the pod is running"
82-
print_info "Expected label: cr=${RESTORE_NAME}"
8395
exit 1
8496
fi
8597

@@ -99,7 +111,7 @@ print_success "Restore pod is running"
99111
print_header "Finding Latest Backup"
100112
print_info "Listing backup files in /backup/data/..."
101113

102-
BACKUP_FILES=$(oc exec -n "$OPENSTACK_NAMESPACE" "$POD_NAME" -- ls -1 /backup/data/*_backup_*.sql.gz 2>/dev/null | grep -v grants || true)
114+
BACKUP_FILES=$(oc exec -n "$OPENSTACK_NAMESPACE" "$POD_NAME" -- sh -c 'ls -1 /backup/data/*_backup_*.sql.gz 2>/dev/null | grep -v grants' || true)
103115

104116
if [ -z "$BACKUP_FILES" ]; then
105117
print_error "No backup files found in /backup/data/"
@@ -137,7 +149,7 @@ print_info "Restore pattern: $RESTORE_PATTERN"
137149
# Verify both backup and grants files exist
138150
echo ""
139151
print_info "Verifying backup files exist..."
140-
MATCHED_FILES=$(oc exec -n "$OPENSTACK_NAMESPACE" "$POD_NAME" -- ls -1 "$RESTORE_PATTERN" 2>/dev/null || true)
152+
MATCHED_FILES=$(oc exec -n "$OPENSTACK_NAMESPACE" "$POD_NAME" -- sh -c "ls -1 $RESTORE_PATTERN 2>/dev/null" || true)
141153

142154
if [ -z "$MATCHED_FILES" ]; then
143155
print_error "No files match pattern: $RESTORE_PATTERN"

0 commit comments

Comments
 (0)