diff --git a/postgres-dump-zstd-docker-sock.sh b/postgres-dump-zstd-docker-sock.sh index 697d716..c9d4f90 100755 --- a/postgres-dump-zstd-docker-sock.sh +++ b/postgres-dump-zstd-docker-sock.sh @@ -190,7 +190,72 @@ if [ -f "$backup_path" ]; then log "backup completed: $backup_path" fi 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 log "database backup finished"