Skip to content

Commit

Permalink
pgbackrest for backup
Browse files Browse the repository at this point in the history
  • Loading branch information
Pandoks committed Dec 11, 2024
1 parent b7a2749 commit 1900d3c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 43 deletions.
4 changes: 1 addition & 3 deletions packages/core/src/database/images/Dockerfile.slave
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ RUN rm -rf /var/lib/postgresql/data/*
COPY entrypoint.slave.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]

USER postgres

CMD ["postgres", "-c", "config_file=/etc/postgresql/postgresql.conf"]
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
75 changes: 37 additions & 38 deletions packages/core/src/database/images/compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ networks:
services:
s3:
image: localstack/localstack
ports:
- "4566:4566"
environment:
- SERVICES=s3
- AWS_DEFAULT_REGION=us-west-1
Expand Down Expand Up @@ -58,8 +56,6 @@ services:
build:
context: .
dockerfile: Dockerfile
ports:
- "5432:5432"
environment:
# used to setup database
- POSTGRES_USER=user
Expand All @@ -69,7 +65,7 @@ services:
- PGUSER=user
- PGDATABASE=userdb
- PGPASSWORD=password # server replication password
- POSTGRES_TYPE=backup
# - POSTGRES_TYPE=backup
volumes:
- ../main/setup/_init.sql:/docker-entrypoint-initdb.d/init.sql
- ../main/setup/roles.sql:/docker-entrypoint-initdb.d/roles.sql
Expand All @@ -92,39 +88,42 @@ services:
backup:
condition: service_healthy

# slavedb:
# image: slavedb:latest
# build:
# context: .
# dockerfile: Dockerfile.slave
# ports:
# - "5433:5432"
# environment:
# # used to setup database
# - POSTGRES_USER=user
# - POSTGRES_PASSWORD=password
# - POSTGRES_DB=userdb
# # used to connect to the database
# - PGUSER=user
# - PGDATABASE=userdb
# - PGPASSWORD=password # server replication password
# - POSTGRES_TYPE=backup
# volumes:
# - ./postgresql.conf:/etc/postgresql/postgresql.conf
# - ./pg_hba.conf:/etc/postgresql/pg_hba.conf
# - ./init.sh:/docker-entrypoint-initdb.d/init.sh
# - ./pgbackrest.conf:/etc/pgbackrest/pgbackrest.conf
# networks:
# - database-network
# healthcheck:
# test: ["CMD-SHELL", "pg_isready", "-U", "user", "-d", "userdb"]
# interval: 1s
# retries: 10
# depends_on:
# masterdb:
# condition: service_healthy
# s3:
# condition: service_healthy
slavedb:
image: slavedb:latest
build:
context: .
dockerfile: Dockerfile.slave
environment:
# used to setup database
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
- POSTGRES_DB=userdb
# used to connect to the database
- PGUSER=user
- PGDATABASE=userdb
- PGPASSWORD=password # server replication password
- POSTGRES_TYPE=backup
volumes:
- ./postgresql.conf:/etc/postgresql/postgresql.conf
- ./pg_hba.conf:/etc/postgresql/pg_hba.conf
- ./init.sh:/docker-entrypoint-initdb.d/init.sh
- ./pgbackrest.conf:/etc/pgbackrest/pgbackrest.conf
- ../../../../../.certs/dev/ca/ca.crt:/etc/pgbackrest/ca.crt
- ../../../../../.certs/dev/server/server.crt:/etc/pgbackrest/server.crt
- ../../../../../.certs/dev/server/server.key:/etc/pgbackrest/server.key
- ../../../../../.certs/dev/client/images-slavedb-1.key:/etc/pgbackrest/client.key
- ../../../../../.certs/dev/client/images-slavedb-1.crt:/etc/pgbackrest/client.crt
networks:
- database-network
healthcheck:
test: ["CMD-SHELL", "pg_isready", "-U", "user", "-d", "userdb"]
interval: 1s
retries: 10
depends_on:
masterdb:
condition: service_healthy
s3:
condition: service_healthy

# pgcat:
# image: ghcr.io/postgresml/pgcat:latest
Expand Down
24 changes: 23 additions & 1 deletion packages/core/src/database/images/entrypoint.slave.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,26 @@ pg_basebackup -D /var/lib/postgresql/data/ -h images-masterdb-1 -X stream -c fas
chmod 700 /var/lib/postgresql/data
touch /var/lib/postgresql/data/standby.signal

exec $@
postgres -c config_file=/etc/postgresql/postgresql.conf &

until pg_isready -U $POSTGRES_USER -d $POSTGRES_DB; do
sleep 1
done

PG_PID=$!
echo "Postgresql is ready"

monitor_pgbackrest() {
while true; do
wait $PGBR_PID
echo "Pgbackrest doesn't exist. Starting pgbackrest..."
pgbackrest server --config=/etc/pgbackrest/pgbackrest.conf &
PGBR_PID=$!
echo "Pgbackrest is ready"
done
}
monitor_pgbackrest &

# shutdown container when database dies (allows for auto restart recovery)
wait $PG_PID
kill $PGBR_PID
2 changes: 1 addition & 1 deletion packages/core/src/database/images/pgbackrest.backup.conf
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ tls-server-cert-file=/etc/pgbackrest/server.crt
tls-server-key-file=/etc/pgbackrest/server.key
tls-server-ca-file=/etc/pgbackrest/ca.crt
tls-server-auth=images-masterdb-1=main
tls-server-auth=images-slavedb-1=main

[main]
pg1-path=/var/lib/postgresql/data
pg1-host=images-masterdb-1
pg1-host-type=tls

pg1-host-cert-file=/etc/pgbackrest/server.crt
pg1-host-key-file=/etc/pgbackrest/server.key
pg1-host-ca-file=/etc/pgbackrest/ca.crt

0 comments on commit 1900d3c

Please sign in to comment.