fixed the logic and errors
All checks were successful
Build and Push Docker Image / build (push) Successful in 26s

This commit is contained in:
2025-10-17 21:55:31 -07:00
parent a2282042ee
commit b362f72859

View File

@@ -1,28 +1,36 @@
#!/bin/sh #!/bin/sh
# === Postgres Auto Backup Script === # === Postgres Auto Backup Script ===
# Description: Finds all Postgres containers and runs pg_dumpall. # Description: Detects Postgres containers by known image names and runs pg_dumpall.
# Author: Calahil Studios # Author: Calahil Studios
# === CONFIGURATION === # === CONFIGURATION ===
BACKUP_DIR="/backups/postgres" BACKUP_DIR="/backups/postgres"
KNOWN_IMAGES=" INTERVAL_HOURS="${INTERVAL_HOURS:-24}" # Default to 24 hours if not set
postgres:17.0-alpine \ RETENTION_DAYS="${RETENTION_DAYS:-7}" # Keep 7 days of backups
postgres:17 \
postgres \ # List of known image name patterns
lscr.io/linuxserver/postgres \ KNOWN_IMAGES=$(
postgres:14.0-alpine \ cat <<'EOF'
ghcr.io/immich-app/postgres:14-vectorchord0.4.3-pgvectors0.2.0 postgres:17.0-alpine
" postgres:17
postgres
lscr.io/linuxserver/postgres
postgres:14.0-alpine
ghcr.io/immich-app/postgres:14-vectorchord0.4.3-pgvectors0.2.0
EOF
)
echo "[INFO] Starting PostgreSQL backup service..." echo "[INFO] Starting PostgreSQL backup service..."
mkdir -p "$BACKUP_DIR" mkdir -p "$BACKUP_DIR"
while true; do while true; do
TIMESTAMP=$(date +'%Y-%m-%d_%H-%M-%S') TIMESTAMP=$(date +'%Y-%m-%d_%H-%M-%S')
echo "[INFO] Starting backup at $TIMESTAMP" echo "[INFO] $(date) - Starting backup cycle ($TIMESTAMP)"
echo "[INFO] Checking for Postgres containers..." echo "[INFO] Checking for running Postgres containers..."
# === FIND MATCHING CONTAINERS === # Find running containers matching known image names
for container in $(docker ps --format "{{.ID}} {{.Image}}" | while read -r ID IMAGE; do MATCHING_CONTAINERS=$(
docker ps --format "{{.ID}} {{.Image}}" | while read -r ID IMAGE; do
for pattern in $KNOWN_IMAGES; do for pattern in $KNOWN_IMAGES; do
case "$IMAGE" in case "$IMAGE" in
*"$pattern"*) *"$pattern"*)
@@ -31,20 +39,36 @@ while true; do
;; ;;
esac esac
done done
done) do done
NAME=$(docker inspect --format '{{.Name}}' "$container" | sed 's#^/##') )
mkdir -p "$BACKUP_DIR}/${NAME}"
FILE="$BACKUP_DIR/${NAME}/${TIMESTAMP}.sql"
if [ -z "$MATCHING_CONTAINERS" ]; then
echo "[WARN] No Postgres containers found."
else
for container in $MATCHING_CONTAINERS; do
NAME=$(docker inspect --format '{{.Name}}' "$container" | sed 's#^/##')
CONTAINER_BACKUP_DIR="$BACKUP_DIR/$NAME"
FILE="$CONTAINER_BACKUP_DIR/${TIMESTAMP}.sql"
mkdir -p "$CONTAINER_BACKUP_DIR"
echo "[INFO] Backing up container: $NAME ($container)" echo "[INFO] Backing up container: $NAME ($container)"
if docker exec "$container" pg_dumpall -U postgres >"$FILE" 2>/tmp/pg_backup_error.log; then
# Try to dump as postgres, fallback to root
if docker exec -u postgres "$container" pg_dumpall >"$FILE" 2>/tmp/pg_backup_error.log; then
echo "[SUCCESS] Backup complete for $NAME -> $FILE" echo "[SUCCESS] Backup complete for $NAME -> $FILE"
elif docker exec "$container" pg_dumpall >"$FILE" 2>/tmp/pg_backup_error.log; then
echo "[SUCCESS] Backup complete for $NAME (fallback user) -> $FILE"
else else
echo "[ERROR] Backup failed for $NAME (check /tmp/pg_backup_error.log)" echo "[ERROR] Backup failed for $NAME (check /tmp/pg_backup_error.log)"
rm -f "$FILE"
fi fi
done
echo "[INFO] All backups done." # Retention cleanup
echo "[INFO] Sleeping for ${INTERVAL_HOURS} hours..." find "$CONTAINER_BACKUP_DIR" -type f -mtime +$RETENTION_DAYS -name '*.sql' -delete
done
fi
echo "[INFO] Backup cycle complete."
echo "[INFO] Sleeping for ${INTERVAL_HOURS}h..."
sleep "${INTERVAL_HOURS}h" sleep "${INTERVAL_HOURS}h"
done done