Adding a secondary postgres that starts from a pg_basebackup. Still needs testing.

This commit is contained in:
Samuel Clay 2022-04-30 07:24:46 -04:00
parent de309183c5
commit 3670faf99d
3 changed files with 36 additions and 11 deletions

View file

@ -21,20 +21,15 @@
state: directory
mode: 0777
- name: Start postgres docker containers
- name: Start postgres basebackup on secondary
become: yes
docker_container:
name: postgres
image: postgres:13
state: started
container_default_behavior: no_defaults
command: postgres -c config_file=/etc/postgresql/postgresql.conf
env:
# POSTGRES_USER: "{{ postgres_user }}" # Don't auto-create newsblur, manually add it
POSTGRES_PASSWORD: "{{ postgres_password }}"
hostname: "{{ inventory_hostname }}"
command: pg_basebackup -h db-postgres.service.nyc1.consul -p 5432 -U newsblur -D /var/lib/postgresql/main -Fp -R -Xs -P -c fast
networks_cli_compatible: yes
# network_mode: host
network_mode: default
networks:
- name: newsblurnet
@ -48,6 +43,35 @@
- /srv/newsblur/docker/postgres/postgres_hba-13.conf:/etc/postgresql/pg_hba.conf
- /srv/newsblur/backups/:/var/lib/postgresql/backup/
restart_policy: unless-stopped
when: (inventory_hostname | regex_replace('[0-9]+', '')) in ['db-postgres-secondary']
- name: Start postgres docker containers
become: yes
docker_container:
name: postgres
image: postgres:13
state: started
container_default_behavior: no_defaults
command: postgres -c config_file=/etc/postgresql/postgresql.conf
env:
# POSTGRES_USER: "{{ postgres_user }}" # Don't auto-create newsblur, manually add it
POSTGRES_PASSWORD: "{{ postgres_password }}"
hostname: "{{ inventory_hostname }}"
networks_cli_compatible: yes
network_mode: default
networks:
- name: newsblurnet
aliases:
- postgres
ports:
- 5432:5432
volumes:
- /srv/newsblur/docker/volumes/postgres:/var/lib/postgresql
- /srv/newsblur/docker/postgres/postgres.conf:/etc/postgresql/postgresql.conf
- /srv/newsblur/docker/postgres/postgres_hba-13.conf:/etc/postgresql/pg_hba.conf
- /srv/newsblur/backups/:/var/lib/postgresql/backup/
restart_policy: unless-stopped
when: (inventory_hostname | regex_replace('[0-9]+', '')) in ['db-postgres-primary', 'db-postgres']
- name: Ensure newsblur role in postgres
shell: >

View file

@ -1,11 +1,12 @@
#!/usr/bin/env bash
now=$(date '+%Y-%m-%d-%H-%M')
BACKUP_FILENAME="backup_postgresql_${now}.sql"
BACKUP_PATH="/var/lib/postgresql/backup/"
UPLOAD_PATH="/srv/newsblur/backups/"
BACKUP_FILENAME="backup_postgresql_${now}.sql"
BACKUP_FILE="${BACKUP_PATH}${BACKUP_FILENAME}"
UPLOAD_FILE="${UPLOAD_PATH}${BACKUP_FILENAME}"
echo $(date -u) "---> PG dumping - ${now}: ${BACKUP_FILE}"
sudo docker exec postgres sh -c "mkdir -p $BACKUP_PATH"
sudo docker exec postgres sh -c "/usr/lib/postgresql/13/bin/pg_dump -U newsblur -h 127.0.0.1 -Fc newsblur > $BACKUP_FILE"

View file

@ -317,10 +317,10 @@ restore_command = 'cp /var/lib/postgresql/archive/%f %p' # command to use to re
#promote_trigger_file = '' # file name whose presence ends recovery
hot_standby = on # "off" disallows queries during recovery
# (change requires restart)
#max_standby_archive_delay = 30s # max delay before canceling queries
max_standby_archive_delay = 900s # max delay before canceling queries
# when reading WAL from archive;
# -1 allows indefinite delay
#max_standby_streaming_delay = 30s # max delay before canceling queries
max_standby_streaming_delay = 900s # max delay before canceling queries
# when reading streaming WAL;
# -1 allows indefinite delay
#wal_receiver_create_temp_slot = off # create temp slot if primary_slot_name