Files
Simple-Wishlist/deploy.sh
2025-08-18 20:39:36 +02:00

119 lines
3.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# =========================
# Wishlist Deploy (SFTP)
# =========================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SOURCE_DIR="${SOURCE_DIR:-"$SCRIPT_DIR/"}"
TARGET_DIR="${TARGET_DIR:-"/public_html/wishlist.hiabuto.de"}"
SFTP_HOST="${SFTP_HOST:-www374.your-server.de}"
SFTP_USER="${SFTP_USER:-peterksd}"
SSH_KEY="${SSH_KEY:-$HOME/.ssh/id_ed25519}"
DRY_RUN="${DRY_RUN:-0}"
# Glob-Excludes (rein mit Globs, kompatibel zu älteren lftp-Versionen)
EXCLUDE_ARGS=(
# Git-Kram (rekursiv, egal wo)
--exclude-glob ".git"
--exclude-glob ".git/*"
--exclude-glob "*/.git"
--exclude-glob "*/.git/*"
--exclude-glob "**/.git"
--exclude-glob "**/.git/*"
--exclude-glob ".git*"
--exclude-glob "*/.git*"
--exclude-glob "**/.git*"
--exclude-glob ".gitattributes"
--exclude-glob ".gitignore"
--exclude-glob ".github*"
# Secrets/Meta
--exclude-glob ".env*"
--exclude-glob "deploy*.sh"
--exclude-glob "README*"
--exclude-glob "*.md"
--exclude-glob "*.sql"
# Vendor/Node
--exclude-glob "node_modules"
--exclude-glob "node_modules/**"
--exclude-glob "vendor/*/.git*"
# Deine echte Config NICHT überschreiben
--exclude-glob "config/config.php"
# Server-Daten (Bilder) NICHT anfassen
--exclude-glob "data"
--exclude-glob "data/*"
--exclude-glob "data/**"
)
RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'; NC='\033[0m'
echo -e "${GREEN}Starting wishlist deployment...${NC}"
echo -e "${GREEN}SFTP Upload -> ${SFTP_USER}@${SFTP_HOST}${NC}"
echo -e "${GREEN}Target Dir -> ${TARGET_DIR}${NC}"
(( DRY_RUN == 1 )) && echo -e "${YELLOW}Mode -> DRY-RUN${NC}"
need() { command -v "$1" >/dev/null 2>&1 || { echo -e "${RED}Error: '$1' ist nicht installiert.${NC}"; exit 1; }; }
build_connect_program() {
printf "ssh -i %q -o IdentitiesOnly=yes -o PreferredAuthentications=publickey -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no -o NumberOfPasswordPrompts=0 -o BatchMode=yes -o ConnectTimeout=15 -o LogLevel=ERROR" "$SSH_KEY"
}
need lftp
[ -d "$SOURCE_DIR" ] || { echo -e "${RED}Error: SOURCE_DIR existiert nicht: ${SOURCE_DIR}${NC}"; exit 1; }
[ -r "$SSH_KEY" ] || { echo -e "${RED}Error: SSH-Key nicht gefunden/lesbar: ${SSH_KEY}${NC}"; exit 1; }
case "$TARGET_DIR" in
/public_html/*) : ;;
*) echo -e "${RED}TARGET_DIR muss unter /public_html/ liegen (aktuell: ${TARGET_DIR})${NC}"; exit 1;;
esac
echo -e "${YELLOW}>> Prüfe SFTP-Verbindung (key-only)...${NC}"
if ! lftp </dev/null -e "
set cmd:interactive false;
set cmd:fail-exit yes;
set net:max-retries 1;
set net:timeout 15;
set sftp:auto-confirm yes;
set sftp:connect-program '$(build_connect_program)';
open -u ${SFTP_USER}, sftp://${SFTP_HOST};
cls -1 '${TARGET_DIR}';
bye
" >/dev/null 2>&1; then
echo -e "${RED}SFTP-Test fehlgeschlagen.${NC}"
exit 255
fi
echo -e "${YELLOW}>> Upload per SFTP (mirror -R)...${NC}"
MIRROR_OPTS=( -R --delete --verbose --parallel=4 )
(( DRY_RUN == 1 )) && MIRROR_OPTS+=( --dry-run )
(( ${DEBUG:-0} == 1 )) && {
echo "mirror opts: ${MIRROR_OPTS[*]}"
echo "exclude : ${EXCLUDE_ARGS[*]}"
}
lftp -e "
set cmd:interactive false;
set cmd:fail-exit yes;
set net:max-retries 2;
set net:timeout 20;
set sftp:auto-confirm yes;
set sftp:connect-program '$(build_connect_program)';
open -u ${SFTP_USER}, sftp://${SFTP_HOST};
mirror ${MIRROR_OPTS[*]} ${EXCLUDE_ARGS[*]} '${SOURCE_DIR%/}/' '${TARGET_DIR%/}/';
bye
"
if (( DRY_RUN == 1 )); then
echo -e "${GREEN}DRY-RUN erfolgreich (keine Dateien verändert).${NC}"
else
echo -e "${GREEN}SFTP-Upload erfolgreich.${NC}"
fi
echo -e "${GREEN}Deployment completed.${NC}"