feat(docker): PostgreSQL 每日自动备份

- 新增 backup.sh: pg_dump + gzip,自动清理过期备份
- production compose 添加 backup 服务: cron 每日 02:00 执行
- 可通过 BACKUP_CRON / BACKUP_KEEP_DAYS 环境变量自定义
This commit is contained in:
iven
2026-05-11 10:27:38 +08:00
parent 533a2b6a8e
commit fc30702846
2 changed files with 122 additions and 0 deletions

View File

@@ -0,0 +1,79 @@
# 生产环境 Docker Compose 配置
# 使用方式: docker compose -f docker/docker-compose.yml -f docker/docker-compose.production.yml up -d
services:
app:
build:
context: ..
dockerfile: Dockerfile
container_name: hms-server
restart: unless-stopped
ports:
- "${APP_PORT:-3000}:3000"
- "${METRICS_PORT:-9090}:9090"
env_file:
- .env.production
environment:
ERP__DATABASE__URL: postgres://${POSTGRES_USER:-erp}:${POSTGRES_PASSWORD}@postgres:${POSTGRES_PORT:-5432}/${POSTGRES_DB:-erp}
ERP__REDIS__URL: redis://:${REDIS_PASSWORD}@redis:${REDIS_PORT:-6379}
volumes:
- app-uploads:/app/uploads
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:3000/api/v1/health"]
interval: 30s
timeout: 5s
start_period: 60s
retries: 3
deploy:
resources:
limits:
cpus: "2"
memory: 1024M
reservations:
cpus: "0.5"
memory: 256M
networks:
- hms-internal
# 每日自动备份 — 每天凌晨 02:00 执行 pg_dump保留 7 天
# 手动触发: docker compose -f docker/docker-compose.yml -f docker/docker-compose.production.yml run --rm backup
backup:
image: postgres:16-alpine
container_name: hms-backup
restart: unless-stopped
entrypoint: >
sh -c "
echo '$$BACKUP_CRON /usr/local/bin/backup.sh' > /etc/crontabs/root &&
crond -f -l 2
"
environment:
PGHOST: postgres
PGPORT: "${POSTGRES_PORT:-5432}"
PGUSER: "${POSTGRES_USER:-erp}"
PGDATABASE: "${POSTGRES_DB:-erp}"
BACKUP_DIR: /backups
KEEP_DAYS: "${BACKUP_KEEP_DAYS:-7}"
BACKUP_CRON: "${BACKUP_CRON:-0 2 * * *}"
volumes:
- ./backup.sh:/usr/local/bin/backup.sh:ro
- backup_data:/backups
depends_on:
postgres:
condition: service_healthy
networks:
- hms-internal
volumes:
app-uploads:
driver: local
backup_data:
driver: local
networks:
hms-internal:
driver: bridge