Skip to content

Commit c9aba84

Browse files
committed
feat: added custom database script to run a postgres with pgbackrest inside a docker container
1 parent 0fa85a7 commit c9aba84

File tree

5 files changed

+98
-0
lines changed

5 files changed

+98
-0
lines changed

bare-metal/.env.template

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
BACKUP_HOST_IP=100.000.0.1
2+
SYNC_NETWORK=mainnet01

bare-metal/Dockerfile.customdb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
FROM postgres:17
2+
3+
# Install pgBackRest and dependencies
4+
RUN apt-get update && \
5+
apt-get install -y --no-install-recommends \
6+
pgbackrest \
7+
openssh-client \
8+
gettext-base \
9+
ssh \
10+
&& rm -rf /var/lib/apt/lists/*
11+
12+
# Create folders for pgBackRest and adjust permissions
13+
RUN mkdir -p /tmp/pgbackrest /var/log/pgbackrest && \
14+
chown postgres:postgres /tmp/pgbackrest /var/log/pgbackrest && \
15+
chmod 700 /tmp/pgbackrest /var/log/pgbackrest
16+
17+
# Add the pgbackrest configuration file
18+
COPY pgbackrest.conf.generated /etc/pgbackrest.conf
19+
RUN chown postgres:postgres /etc/pgbackrest.conf && \
20+
chmod 600 /etc/pgbackrest.conf
21+
22+
# Create the .ssh folder and copy the SSH keys
23+
RUN mkdir -p /var/lib/postgresql/.ssh
24+
COPY id_ed25519 /var/lib/postgresql/.ssh/id_ed25519
25+
COPY id_ed25519.pub /var/lib/postgresql/.ssh/id_ed25519.pub
26+
27+
# Adjust permissions for the SSH keys
28+
RUN chown -R postgres:postgres /var/lib/postgresql/.ssh && \
29+
chmod 700 /var/lib/postgresql/.ssh && \
30+
chmod 600 /var/lib/postgresql/.ssh/id_ed25519 && \
31+
chmod 644 /var/lib/postgresql/.ssh/id_ed25519.pub
32+
33+
# Copy the custom initialization script
34+
COPY script.sh.generated /script.sh
35+
RUN chmod +x /script.sh
36+
37+
# Expose the default PostgreSQL port
38+
EXPOSE 5432
39+
EXPOSE 5433
40+
41+
# Default CMD to keep container alive and interactive
42+
CMD ["bash"]

bare-metal/build.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# Exit immediately if a command exits with a non-zero status.
4+
set -e
5+
6+
# Check if .env file exists
7+
if [ ! -f .env ]; then
8+
echo "Error: .env file not found."
9+
exit 1
10+
fi
11+
12+
# Export variables from .env file
13+
export $(grep -v '^#' .env | xargs)
14+
15+
# Check for required variables
16+
if [ -z "$SYNC_NETWORK" ] || [ -z "$BACKUP_HOST_IP" ]; then
17+
echo "Error: SYNC_NETWORK and BACKUP_HOST_IP must be set in .env"
18+
exit 1
19+
fi
20+
21+
# Substitute variables in script.sh.template
22+
envsubst < ./script.sh.template > ./script.sh.generated
23+
24+
# Substitute variables in pgbackrest.conf.template
25+
envsubst < ./pgbackrest.conf.template > ./pgbackrest.conf.generated
26+
27+
# Build the Docker image
28+
docker build -f Dockerfile.customdb -t custom-db:${SYNC_NETWORK} .
29+
30+
# Clean up generated files
31+
rm script.sh.generated
32+
rm pgbackrest.conf.generated
33+
34+
echo "Docker image 'custom-db' built successfully."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[global]
2+
repo1-host=${BACKUP_HOST_IP}
3+
repo1-host-user=ubuntu
4+
repo1-path=/var/lib/pgbackrest
5+
repo1-retention-full=2
6+
start-fast=y
7+
log-level-console=info
8+
log-level-file=info
9+
10+
[${SYNC_NETWORK}]
11+
pg1-path=/var/lib/postgresql/data
12+
pg1-user=postgres
13+
pg1-database=indexer

bare-metal/script.sh.template

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
3+
ssh-keyscan -H ${BACKUP_HOST_IP} >> ~postgres/.ssh/known_hosts
4+
chown postgres:postgres ~postgres/.ssh/known_hosts
5+
chmod 644 ~postgres/.ssh/known_hosts
6+
7+
exec /usr/local/bin/docker-entrypoint.sh postgres -c archive_mode=on -c archive_command='pgbackrest --stanza=${SYNC_NETWORK} archive-push %p' -c ssl=on -c ssl_cert_file=/var/lib/postgresql/server.crt -c ssl_key_file=/var/lib/postgresql/server.key -c shared_buffers=8GB -c effective_cache_size=24GB -c maintenance_work_mem=2GB -c work_mem=64MB -c max_wal_size=16GB -c random_page_cost=1.1 -c effective_io_concurrency=200 -c synchronous_commit=off

0 commit comments

Comments
 (0)