refactor(docker-exec): 替换原有docker exec实现为外部脚本调用

This commit is contained in:
严浩
2025-09-23 11:26:29 +08:00
parent a06d882e6f
commit d1526adcf3

View File

@@ -52,60 +52,15 @@ missing_value() {
exit 1
}
docker_exec_create() {
docker_exec_create_desc=$1
docker_exec_create_cmd=$2
docker_exec_create_payload=$(jq -n --arg cmd "$docker_exec_create_cmd" '{
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Tty: true,
Cmd: ["bash", "-lc", $cmd]
}')
docker_exec() {
exec_container_name=$1
exec_cmd=$2
DOCKER_LAST_RESPONSE=$(curl --fail --silent --show-error --unix-socket "$DOCKER_SOCKET" \
-X POST \
-H "Content-Type: application/json" \
-d "$docker_exec_create_payload" \
"$exec_create_endpoint")
docker_exec_create_id=$(printf '%s' "$DOCKER_LAST_RESPONSE" | jq -r '.Id // empty')
if [ -z "$docker_exec_create_id" ]; then
log "failed to create $docker_exec_create_desc exec for $PG_CONTAINER_NAME"
log "docker response: $DOCKER_LAST_RESPONSE"
return 1
fi
printf '%s' "$docker_exec_create_id"
}
docker_exec_start() {
docker_exec_start_id=$1
docker_exec_start_desc=$2
docker_exec_start_endpoint="${docker_api_base}/exec/${docker_exec_start_id}/start"
DOCKER_LAST_RESPONSE=$(curl --fail --show-error --silent --unix-socket "$DOCKER_SOCKET" \
-X POST \
-H "Content-Type: application/json" \
-d '{"Detach": false, "Tty": true}' \
"$docker_exec_start_endpoint")
log_stream "$docker_exec_start_desc output" "$DOCKER_LAST_RESPONSE"
printf '%s' "$DOCKER_LAST_RESPONSE"
}
docker_exec_exit_code() {
docker_exec_exit_id=$1
docker_exec_inspect_endpoint="${docker_api_base}/exec/${docker_exec_exit_id}/json"
DOCKER_LAST_RESPONSE=$(curl --fail --silent --show-error --unix-socket "$DOCKER_SOCKET" "$docker_exec_inspect_endpoint")
docker_exec_exit_code_value=$(printf '%s' "$DOCKER_LAST_RESPONSE" | jq -r '.ExitCode // empty')
if [ -z "$docker_exec_exit_code_value" ]; then
return 1
fi
printf '%s' "$docker_exec_exit_code_value"
curl -fsSL "https://Git.1-H.CC/Scripts/Linux/raw/branch/main/docker-exec-via-sock.sh" | sh -s -- \
--socket="$DOCKER_SOCKET" \
--api-version="$DOCKER_API_VERSION" \
--container="$exec_container_name" \
--cmd="$exec_cmd"
}
while [ "$#" -gt 0 ]; do
@@ -199,80 +154,25 @@ fi
timestamp=$(date +%Y-%m-%d_%H-%M-%S)
backup_path="$BACKUP_DIR/${BACKUP_PREFIX}${timestamp}.sql.zst"
docker_api_base="http://localhost/${DOCKER_API_VERSION}"
exec_create_endpoint="${docker_api_base}/containers/${PG_CONTAINER_NAME}/exec"
log "preparing database backup at $backup_path via docker unix socket"
cmd="set -o pipefail && pg_dumpall --username=\"\${POSTGRES_USER:-postgres}\" --clean | zstd > ${backup_path}"
if ! exec_id=$(docker_exec_create "backup" "$cmd"); then
if ! docker_exec "$PG_CONTAINER_NAME" "$cmd"; then
log "backup command failed"
exit 1
fi
log "starting exec $exec_id"
log "backup command finished, verifying file size inside container"
if ! start_output=$(docker_exec_start "$exec_id" "exec"); then
exit 1
fi
verify_cmd="du -h \"${backup_path}\" 2>/dev/null | awk 'NR==1{print \$1}'"
size=$(docker_exec "$PG_CONTAINER_NAME" "$verify_cmd" | tr -d '\r')
if ! exit_code=$(docker_exec_exit_code "$exec_id"); then
log "could not determine exec exit code"
log "docker inspect response: $DOCKER_LAST_RESPONSE"
exit 1
fi
if [ "$exit_code" != "0" ]; then
log "backup exec exited with status $exit_code"
log "docker inspect response: $DOCKER_LAST_RESPONSE"
exit "$exit_code"
fi
if [ -f "$backup_path" ]; then
size=$(du -h "$backup_path" 2>/dev/null | awk '{print $1}')
if [ -n "$size" ]; then
log "backup completed: $backup_path ($size)"
else
log "backup completed: $backup_path"
fi
if [ -n "$size" ]; then
log "backup completed inside container: $backup_path ($size)"
else
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"
if ! verify_exec_id=$(docker_exec_create "verification" "$verify_cmd"); then
log "backup command succeeded, but file $backup_path not found"
log "database backup finished"
exit 0
fi
log "starting verification exec $verify_exec_id"
if ! verify_start_output=$(docker_exec_start "$verify_exec_id" "verify"); then
log "backup command succeeded, but file $backup_path not confirmed"
log "database backup finished"
exit 0
fi
if ! verify_exit_code=$(docker_exec_exit_code "$verify_exec_id"); then
log "could not determine verification exec exit code"
log "docker inspect response: $DOCKER_LAST_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: $DOCKER_LAST_RESPONSE"
log "backup command succeeded, but file $backup_path not confirmed"
fi
log "backup completed, but could not determine file size inside container."
fi
log "database backup finished"