Files
app-api/scripts/upload.sh
T
2026-06-11 11:12:19 +08:00

56 lines
1.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -euo pipefail
KEY="$HOME/.ssh/aws_eafon.pem"
REMOTE_DIR="/data/project/app-api/"
SERVER="api1"
if [ $# -gt 0 ] && { [ "$1" = "api1" ] || [ "$1" = "api2" ]; }; then
SERVER="$1"
shift
fi
case "$SERVER" in
api1)
REMOTE_HOST="ubuntu@ec2-18-184-205-87.eu-central-1.compute.amazonaws.com"
;;
api2)
REMOTE_HOST="ubuntu@ec2-3-71-153-45.eu-central-1.compute.amazonaws.com"
;;
*)
echo "未知服务器: $SERVER(可选: api1, api2" >&2
exit 1
;;
esac
REMOTE="${REMOTE_HOST}:${REMOTE_DIR}"
if [ $# -eq 0 ]; then
PATHS=(
./cmd/
./internal/
./pkg
./go.mod
./go.sum
./Dockerfile
./docker-compose.yml
)
else
PATHS=("$@")
fi
echo "上传到 ${SERVER} (${REMOTE_HOST}) ..."
SSH_OPTS="-i $KEY -o StrictHostKeyChecking=no"
# 上传前先删除远程同名目录,避免 scp 嵌套
for item in "${PATHS[@]}"; do
if [[ -d "$item" ]]; then
remote_item="${REMOTE_DIR}${item#./}"
echo " 删除远程目录: ${REMOTE_HOST}:${remote_item}"
ssh $SSH_OPTS "$REMOTE_HOST" "rm -rf '${remote_item}'"
fi
done
scp -i "$KEY" -r "${PATHS[@]}" "$REMOTE"