feat: add Nginx to Caddy migration toolkit with scripts and usage guide
This commit is contained in:
113
setup/nginx-to-caddy/extract_nginx_inventory.sh
Executable file
113
setup/nginx-to-caddy/extract_nginx_inventory.sh
Executable file
@@ -0,0 +1,113 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
# shellcheck source=./lib.sh
|
||||
source "$SCRIPT_DIR/lib.sh"
|
||||
|
||||
REMOTE_HOST=""
|
||||
REMOTE_USER=""
|
||||
REMOTE_PORT="22"
|
||||
OUT_DIR="$SCRIPT_DIR/output"
|
||||
AUTO_YES=false
|
||||
USE_SUDO=true
|
||||
|
||||
usage() {
|
||||
cat <<USAGE
|
||||
Usage: $(basename "$0") --host=IP --user=NAME [options]
|
||||
|
||||
Collect Nginx config inventory from a remote host for conversion planning.
|
||||
|
||||
Options:
|
||||
--host=IP Remote host/IP (required)
|
||||
--user=NAME SSH username (required)
|
||||
--port=PORT SSH port (default: 22)
|
||||
--out-dir=PATH Output directory (default: setup/nginx-to-caddy/output)
|
||||
--no-sudo Use nginx commands without sudo
|
||||
--yes, -y Skip confirmation prompt
|
||||
--help, -h Show help
|
||||
|
||||
Outputs:
|
||||
nginx-version.txt
|
||||
nginx-full.conf (nginx -T combined output)
|
||||
etc-nginx.tar.gz (/etc/nginx snapshot tarball)
|
||||
inventory-summary.txt (basic discovered domains/proxy targets)
|
||||
USAGE
|
||||
}
|
||||
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--host=*) REMOTE_HOST="${arg#*=}" ;;
|
||||
--user=*) REMOTE_USER="${arg#*=}" ;;
|
||||
--port=*) REMOTE_PORT="${arg#*=}" ;;
|
||||
--out-dir=*) OUT_DIR="${arg#*=}" ;;
|
||||
--no-sudo) USE_SUDO=false ;;
|
||||
--yes|-y) AUTO_YES=true ;;
|
||||
--help|-h) usage; exit 0 ;;
|
||||
*) log_error "Unknown argument: $arg"; usage; exit 1 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
require_cmd ssh tar awk sed grep
|
||||
|
||||
if [[ -z "$REMOTE_HOST" || -z "$REMOTE_USER" ]]; then
|
||||
log_error "--host and --user are required"
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if ! confirm_action "Collect nginx config from ${REMOTE_USER}@${REMOTE_HOST}?" "$AUTO_YES"; then
|
||||
log_info "Cancelled"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
mkdir -p "$OUT_DIR"
|
||||
|
||||
sudo_prefix=""
|
||||
if [[ "$USE_SUDO" == "true" ]]; then
|
||||
sudo_prefix="sudo "
|
||||
fi
|
||||
|
||||
ssh_target="${REMOTE_USER}@${REMOTE_HOST}"
|
||||
ssh_opts=( -p "$REMOTE_PORT" -o BatchMode=yes -o StrictHostKeyChecking=accept-new )
|
||||
|
||||
log_info "Checking SSH connectivity..."
|
||||
ssh "${ssh_opts[@]}" "$ssh_target" "echo connected" >/dev/null
|
||||
|
||||
log_info "Capturing nginx version and build info..."
|
||||
ssh "${ssh_opts[@]}" "$ssh_target" "${sudo_prefix}nginx -V 2>&1" > "${OUT_DIR}/nginx-version.txt"
|
||||
|
||||
log_info "Capturing full rendered nginx config (nginx -T)..."
|
||||
ssh "${ssh_opts[@]}" "$ssh_target" "${sudo_prefix}nginx -T 2>&1" > "${OUT_DIR}/nginx-full.conf"
|
||||
|
||||
log_info "Capturing /etc/nginx snapshot..."
|
||||
ssh "${ssh_opts[@]}" "$ssh_target" "${sudo_prefix}tar -C / -czf - etc/nginx" > "${OUT_DIR}/etc-nginx.tar.gz"
|
||||
|
||||
log_info "Building inventory summary..."
|
||||
{
|
||||
echo "Inventory generated: $(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
||||
echo
|
||||
echo "Server names:"
|
||||
awk '
|
||||
/^[[:space:]]*server_name[[:space:]]+/ {
|
||||
line=$0
|
||||
sub(/^[[:space:]]*server_name[[:space:]]+/, "", line)
|
||||
sub(/[[:space:]]*;[[:space:]]*$/, "", line)
|
||||
gsub(/[[:space:]]+/, " ", line)
|
||||
print " - " line
|
||||
}
|
||||
' "${OUT_DIR}/nginx-full.conf" | sort -u
|
||||
echo
|
||||
echo "Proxy targets:"
|
||||
awk '
|
||||
/^[[:space:]]*proxy_pass[[:space:]]+/ {
|
||||
line=$0
|
||||
sub(/^[[:space:]]*proxy_pass[[:space:]]+/, "", line)
|
||||
sub(/[[:space:]]*;[[:space:]]*$/, "", line)
|
||||
print " - " line
|
||||
}
|
||||
' "${OUT_DIR}/nginx-full.conf" | sort -u
|
||||
} > "${OUT_DIR}/inventory-summary.txt"
|
||||
|
||||
log_success "Nginx inventory collected in: ${OUT_DIR}"
|
||||
log_info "Next: run nginx_to_caddy.sh --input=${OUT_DIR}/nginx-full.conf"
|
||||
Reference in New Issue
Block a user