scripts/update_containers.sh

54 lines
2.7 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.

#!/bin/bash
# ────────────────────────────────────────────────────────────────────────
# Zweck: Automatisches Prüfen und Anwenden von Updates für alle
# DockerComposeProjekte unter /opt/docker/
# ────────────────────────────────────────────────────────────────────────
set -euo pipefail
IFS=$'\n\t'
# ---------- Konfiguration ----------
BASE_DIR="/opt/docker" # Verzeichnis mit allen ComposeProjekten
LOG_DIR="/var/log/docker-update" # Ort für LogDateien
COMPOSE_CMD="docker-compose" # dockercompose v1
# ---------- Hilfsfunktion ----------
log() {
local lvl=${1:-INFO}
local msg=${2:-}
echo "$(date '+%Y-%m-%d %H:%M:%S') [$lvl] $msg"
}
# ---------- LogVerzeichnis anlegen ----------
mkdir -p "$LOG_DIR"
# ---------- HauptSchleife ----------
for proj_dir in "$BASE_DIR"/*/; do
# ComposeDatei vorhanden?
if [[ -f "$proj_dir/docker-compose.yml" || -f "$proj_dir/docker-compose.yaml" ]]; then
pushd "$proj_dir" >/dev/null
# ───── LogDatei für dieses Projekt ──────────────────────────────────────
# (ohne `local`, weil wir uns nicht in einer Funktion befinden)
log_file="${LOG_DIR}/$(basename "$proj_dir")-update.log"
log "INFO" "Starte Update in ${proj_dir%/}"
echo "$(date '+%Y-%m-%d %H:%M:%S') [INFO] Update in ${proj_dir%/}" >> "$log_file"
# ───── 1. Container herunterfahren ─────────────────────────────────────────────
$COMPOSE_CMD down >> "$log_file" 2>&1 || true
# ───── 2. Images pullen ───────────────────────────────────────────────────────
$COMPOSE_CMD pull >> "$log_file" 2>&1
# ───── 3. Container neu starten ───────────────────────────────────────────────
$COMPOSE_CMD up -d >> "$log_file" 2>&1
popd >/dev/null
log "INFO" "Update für ${proj_dir%/} abgeschlossen."
else
log "INFO" "Keine docker-compose.yml in ${proj_dir%/} überspringen."
fi
done
log "INFO" "Alle Projekte wurden aktualisiert."