初始化
This commit is contained in:
@@ -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 "$@"
|
|
||||||
@@ -28,7 +28,6 @@ services:
|
|||||||
- ../data/postgres:/var/lib/postgresql/data
|
- ../data/postgres:/var/lib/postgresql/data
|
||||||
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
|
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||||
- ./sql/init-replication.sql:/docker-entrypoint-initdb.d/init-replication.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"]
|
command: ["postgres", "-c", "wal_level=replica", "-c", "max_wal_senders=10", "-c", "max_replication_slots=10", "-c", "hot_standby=on"]
|
||||||
networks:
|
networks:
|
||||||
- redfuture-net
|
- redfuture-net
|
||||||
@@ -48,7 +47,6 @@ services:
|
|||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
volumes:
|
volumes:
|
||||||
- /data/pgsql/backup:/backup
|
- /data/pgsql/backup:/backup
|
||||||
- ./scripts/postgres-backup.sh:/usr/local/bin/backup.sh
|
|
||||||
environment:
|
environment:
|
||||||
PGUSER: postgres
|
PGUSER: postgres
|
||||||
PGPASSWORD: Bjang09@686^*^
|
PGPASSWORD: Bjang09@686^*^
|
||||||
@@ -56,9 +54,19 @@ services:
|
|||||||
PGPORT: 5432
|
PGPORT: 5432
|
||||||
BACKUP_DIR: /backup
|
BACKUP_DIR: /backup
|
||||||
RETENTION_DAYS: 7
|
RETENTION_DAYS: 7
|
||||||
command: sh /usr/local/bin/backup.sh
|
entrypoint: ["/bin/sh", "-c"]
|
||||||
networks:
|
command:
|
||||||
- redfuture-net
|
- |
|
||||||
|
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 ----
|
||||||
redis:
|
redis:
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
# 添加 pg_hba 复制访问规则(在 docker-entrypoint 初始化后执行)
|
|
||||||
echo "host replication all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
|
|
||||||
Reference in New Issue
Block a user