refactor(update scripts): enhance directory handling and error management
Some checks failed
Deploy to production / deploy (push) Failing after 1m46s
Some checks failed
Deploy to production / deploy (push) Failing after 1m46s
- Updated update-backend.sh and update-frontend.sh to accept a target directory as a parameter, improving flexibility. - Refactored paths to use the target directory for all operations, ensuring consistency and reducing hardcoded values. - Added error handling with 'set -euo pipefail' for better script reliability. - Improved user feedback by displaying the target directory during updates.
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
#!/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
# Optionaler Parameter für STAGE (Standard: production)
|
||||
STAGE=${1:-production}
|
||||
TARGET_DIR="${1:?Zielverzeichnis fehlt: ./update-backend.sh <z. B. /opt/yourpart-green> [STAGE]}"
|
||||
# Zweiter Parameter: STAGE für DB-Sync (früher erster Parameter)
|
||||
STAGE="${2:-production}"
|
||||
BACKEND_TARGET="$TARGET_DIR/backend"
|
||||
|
||||
echo "Updating YourPart Backend..."
|
||||
echo "Ziel: $BACKEND_TARGET"
|
||||
echo "Using STAGE: $STAGE"
|
||||
echo "NOTE: .env files will NOT be overwritten"
|
||||
|
||||
@@ -20,45 +24,45 @@ rm -rf tmp/
|
||||
echo "Installing dependencies..."
|
||||
npm ci --production
|
||||
|
||||
# Sichere .env-Dateien aus /opt/yourpart/backend temporär
|
||||
# Sichere .env-Dateien aus dem Ziel-Backend temporär
|
||||
TEMP_ENV_DIR="/tmp/yourpart-env-backup-$$"
|
||||
echo "Sichere .env-Dateien aus /opt/yourpart/backend..."
|
||||
echo "Sichere .env-Dateien aus $BACKEND_TARGET..."
|
||||
sudo mkdir -p "$TEMP_ENV_DIR"
|
||||
if [ -d /opt/yourpart/backend ]; then
|
||||
sudo find /opt/yourpart/backend -maxdepth 1 -name '.env*' -type f -exec cp {} "$TEMP_ENV_DIR/" \; 2>/dev/null || true
|
||||
if [ -d "$BACKEND_TARGET" ]; then
|
||||
sudo find "$BACKEND_TARGET" -maxdepth 1 -name '.env*' -type f -exec cp {} "$TEMP_ENV_DIR/" \; 2>/dev/null || true
|
||||
if [ "$(ls -A $TEMP_ENV_DIR 2>/dev/null)" ]; then
|
||||
echo "✓ .env-Dateien gesichert: $(ls $TEMP_ENV_DIR)"
|
||||
else
|
||||
echo "⚠ Keine .env-Dateien in /opt/yourpart/backend gefunden"
|
||||
echo "⚠ Keine .env-Dateien in $BACKEND_TARGET gefunden"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Altes Backend löschen
|
||||
echo "Lösche altes Backend..."
|
||||
sudo rm -rf /opt/yourpart/backend
|
||||
sudo rm -rf "$BACKEND_TARGET"
|
||||
|
||||
# Erstelle Backend-Verzeichnis
|
||||
echo "Erstelle Backend-Verzeichnis..."
|
||||
sudo mkdir -p /opt/yourpart/backend
|
||||
sudo mkdir -p "$BACKEND_TARGET"
|
||||
sudo mkdir -p /opt/yourpart-data/adult-verification
|
||||
|
||||
# Kopiere neues Backend (ohne .env-Dateien aus dem Quellverzeichnis)
|
||||
echo "Kopiere neues Backend..."
|
||||
if command -v rsync &> /dev/null; then
|
||||
sudo rsync -av --exclude='.env*' --exclude='node_modules' . /opt/yourpart/backend/ 2>/dev/null
|
||||
sudo rsync -av --exclude='.env*' --exclude='node_modules' . "$BACKEND_TARGET/" 2>/dev/null
|
||||
if [ -d node_modules ]; then
|
||||
sudo rsync -av --exclude='.env*' node_modules/ /opt/yourpart/backend/node_modules/ 2>/dev/null || \
|
||||
sudo cp -r node_modules /opt/yourpart/backend/ 2>/dev/null
|
||||
sudo rsync -av --exclude='.env*' node_modules/ "$BACKEND_TARGET/node_modules/" 2>/dev/null || \
|
||||
sudo cp -r node_modules "$BACKEND_TARGET/" 2>/dev/null
|
||||
fi
|
||||
else
|
||||
# Fallback: Kopiere alles außer .env
|
||||
sudo cp -r * /opt/yourpart/backend/ 2>/dev/null
|
||||
sudo cp -r * "$BACKEND_TARGET/" 2>/dev/null
|
||||
fi
|
||||
|
||||
# Stelle .env-Dateien wieder her
|
||||
echo "Stelle .env-Dateien wieder her..."
|
||||
if [ -d "$TEMP_ENV_DIR" ] && [ "$(ls -A $TEMP_ENV_DIR 2>/dev/null)" ]; then
|
||||
sudo cp "$TEMP_ENV_DIR"/.env* /opt/yourpart/backend/ 2>/dev/null || true
|
||||
sudo cp "$TEMP_ENV_DIR"/.env* "$BACKEND_TARGET/" 2>/dev/null || true
|
||||
echo "✓ .env-Dateien wiederhergestellt"
|
||||
else
|
||||
echo "⚠ Keine .env-Dateien zum Wiederherstellen vorhanden"
|
||||
@@ -66,22 +70,22 @@ fi
|
||||
sudo rm -rf "$TEMP_ENV_DIR"
|
||||
|
||||
# Prüfe ob .env vorhanden ist
|
||||
if [ -f /opt/yourpart/backend/.env ]; then
|
||||
if [ -f "$BACKEND_TARGET/.env" ]; then
|
||||
echo "✓ .env-Datei ist vorhanden"
|
||||
else
|
||||
echo "⚠ WARNUNG: Keine .env-Datei in /opt/yourpart/backend gefunden!"
|
||||
echo " Bitte manuell erstellen: sudo nano /opt/yourpart/backend/.env"
|
||||
echo "⚠ WARNUNG: Keine .env-Datei in $BACKEND_TARGET gefunden!"
|
||||
echo " Bitte manuell erstellen: sudo nano $BACKEND_TARGET/.env"
|
||||
fi
|
||||
|
||||
# Berechtigungen setzen
|
||||
echo "Setting permissions..."
|
||||
sudo chown -R yourpart:yourpart /opt/yourpart/backend
|
||||
sudo chmod -R 755 /opt/yourpart/backend
|
||||
sudo chown -R yourpart:yourpart "$BACKEND_TARGET"
|
||||
sudo chmod -R 755 "$BACKEND_TARGET"
|
||||
sudo chown -R yourpart:yourpart /opt/yourpart-data
|
||||
sudo chmod -R 755 /opt/yourpart-data
|
||||
# Stelle sicher, dass .env-Dateien die richtigen Berechtigungen haben
|
||||
if [ -f /opt/yourpart/backend/.env ]; then
|
||||
sudo chmod 600 /opt/yourpart/backend/.env
|
||||
if [ -f "$BACKEND_TARGET/.env" ]; then
|
||||
sudo chmod 600 "$BACKEND_TARGET/.env"
|
||||
fi
|
||||
|
||||
# Systemd-Service aktualisieren und neu laden
|
||||
@@ -91,7 +95,7 @@ sudo systemctl daemon-reload
|
||||
|
||||
# Datenbank-Synchronisation durchführen
|
||||
echo "Running database synchronization..."
|
||||
cd /opt/yourpart/backend
|
||||
cd "$BACKEND_TARGET"
|
||||
|
||||
# STAGE für Schema-Updates verwenden
|
||||
echo "Running database sync with STAGE=$STAGE..."
|
||||
|
||||
Reference in New Issue
Block a user