Add backup-bot-two labels

This commit is contained in:
3wc 2025-06-12 15:39:09 +01:00
parent 8f34f18ba3
commit 2334f04e21
4 changed files with 53 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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

35
pg_backup.sh Normal file
View File

@ -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
}
$@