All checks were successful
Build and Push Docker Image / build (push) Successful in 39s
-switched to user 1000 for security. -added user to docker group -properly mounted btrfs drive on host allows users to create snapshots
69 lines
2.3 KiB
Bash
69 lines
2.3 KiB
Bash
#!/usr/bin/env bash
|
|
# === Postgres Auto Backup Script ===
|
|
# Description: Detects Postgres containers by known image names and runs pg_dumpall.
|
|
# Author: Calahil Studios
|
|
|
|
# === CONFIGURATION ===
|
|
BACKUP_DIR="/backups/postgres_dumps"
|
|
RETENTION_DAYS="${RETENTION_DAYS:-7}" # Keep 7 days of backups
|
|
|
|
# List of known image name patterns
|
|
KNOWN_IMAGES=$(
|
|
cat <<'EOF'
|
|
postgres:17.0-alpine
|
|
postgres:17
|
|
postgres
|
|
postgres:14.0-alpine
|
|
ghcr.io/immich-app/postgres:14-vectorchord0.4.3-pgvectors0.2.0
|
|
EOF
|
|
)
|
|
|
|
echo "[BACKUPBOT_INFO] Starting PostgreSQL backup service..."
|
|
mkdir -p "$BACKUP_DIR"
|
|
|
|
TIMESTAMP=$(date +'%Y-%m-%d_%H-%M-%S')
|
|
echo "[BACKUPBOT_INFO] $(date) - Starting backup cycle ($TIMESTAMP)"
|
|
echo "[BACKUPBOT_INFO] Checking for running Postgres containers..."
|
|
|
|
# Find running containers matching known image names
|
|
MATCHING_CONTAINERS=$(
|
|
docker ps --format "{{.ID}} {{.Image}}" | while read -r ID IMAGE; do
|
|
for pattern in $KNOWN_IMAGES; do
|
|
case "$IMAGE" in
|
|
*"$pattern"*)
|
|
echo "$ID"
|
|
break
|
|
;;
|
|
esac
|
|
done
|
|
done
|
|
)
|
|
|
|
if [ -z "$MATCHING_CONTAINERS" ]; then
|
|
echo "[BACKUPBOT_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 "[BACKUPBOT_INFO] Backing up container: $NAME ($container)" | tee -a "$LOG_FILE"
|
|
PG_USER=$(docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$container" | grep POSTGRES_USER | cut -d= -f2)
|
|
PG_PASS=$(docker inspect --format '{{range .Config.Env}}{{println .}}{{end}}' "$container" | grep POSTGRES_PASSWORD | cut -d= -f2)
|
|
if docker exec -e PGPASSWORD="$PG_PASS" "$container" pg_dumpall -U "$PG_USER" -h 127.0.0.1 >"$FILE" 2>/tmp/pg_backup_error.log; then
|
|
echo "[BACKUPBOT_SUCCESS] Backup complete for $NAME -> $FILE"
|
|
else
|
|
echo "[BACKUPBOT_ERROR] Backup failed for $NAME (check /tmp/pg_backup_error.log)"
|
|
fi
|
|
# Retention cleanup
|
|
find "$CONTAINER_BACKUP_DIR" -type f -mtime +$RETENTION_DAYS -name '*.sql' -delete
|
|
done
|
|
fi
|
|
|
|
echo "[BACKUPBOT_INFO] Creating a snapshot of /srv/appdata"
|
|
btrfs subvolume snapshot -r /source/appdata /backups/snapshots/$(hostname)-$(date +%F)
|
|
|
|
echo "[BACKUPBOT_INFO] Backup cycle complete."
|