feat(database-dump-via-docker-sock): 增加对EXEC_SHELL变量的支持以指定执行shell

This commit is contained in:
严浩
2025-09-23 15:41:01 +08:00
parent 3c688efbb3
commit 4cefdf398f
2 changed files with 25 additions and 4 deletions

View File

@@ -21,7 +21,7 @@ log_stream() {
print_usage() {
cat <<EOF >&2
Usage: $0 [--socket=PATH] [--api-version=VERSION] --container=NAME --cmd=COMMAND [--user=NAME]
Usage: $0 [--socket=PATH] [--api-version=VERSION] --container=NAME --cmd=COMMAND [--user=NAME] [--shell=SHELL]
Options:
--socket=PATH Docker Engine unix socket path (default: /var/run/docker.sock)
@@ -29,7 +29,7 @@ Options:
--container=NAME Container name to execute command in
--cmd=COMMAND Command to execute in the container
--user=NAME Run command as the specified user (default: container default user)
--shell=SHELL Shell to use (default: sh) 暂未实现
--shell=SHELL Shell to use (default: sh)
--help Show this help message
EOF
}
@@ -40,6 +40,7 @@ CONTAINER_NAME=""
CMD_TO_EXEC=""
EXEC_USER=""
DOCKER_LAST_RESPONSE=""
EXEC_SHELL="sh"
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
@@ -57,12 +58,12 @@ missing_value() {
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" --arg user "$EXEC_USER" '{
docker_exec_create_payload=$(jq -n --arg cmd "$docker_exec_create_cmd" --arg user "$EXEC_USER" --arg shell "$EXEC_SHELL" '{
AttachStdin: false,
AttachStdout: true,
AttachStderr: true,
Tty: true,
Cmd: ["sh", "-c", $cmd]
Cmd: [$shell, "-c", $cmd]
} | if ($user | length) > 0 then .User = $user else . end')
DOCKER_LAST_RESPONSE=$(curl --silent --show-error --unix-socket "$DOCKER_SOCKET" \
@@ -151,6 +152,14 @@ while [ "$#" -gt 0 ]; do
shift
EXEC_USER="$1"
;;
--shell=*)
EXEC_SHELL="${1#*=}"
;;
--shell)
if [ "$#" -lt 2 ]; then missing_value '--shell'; fi
shift
EXEC_SHELL="$1"
;;
--help)
print_usage
exit 0
@@ -182,6 +191,10 @@ if [ -z "$DOCKER_SOCKET" ] || [ -z "$DOCKER_API_VERSION" ] || [ -z "$CONTAINER_N
exit 1
fi
if [ -z "$EXEC_SHELL" ]; then
EXEC_SHELL="sh"
fi
if [ ! -S "$DOCKER_SOCKET" ]; then
log "docker socket $DOCKER_SOCKET not found"
exit 1