初始化

This commit is contained in:
2026-06-25 13:34:41 +08:00
parent b53df728ce
commit 635248ece7
4 changed files with 13 additions and 84 deletions
-37
View File
@@ -1,37 +0,0 @@
#!/bin/bash
set -e
# PostgreSQL standby bootstrap entrypoint
# If data dir is empty, pull base backup from primary, then hand off to postgres entrypoint
PRIMARY_HOST="${PG_PRIMARY_HOST:-host1}"
PRIMARY_PORT="${PG_PRIMARY_PORT:-5432}"
REPL_PASSWORD="${REPL_PASSWORD:-Bjang09@686-Repl}"
PGDATA="/var/lib/postgresql/data"
if [ ! -s "$PGDATA/PG_VERSION" ]; then
echo "[standby] Data directory empty, bootstrapping from primary $PRIMARY_HOST:$PRIMARY_PORT"
# Wait for primary to accept connections
for i in $(seq 1 30); do
if PGPASSWORD="$REPL_PASSWORD" pg_isready -h "$PRIMARY_HOST" -p "$PRIMARY_PORT" -U replicator >/dev/null 2>&1; then
break
fi
echo "[standby] Waiting for primary to be ready (attempt $i/30)..."
sleep 2
done
# Pull base backup from primary
echo "[standby] Running pg_basebackup..."
PGPASSWORD="$REPL_PASSWORD" pg_basebackup -h "$PRIMARY_HOST" -p "$PRIMARY_PORT" \
-U replicator -D "$PGDATA" -P -R \
-S replication_slot_slave --checkpoint=fast --wal-method=stream
# Ensure standby signal file exists
touch "$PGDATA/standby.signal"
echo "[standby] Bootstrap complete, starting standby..."
fi
# Hand off to the standard postgres entrypoint
exec /usr/local/bin/docker-entrypoint.sh "$@"
+13 -5
View File
@@ -28,7 +28,6 @@ services:
- ../data/postgres:/var/lib/postgresql/data
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
- ./sql/init-replication.sql:/docker-entrypoint-initdb.d/init-replication.sql
- ./sql/init-pg-hba.sh:/docker-entrypoint-initdb.d/init-pg-hba.sh
command: ["postgres", "-c", "wal_level=replica", "-c", "max_wal_senders=10", "-c", "max_replication_slots=10", "-c", "hot_standby=on"]
networks:
- redfuture-net
@@ -48,7 +47,6 @@ services:
condition: service_healthy
volumes:
- /data/pgsql/backup:/backup
- ./scripts/postgres-backup.sh:/usr/local/bin/backup.sh
environment:
PGUSER: postgres
PGPASSWORD: Bjang09@686^*^
@@ -56,9 +54,19 @@ services:
PGPORT: 5432
BACKUP_DIR: /backup
RETENTION_DAYS: 7
command: sh /usr/local/bin/backup.sh
networks:
- redfuture-net
entrypoint: ["/bin/sh", "-c"]
command:
- |
set -e
mkdir -p "/backup"
while true; do
f="backup_$(date +%Y%m%d_%H%M%S).sql.gz"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Starting backup: $${f}"
pg_dumpall -h postgres -U postgres | gzip > "/backup/$${f}"
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Backup complete"
find /backup -name "backup_*.sql.gz" -mtime +7 -delete 2>/dev/null || true
sleep 86400
done
# ---- Redis ----
redis:
-39
View File
@@ -1,39 +0,0 @@
#!/bin/sh
set -e
CREDENTIALS="-h ${PGHOST:-postgres} -U ${PGUSER:-postgres}"
BACKUP_DIR=${BACKUP_DIR:-/backup}
RETENTION_DAYS=${RETENTION_DAYS:-7}
# 自动创建备份目录
mkdir -p "$BACKUP_DIR"
backup() {
local filename="backup_$(date +%Y%m%d_%H%M%S).sql.gz"
echo "[$(date +%Y-%m-%d\ %H:%M:%S)] Starting backup: $filename"
pg_dumpall $CREDENTIALS | gzip > "$BACKUP_DIR/$filename"
echo "[$(date +%Y-%m-%d\ %H:%M:%S)] Backup complete: $(du -h "$BACKUP_DIR/$filename" | cut -f1)"
}
cleanup() {
local count=$(find "$BACKUP_DIR" -name "backup_*.sql.gz" -mtime +$RETENTION_DAYS | wc -l)
if [ "$count" -gt 0 ]; then
find "$BACKUP_DIR" -name "backup_*.sql.gz" -mtime +$RETENTION_DAYS -delete
echo "[$(date +%Y-%m-%d\ %H:%M:%S)] Cleaned up $count backup(s) older than $RETENTION_DAYS days"
fi
}
# Run immediately on startup
backup
cleanup
# Schedule: calculate seconds until next 2:00 AM, then run every 24h
while true; do
now=$(date +%s)
target=$(date -d "tomorrow 02:00" +%s)
sleep_seconds=$((target - now))
echo "[$(date +%Y-%m-%d\ %H:%M:%S)] Next backup scheduled at $(date -d @$target '+%Y-%m-%d %H:%M:%S')"
sleep $sleep_seconds
backup
cleanup
done
-3
View File
@@ -1,3 +0,0 @@
#!/bin/bash
# 添加 pg_hba 复制访问规则(在 docker-entrypoint 初始化后执行)
echo "host replication all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"