commit b38b1711162545a64139289f6f6fe3e72825988a Author: gsd Date: Sat Nov 9 21:24:21 2024 +0300 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c49bd7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9d3dec7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM docker.pblr-nyk.pro/postgres:16.4-bullseye +COPY reppoint /reppoint +RUN chmod +x /reppoint +ENTRYPOINT ["/reppoint"] +CMD [] diff --git a/README b/README new file mode 100644 index 0000000..1802be9 --- /dev/null +++ b/README @@ -0,0 +1,14 @@ +Делает полную копию бд посгресса в оперативку или любое другое место. + +Че добавить на реплике: +-> .env +MASTER_ADDRESS=x.x.x.x #Ип бд +MASTER_USER=user #Юзер для реплики +TZ=Europe/Moscow #Я русский + +Че добавить на мастере: +wal_level = hot_standby +archive_mode = on +archive_command = 'cd .' +max_wal_senders = 8 +hot_standby = on diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..2f6eed6 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,20 @@ +services: + postgres_db_mem_rep: + build: ./ + container_name: postgress_db_mem_replica + #restart: unless-stopped + env_file: .env + tmpfs: + - /var/lib/postgresql/data + ports: + - 5442:5432 + deploy: + resources: + limits: + cpus: "4.0" + memory: 16G +# networks: +# - postgres_db_default +#networks: +# postgres_db_default: +# external: true diff --git a/reppoint b/reppoint new file mode 100644 index 0000000..df7a21c --- /dev/null +++ b/reppoint @@ -0,0 +1,46 @@ +#!/bin/bash + +if [[ -z "${MASTER_ADDRESS}" ]]; then + echo "Cannot startup, need MASTER_ADDRESS" +else + MASTER_ADDRESS="${MASTER_ADDRESS}" + echo "copy from ${MASTER_ADDRESS}" +fi + +echo "init default scripts" +export POSTGRES_PASSWORD="replica" +docker-entrypoint.sh postgres -v + +echo "add user" +#add replication user +cat <> /var/lib/postgresql/data/pg_hba.conf +host replication postgres $MASTER_ADDRESS/24 trust +host all all all md5 +EOT + +echo "add replica rules" +#add replica rules +cat <> /var/lib/postgresql/data/postgresql.conf +listen_addresses = '*' +wal_level = hot_standby +archive_mode = on +archive_command = 'cd .' +max_wal_senders = 8 +hot_standby = on +max_connections = 1000 +EOT + +#create copy +mkdir /tmp/pg_copy +cp /var/lib/postgresql/data/*.conf /tmp/pg_copy +echo "copy from master" +rm -rf /var/lib/postgresql/data +mkdir /var/lib/postgresql/data +pg_basebackup -v -P -R -X stream -c fast -h ${MASTER_ADDRESS} -U ${MASTER_USER} -D /var/lib/postgresql/data +rm /var/lib/postgresql/data/*.conf +cp /tmp/pg_copy/*.conf /var/lib/postgresql/data/ +chown postgres:postgres -R /var/lib/postgresql/data + +echo "run postgres" +#run postgres +docker-entrypoint.sh postgres