feat(postgres-dump): 增强备份文件存在性验证逻辑

This commit is contained in:
严浩
2025-09-22 18:56:11 +08:00
parent 13c6f807e0
commit a71646df7c

View File

@@ -190,7 +190,72 @@ if [ -f "$backup_path" ]; then
log "backup completed: $backup_path" log "backup completed: $backup_path"
fi fi
else else
log "backup command succeeded, but file $backup_path not found" log "backup file not found on host; verifying inside container $PG_CONTAINER_NAME"
verify_cmd="set -eo pipefail && if [ -f \"${backup_path}\" ]; then du -h \"${backup_path}\" 2>/dev/null | awk 'NR==1{print \$1}' || printf 'exists'; else exit 44; fi"
verify_payload=$(jq -n --arg cmd "$verify_cmd" '{
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Tty: true,
Cmd: ["bash", "-lc", $cmd]
}')
verify_create_response=$(curl --fail --silent --show-error --unix-socket "$DOCKER_SOCKET" \
-X POST \
-H "Content-Type: application/json" \
-d "$verify_payload" \
"$exec_create_endpoint")
verify_exec_id=$(printf '%s' "$verify_create_response" | jq -r '.Id // empty')
if [ -z "$verify_exec_id" ]; then
log "failed to create verification exec for $PG_CONTAINER_NAME"
log "docker response: $verify_create_response"
log "backup command succeeded, but file $backup_path not found"
log "database backup finished"
exit 0
fi
log "starting verification exec $verify_exec_id"
verify_start_endpoint="${docker_api_base}/exec/${verify_exec_id}/start"
verify_start_output=$(curl --fail --show-error --silent --unix-socket "$DOCKER_SOCKET" \
-X POST \
-H "Content-Type: application/json" \
-d '{"Detach": false, "Tty": true}' \
"$verify_start_endpoint")
if [ -n "$verify_start_output" ]; then
printf '%s\n' "$verify_start_output" | while IFS= read -r line; do
log "verify output: $line"
done
fi
verify_inspect_endpoint="${docker_api_base}/exec/${verify_exec_id}/json"
verify_inspect_response=$(curl --fail --silent --show-error --unix-socket "$DOCKER_SOCKET" "$verify_inspect_endpoint")
verify_exit_code=$(printf '%s' "$verify_inspect_response" | jq -r '.ExitCode // empty')
if [ -z "$verify_exit_code" ]; then
log "could not determine verification exec exit code"
log "docker inspect response: $verify_inspect_response"
log "backup command succeeded, but file $backup_path not found"
elif [ "$verify_exit_code" = "0" ]; then
verify_size=$(printf '%s' "$verify_start_output" | awk 'NF {last=$0} END {print last}')
verify_size=$(printf '%s' "$verify_size" | tr -d '\r')
if [ -n "$verify_size" ]; then
log "backup completed inside container: $backup_path ($verify_size)"
else
log "backup completed inside container: $backup_path"
fi
elif [ "$verify_exit_code" = "44" ]; then
log "backup command succeeded, but file $backup_path not found inside container $PG_CONTAINER_NAME"
else
log "verification exec exited with status $verify_exit_code"
log "docker inspect response: $verify_inspect_response"
log "backup command succeeded, but file $backup_path not confirmed"
fi
fi fi
log "database backup finished" log "database backup finished"