From c52e9c6744e138985331e3967b6204447a7f464c Mon Sep 17 00:00:00 2001 From: f Date: Thu, 2 Jan 2025 16:47:25 -0300 Subject: [PATCH 1/3] feat: enable backup-bot-two --- .env.sample | 1 + README.md | 2 +- compose.yml | 15 +++++++++++++++ pg_backup.sh | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 pg_backup.sh diff --git a/.env.sample b/.env.sample index ec3d75f..491379c 100644 --- a/.env.sample +++ b/.env.sample @@ -9,3 +9,4 @@ LETS_ENCRYPT_ENV=production SECRET_DB_PASSWORD_VERSION=v1 SECRET_NC_DB_URL_VERSION=v1 +ENABLE_BACKUPS=true diff --git a/README.md b/README.md index 5d91d88..2fc3d56 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ * **Status**: 0 * **Image**: [`nocodb`](https://hub.docker.com/r/nocodb), 4, upstream * **Healthcheck**: No -* **Backups**: No +* **Backups**: Yes * **Email**: No * **Tests**: No * **SSO**: No diff --git a/compose.yml b/compose.yml index c1a1a17..afe482b 100644 --- a/compose.yml +++ b/compose.yml @@ -29,6 +29,7 @@ services: #- "traefik.http.middlewares.${STACK_NAME}-redirect.headers.SSLForceHost=true" #- "traefik.http.middlewares.${STACK_NAME}-redirect.headers.SSLHost=${DOMAIN}" - "coop-cloud.${STACK_NAME}.version=1.0.1+0.258.10" + - "backupbot.backup=${ENABLE_BACKUPS:-true}" db: image: postgres:13.2-alpine @@ -47,6 +48,16 @@ services: interval: 10s timeout: 2s retries: 10 + 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 volumes: data: @@ -65,3 +76,7 @@ secrets: nc_db_url: external: true name: ${STACK_NAME}_nc_db_url_${SECRET_NC_DB_URL_VERSION} +configs: + 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..e83074d --- /dev/null +++ b/pg_backup.sh @@ -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 +} + +$@ -- 2.47.2 From 955c3b24977351df4be6e9a73e618cbf23918010 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 2 Jan 2025 16:57:28 -0300 Subject: [PATCH 2/3] fix: abra.sh --- abra.sh | 1 + 1 file changed, 1 insertion(+) create mode 100644 abra.sh diff --git a/abra.sh b/abra.sh new file mode 100644 index 0000000..0975f1e --- /dev/null +++ b/abra.sh @@ -0,0 +1 @@ +export PG_BACKUP_VERSION=v1 -- 2.47.2 From 500bd060af198906626de544db2b2e71ecc73c87 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 14 Apr 2025 10:15:41 -0300 Subject: [PATCH 3/3] fix: use volume name --- compose.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compose.yml b/compose.yml index aa605f9..2f4a375 100644 --- a/compose.yml +++ b/compose.yml @@ -52,7 +52,7 @@ services: labels: backupbot.backup: "${ENABLE_BACKUPS:-true}" backupbot.backup.pre-hook: "/pg_backup.sh backup" - backupbot.backup.volumes.postgres.path: "backup.sql" + backupbot.backup.volumes.db.path: "backup.sql" backupbot.restore.post-hook: '/pg_backup.sh restore' configs: - source: pg_backup -- 2.47.2