update backupbot label

This commit is contained in:
Moritz 2024-10-24 17:01:04 +02:00
parent cae11a78e6
commit bd57d6121b
7 changed files with 52 additions and 10 deletions

View File

@ -17,6 +17,7 @@ steps:
environment: environment:
APP_INI_VERSION: v1 APP_INI_VERSION: v1
DOCKER_SETUP_SH_VERSION: v1 DOCKER_SETUP_SH_VERSION: v1
PG_BACKUP_VERSION: v1
DOMAIN: gitea.swarm-test.autonomic.zone DOMAIN: gitea.swarm-test.autonomic.zone
GITEA_ALLOW_ONLY_EXTERNAL_REGISTRATION: true GITEA_ALLOW_ONLY_EXTERNAL_REGISTRATION: true
GITEA_APP_NAME: Git with solidaritea GITEA_APP_NAME: Git with solidaritea

View File

@ -3,6 +3,7 @@ TYPE=gitea
DOMAIN=gitea.example.com DOMAIN=gitea.example.com
LETS_ENCRYPT_ENV=production LETS_ENCRYPT_ENV=production
COMPOSE_FILE="compose.yml" COMPOSE_FILE="compose.yml"
ENABLE_BACKUPS=true
COMPOSE_FILE="$COMPOSE_FILE:compose.mariadb.yml" COMPOSE_FILE="$COMPOSE_FILE:compose.mariadb.yml"
# COMPOSE_FILE="$COMPOSE_FILE:compose.postgres.yml" # COMPOSE_FILE="$COMPOSE_FILE:compose.postgres.yml"

View File

@ -1,5 +1,6 @@
export APP_INI_VERSION=v18 export APP_INI_VERSION=v18
export DOCKER_SETUP_SH_VERSION=v1 export DOCKER_SETUP_SH_VERSION=v1
export PG_BACKUP_VERSION=v1
abra_backup_app() { abra_backup_app() {
_abra_backup_dir "app:/var/lib/gitea" _abra_backup_dir "app:/var/lib/gitea"

View File

@ -11,10 +11,9 @@ services:
image: "mariadb:10.11.2" image: "mariadb:10.11.2"
deploy: deploy:
labels: labels:
backupbot.backup: "true"
backupbot.backup.pre-hook: 'mysqldump --single-transaction -u root -p"$$(cat /run/secrets/db_root_password)" gitea > /var/lib/mysql/backup.sql' backupbot.backup.pre-hook: 'mysqldump --single-transaction -u root -p"$$(cat /run/secrets/db_root_password)" gitea > /var/lib/mysql/backup.sql'
backupbot.backup.post-hook: "rm -rf /var/lib/mysql/backup.sql" backupbot.backup.volumes.mariadb.path: "backup.sql"
backupbot.backup.path: "/var/lib/mysql/backup.sql" backupbot.restore.post-hook: "mariadb -u root -p\"$$(cat /run/secrets/db_root_password)\" gitea < /var/lib/mysql/backup.sql"
command: | command: |
mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
environment: environment:
@ -40,4 +39,3 @@ secrets:
volumes: volumes:
mariadb: mariadb:
internal:

View File

@ -11,10 +11,9 @@ services:
image: postgres:15.8 image: postgres:15.8
deploy: deploy:
labels: labels:
backupbot.backup: "true" backupbot.backup.pre-hook: "/pg_backup.sh backup"
backupbot.backup.pre-hook: "PGPASSWORD=$$(cat $${POSTGRES_PASSWORD_FILE}) pg_dump -U $${POSTGRES_USER} $${POSTGRES_DB} > /var/lib/postgresql/data/backup.sql" backupbot.backup.volumes.db.path: "backup.sql"
backupbot.backup.post-hook: "rm -r /var/lib/postgresql/data/backup.sql" backupbot.restore.post-hook: '/pg_backup.sh restore'
backupbot.backup.path: "/var/lib/postgresql/data"
environment: environment:
- POSTGRES_DB=gitea - POSTGRES_DB=gitea
- POSTGRES_USER=gitea - POSTGRES_USER=gitea
@ -25,6 +24,10 @@ services:
- db:/var/lib/postgresql/data - db:/var/lib/postgresql/data
networks: networks:
- internal - internal
configs:
- source: pg_backup
target: /pg_backup.sh
mode: 0555
secrets: secrets:
db_password: db_password:
@ -33,4 +36,8 @@ secrets:
volumes: volumes:
db: db:
internal:
configs:
pg_backup:
name: ${STACK_NAME}_pg_backup_${PG_BACKUP_VERSION}
file: pg_backup.sh

View File

@ -71,7 +71,7 @@ services:
failure_action: rollback failure_action: rollback
order: start-first order: start-first
labels: labels:
- "backupbot.backup=true" - "backupbot.backup=${ENABLE_BACKUPS:-true}"
- "traefik.enable=true" - "traefik.enable=true"
- "traefik.http.routers.${STACK_NAME}.rule=Host(`${DOMAIN}`)" - "traefik.http.routers.${STACK_NAME}.rule=Host(`${DOMAIN}`)"
- "traefik.http.routers.${STACK_NAME}.entrypoints=web-secure" - "traefik.http.routers.${STACK_NAME}.entrypoints=web-secure"

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