update backupbot labels

This commit is contained in:
Moritz 2024-10-22 21:42:57 +02:00
parent 6be1ac13b8
commit 27dd971444
4 changed files with 50 additions and 8 deletions

View File

@ -1,6 +1,7 @@
TYPE=rallly TYPE=rallly
TIMEOUT=300 TIMEOUT=300
ENABLE_AUTO_UPDATE=true ENABLE_AUTO_UPDATE=true
ENABLE_BACKUPS=true
DOMAIN=rallly.example.com DOMAIN=rallly.example.com

View File

@ -1 +1,2 @@
export APP_ENTRYPOINT_VERSION=v3 export APP_ENTRYPOINT_VERSION=v3
export PG_BACKUP_VERSION=v1

View File

@ -56,9 +56,10 @@ services:
image: postgres:14.8 image: postgres:14.8
deploy: deploy:
labels: labels:
backupbot.backup: "true" backupbot.backup: "${ENABLE_BACKUPS:-true}"
backupbot.backup.pre-hook: "PGPASSWORD=$$(cat /run/secrets/db_password) pg_dump -U postgres $${POSTGRES_DB} > /var/lib/postgresql/data/backup.sql" backupbot.backup.pre-hook: "/pg_backup.sh backup"
backupbot.backup.post-hook: "rm -rf /var/lib/postgresql/data/backup.sql" backupbot.backup.volumes.db-data.path: "backup.sql"
backupbot.restore.post-hook: '/pg_backup.sh restore'
volumes: volumes:
- db-data:/var/lib/postgresql/data - db-data:/var/lib/postgresql/data
secrets: secrets:
@ -66,6 +67,7 @@ services:
environment: environment:
- POSTGRES_PASSWORD_FILE=/run/secrets/db_password - POSTGRES_PASSWORD_FILE=/run/secrets/db_password
- POSTGRES_DB=db - POSTGRES_DB=db
- POSTGRES_USER=postgres
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"] test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s interval: 5s
@ -73,6 +75,10 @@ services:
retries: 5 retries: 5
networks: networks:
- internal - internal
configs:
- source: pg_backup
target: /pg_backup.sh
mode: 0555
secrets: secrets:
db_password: db_password:
@ -91,9 +97,6 @@ networks:
internal: internal:
volumes: volumes:
mongodb_log:
mongodb_lib:
mongodb:
db-data: db-data:
configs: configs:
@ -101,3 +104,6 @@ configs:
name: ${STACK_NAME}_app_entrypoint_${APP_ENTRYPOINT_VERSION} name: ${STACK_NAME}_app_entrypoint_${APP_ENTRYPOINT_VERSION}
file: entrypoint.sh.tmpl file: entrypoint.sh.tmpl
template_driver: golang template_driver: golang
pg_backup:
name: ${STACK_NAME}_pg_backup_${PG_BACKUP_VERSION}
file: pg_backup.sh

34
pg_backup.sh Normal file
View File

@ -0,0 +1,34 @@
#!/bin/bash
set -e
BACKUP_FILE='/var/lib/postgresql/data/backup.sql'
function backup {
export PGPASSWORD=$(cat /run/secrets/db_password)
pg_dump -U ${POSTGRES_USER} ${POSTGRES_DB} > $BACKUP_FILE
}
function restore {
cd /var/lib/postgresql/data/
restore_config(){
# Restore allowed connections
cat pg_hba.conf.bak > pg_hba.conf
su postgres -c 'pg_ctl reload'
}
# Don't allow any other connections than local
cp pg_hba.conf pg_hba.conf.bak
echo "local all all trust" > pg_hba.conf
su postgres -c 'pg_ctl reload'
trap restore_config EXIT INT TERM
# Recreate Database
psql -U ${POSTGRES_USER} -d postgres -c "DROP DATABASE ${POSTGRES_DB} WITH (FORCE);"
createdb -U ${POSTGRES_USER} ${POSTGRES_DB}
psql -U ${POSTGRES_USER} -d ${POSTGRES_DB} -1 -f $BACKUP_FILE
trap - EXIT INT TERM
restore_config
}
$@