Files
yourpart3/update-backend.sh
Torsten Schulz (local) b3346d4cac
All checks were successful
Deploy to production / deploy (push) Successful in 4m25s
fix(update-backend): adjust file permissions and handle environment variables safely
- Updated the permission settings for the environment file to 640, ensuring it is readable only by the owner and the deploy group.
- Modified the sequelize configuration to safely handle missing environment variables, converting them to strings or setting them to undefined to prevent runtime errors.
[force-deploy]
2026-04-17 13:53:13 +02:00

83 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
TARGET_DIR="${1:?target dir required}"
STAGE="${2:-production}"
BACKEND_DIR="$TARGET_DIR/backend"
CURRENT_LINK="/opt/yourpart"
CURRENT_BACKEND="$CURRENT_LINK/backend"
DATA_DIR="/opt/yourpart-data"
echo "Updating YourPart Backend..."
echo "Ziel: $BACKEND_DIR"
echo "Using STAGE: $STAGE"
echo "NOTE: .env files will NOT be overwritten"
cd "$BACKEND_DIR"
echo "Lösche alle generierten Verzeichnisse..."
rm -rf node_modules/.cache/
rm -rf logs/
rm -rf tmp/
echo "Installing dependencies..."
npm ci
echo "Übernehme .env-Dateien vom Live-System..."
SOURCE_ENV="/opt/yourpart/backend/.env"
TARGET_ENV="$BACKEND_DIR/.env"
echo "Quelle: $SOURCE_ENV"
echo "Ziel: $TARGET_ENV"
sudo -n ls -l "$SOURCE_ENV"
# 640, Owner yourpart, Gruppe = Deploy-User: npm/db:migrate muss .env lesen (dotenv),
# ohne die Datei weltlesbar zu machen.
DEPLOY_GROUP="$(id -gn)"
sudo -n install -m 640 -o yourpart -g "$DEPLOY_GROUP" "$SOURCE_ENV" "$TARGET_ENV"
echo "Prüfe kopierte Datei..."
ls -l "$TARGET_ENV"
head -n 1 "$TARGET_ENV" >/dev/null
echo "✓ .env-Datei wurde übernommen"
sudo -n mkdir -p "$DATA_DIR/adult-verification"
if [ -f "$BACKEND_DIR/.env" ]; then
echo "✓ .env-Datei ist vorhanden"
else
echo "⚠ WARNUNG: Keine .env-Datei in $BACKEND_DIR gefunden!"
fi
echo "Updating systemd service..."
sudo -n cp "$TARGET_DIR/yourpart.service" /etc/systemd/system/
sudo -n systemctl daemon-reload
echo "Running database migrations..."
cd "$BACKEND_DIR"
export STAGE="$STAGE"
echo "Führe Migrationen aus..."
npm run db:migrate
echo "Entferne Dev-Dependencies nach Migrationen..."
npm prune --omit=dev
if [ "${RUN_LEGACY_SYNC_DB_ON_DEPLOY:-0}" = "1" ]; then
echo "RUN_LEGACY_SYNC_DB_ON_DEPLOY=1 gesetzt -> führe zusätzlich legacy sync-db aus..."
npm run sync-db
else
echo "Legacy sync-db übersprungen (Standard)."
fi
echo "Setting permissions..."
sudo -n chown -R yourpart:yourpart "$BACKEND_DIR"
sudo -n chmod -R 755 "$BACKEND_DIR"
sudo -n chown -R yourpart:yourpart "$DATA_DIR"
sudo -n chmod -R 755 "$DATA_DIR"
echo "Backend update completed!"