fixed the logic and errors
All checks were successful
Build and Push Docker Image / build (push) Successful in 26s
All checks were successful
Build and Push Docker Image / build (push) Successful in 26s
This commit is contained in:
90
backup.sh
90
backup.sh
@@ -1,50 +1,74 @@
|
|||||||
#!/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=$(
|
||||||
for pattern in $KNOWN_IMAGES; do
|
docker ps --format "{{.ID}} {{.Image}}" | while read -r ID IMAGE; do
|
||||||
case "$IMAGE" in
|
for pattern in $KNOWN_IMAGES; do
|
||||||
*"$pattern"*)
|
case "$IMAGE" in
|
||||||
echo "$ID"
|
*"$pattern"*)
|
||||||
break
|
echo "$ID"
|
||||||
;;
|
break
|
||||||
esac
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
done
|
done
|
||||||
done) do
|
)
|
||||||
NAME=$(docker inspect --format '{{.Name}}' "$container" | sed 's#^/##')
|
|
||||||
mkdir -p "$BACKUP_DIR}/${NAME}"
|
|
||||||
FILE="$BACKUP_DIR/${NAME}/${TIMESTAMP}.sql"
|
|
||||||
|
|
||||||
echo "[INFO] Backing up container: $NAME ($container)"
|
if [ -z "$MATCHING_CONTAINERS" ]; then
|
||||||
if docker exec "$container" pg_dumpall -U postgres >"$FILE" 2>/tmp/pg_backup_error.log; then
|
echo "[WARN] No Postgres containers found."
|
||||||
echo "[SUCCESS] Backup complete for $NAME -> $FILE"
|
else
|
||||||
else
|
for container in $MATCHING_CONTAINERS; do
|
||||||
echo "[ERROR] Backup failed for $NAME (check /tmp/pg_backup_error.log)"
|
NAME=$(docker inspect --format '{{.Name}}' "$container" | sed 's#^/##')
|
||||||
fi
|
CONTAINER_BACKUP_DIR="$BACKUP_DIR/$NAME"
|
||||||
done
|
FILE="$CONTAINER_BACKUP_DIR/${TIMESTAMP}.sql"
|
||||||
|
|
||||||
echo "[INFO] All backups done."
|
mkdir -p "$CONTAINER_BACKUP_DIR"
|
||||||
echo "[INFO] Sleeping for ${INTERVAL_HOURS} hours..."
|
echo "[INFO] Backing up container: $NAME ($container)"
|
||||||
|
|
||||||
|
# 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"
|
||||||
|
elif docker exec "$container" pg_dumpall >"$FILE" 2>/tmp/pg_backup_error.log; then
|
||||||
|
echo "[SUCCESS] Backup complete for $NAME (fallback user) -> $FILE"
|
||||||
|
else
|
||||||
|
echo "[ERROR] Backup failed for $NAME (check /tmp/pg_backup_error.log)"
|
||||||
|
rm -f "$FILE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Retention cleanup
|
||||||
|
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
|
||||||
|
|||||||
Reference in New Issue
Block a user