From 2334f04e21738d207bc0aab08d489ab4257a5065 Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Thu, 12 Jun 2025 15:39:09 +0100 Subject: [PATCH] Add backup-bot-two labels --- TODO.md | 2 +- abra.sh | 1 + compose.yml | 16 ++++++++++++++++ pg_backup.sh | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 pg_backup.sh diff --git a/TODO.md b/TODO.md index 640e37b..bdb4b2b 100644 --- a/TODO.md +++ b/TODO.md @@ -7,7 +7,7 @@ - [x] External OIDC server (+ move options to `.env.sample`) - [x] Customisable Django `DJANGO_SECRET_KEY` and `DJANGO_SUPERUSER_PASSWORD` - [ ] Versioned recipe release -- [ ] `backup-bot-two` labels +- [x] `backup-bot-two` labels - [ ] Fix Django admin static files ## Should diff --git a/abra.sh b/abra.sh index 0a818c9..f2314ad 100755 --- a/abra.sh +++ b/abra.sh @@ -1,6 +1,7 @@ # Set any config versions here # Docs: https://docs.coopcloud.tech/maintainers/handbook/#manage-configs export NGINX_CONF_VERSION=v2 +export PG_BACKUP_VERSION=v3 # environment() { # # TODO: Add file_env here diff --git a/compose.yml b/compose.yml index 25dc832..2716c0e 100644 --- a/compose.yml +++ b/compose.yml @@ -141,6 +141,16 @@ services: PGDATA: var/lib/postgresql/data/pgdata volumes: - postgres:/var/lib/postgresql/data/pgdata + deploy: + labels: + backupbot.backup: "${ENABLE_BACKUPS:-true}" + backupbot.backup.pre-hook: "/pg_backup.sh backup" + backupbot.backup.volumes.postgres.path: "backup.sql" + backupbot.restore.post-hook: '/pg_backup.sh restore' + configs: + - source: pg_backup + target: /pg_backup.sh + mode: 0555 redis: image: redis:5 @@ -179,6 +189,9 @@ services: command: minio server /data volumes: - minio:/data + deploy: + labels: + backupbot.backup: "${ENABLE_BACKUPS:-true}" web: image: nginx:1.27 @@ -211,3 +224,6 @@ configs: nginx_conf: name: ${STACK_NAME}_nginx_conf_${NGINX_CONF_VERSION} file: nginx.conf + pg_backup: + name: ${STACK_NAME}_pg_backup_${PG_BACKUP_VERSION} + file: pg_backup.sh diff --git a/pg_backup.sh b/pg_backup.sh new file mode 100644 index 0000000..2ab1ea2 --- /dev/null +++ b/pg_backup.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +set -e + +BACKUP_FILE='/var/lib/postgresql/data/pgdata/backup.sql' + +function backup { + # export PGPASSWORD=$(cat $POSTGRES_PASSWORD_FILE) + export PGPASSWORD="$POSTGRES_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 +} + +$@