Skip to content

Commit 26a85d5

Browse files
author
Andre Wanderley de Souza
committed
added barman - work-in-progress
1 parent d6fc88e commit 26a85d5

File tree

14 files changed

+293
-11
lines changed

14 files changed

+293
-11
lines changed

Dockerfile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ ENV PRIMARY_NODE=localhost
1515
ENV REPMGR_USER=repmgr
1616
ENV REPMGR_DB=repmgr
1717

18-
RUN apt-get update; apt-get install -y git make postgresql-server-dev-10 libpq-dev postgresql-10-repmgr repmgr-common
18+
RUN apt-get update; apt-get install -y git make postgresql-server-dev-10 libpq-dev postgresql-10-repmgr repmgr-common openssh-server
1919

2020
RUN git clone https://github.com/mreithub/pg_recall.git /root/pg_recall/
2121
RUN cd /root/pg_recall/; make install
@@ -29,12 +29,22 @@ COPY scripts/*.sh /docker-entrypoint-initdb.d/
2929
COPY promote.sh /usr/local/bin/promote.sh
3030

3131
COPY pgbouncer.pem /var/lib/postgresql/.ssh/id_rsa
32+
COPY ssh_config /var/lib/postgresql/.ssh/config
3233

3334
RUN chown -R postgres:postgres /var/lib/postgresql/.ssh
3435
RUN chmod 700 /var/lib/postgresql/.ssh
3536
RUN chmod 600 /var/lib/postgresql/.ssh/id_rsa
3637

37-
COPY ssh_config /var/lib/postgresql/.ssh/config
38+
ENV \
39+
BARMAN_USER=barman \
40+
BARMAN_PASSWORD= \
41+
BARMAN_SLOT_NAME=barman \
42+
STREAMING_USER=streaming_barman \
43+
STREAMING_PASSWORD=
44+
45+
COPY barman.pub /var/lib/postgresql/.ssh/authorized_keys
46+
RUN chmod 600 /var/lib/postgresql/.ssh/authorized_keys
47+
RUN chown postgres:postgres /var/lib/postgresql/.ssh/authorized_keys
3848

3949
VOLUME /home/postgres/
4050
VOLUME /var/lib/postgresql/data/

README.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ This docker image uses the following environment variables (with their defaults
1717
- `REPMGR_DB=repmgr`
1818
- `REPMGR_PASSWORD` (required)
1919
Use something like `pwgen -n 24 1` to generate a random one (and make sure you use the same one on all your nodes
20-
- `NODE_HOST=`
20+
- `BARMAN_PASSWORD` (required)
21+
Use something like `pwgen -n 24 1` to generate a random one (and make sure you use the same one on all your nodes
22+
- `STREAMING_PASSWORD` (required)
23+
Use something like `pwgen -n 24 1` to generate a random one (and make sure you use the same one on all your nodes
24+
- `PRIMARY_NODE=`
2125
If set, this is used in the `conninfo` string (used by other nodes to connect to this one.
2226
If empty, `hostname -f` is used
2327
Make sure you use a hostname the others can resolve (or an IP address)
@@ -30,30 +34,42 @@ docker build --tag postgres-repmgr .
3034

3135
docker build --tag postgres-pgbouncer pgbouncer
3236

37+
docker build --tag postgres-barman barman
38+
3339
### RUN IT
34-
export REPMGR_PASSWORD=RANDONSTRING
40+
export REPMGR_PASSWORD=`nicepass --password-length 24`
41+
42+
export BARMAN_PASSWORD=`nicepass --password-length 24`
43+
44+
export STREAMING_PASSWORD=`nicepass --password-length 24`
3545

36-
docker run --name pg-repmgr-1 --network pg_stream -e REPMGR_PASSWORD=$REPMGR_PASSWORD -d postgres-repmgr
46+
docker run --name pg-repmgr-1 --network pg_stream -e REPMGR_PASSWORD=$REPMGR_PASSWORD -e BARMAN_PASSWORD=$BARMAN_PASSWORD -e STREAMING_PASSWORD=$STREAMING_PASSWORD -d postgres-repmgr
3747

3848
sleep 2
3949

40-
docker run --name pg-repmgr-2 --network pg_stream -e REPMGR_PASSWORD=$REPMGR_PASSWORD -e PRIMARY_NODE=pg-repmgr-1 -d postgres-repmgr
50+
docker run --name pg-repmgr-2 --network pg_stream -e REPMGR_PASSWORD=$REPMGR_PASSWORD -e BARMAN_PASSWORD=$BARMAN_PASSWORD -e STREAMING_PASSWORD=$STREAMING_PASSWORD -e PRIMARY_NODE=pg-repmgr-1 -d postgres-repmgr
4151

4252
sleep 2
4353

44-
docker run --name pg-repmgr-3 --network pg_stream -e REPMGR_PASSWORD=$REPMGR_PASSWORD -e PRIMARY_NODE=pg-repmgr-1 -d postgres-repmgr
54+
docker run --name pg-repmgr-3 --network pg_stream -e REPMGR_PASSWORD=$REPMGR_PASSWORD -e BARMAN_PASSWORD=$BARMAN_PASSWORD -e STREAMING_PASSWORD=$STREAMING_PASSWORD -e PRIMARY_NODE=pg-repmgr-1 -d postgres-repmgr
4555

4656
sleep 8
4757

4858
docker exec -it pg-repmgr-2 su -c "repmgr cluster show" - postgres
4959
sleep 3
5060

61+
62+
#### PGBOUNCER
5163
docker run --name pg-pgbouncer-1 --network pg_stream -e PRIMARY_NODE=pg-repmgr-1 -d postgres-pgbouncer
5264

5365
sleep 1
5466

5567
docker exec -it pg-pgbouncer-1 psql -U postgres -c "select client_addr, state, sent_lsn, write_lsn, flush_lsn, replay_lsn from pg_stat_replication;"
5668

69+
#### BARMAN
70+
docker run --name pg-barman-1 --network pg_stream -e BARMAN_PASSWORD=$BARMAN_PASSWORD -e STREAMING_PASSWORD=$STREAMING_PASSWORD -e PRIMARY_NODE=pg-repmgr-1 -d postgres-barman
71+
72+
docker exec -it pg-barman-1 barman check pg-repmgr-1
5773

5874
### FORCE FAILOVER
5975
[ monitor from another shell ] docker logs -f pg-repmgr-2
@@ -90,10 +106,14 @@ docker kill pg-repmgr-3
90106

91107
docker kill pg-pgbouncer-1
92108

109+
docker kill pg-barman-1
110+
93111
docker rm pg-repmgr-1
94112

95113
docker rm pg-repmgr-2
96114

97115
docker rm pg-repmgr-3
98116

99117
docker rm pg-pgbouncer-1
118+
119+
docker rm pg-barman-1

barman.pub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDDKBPsbe58VZuNhCrAKMUaD3uhcF/+aSl5MLx/aCIasI72mDZa7eTkmWob3Uus/1YotC52UQLohUQY6b4y4nySO0uXCQ1rXj+C8p1soMrGlr0P2TuP808bYwBaUxFiXDUxg+nrujwcXzoPkxWXd/CmJi48SO5uoJ6IBpeIkMnLWU+vSTroYL5OJXN4Pu/SKYbYtfKBBb/098kfpEfR9/f933k+ExPa/bKgfvE2Wf0t2Q+s7YPFih9edk7Cfs24CBeQA4dYgyq72WUJUFPfjcjGq2Cesk7H77BdaHeDlZvkYXdJRBLafROquwX1azDIaw3WDmfZC/S90A6clUDm+G20x+eHmaEN36EmpeUcjc0fLqE3g2nXTSuqDofsgRa7X839Hfu8wHsHbKiEKACch7rI3YdrnVo6SXWS/r9K389qv4hPBqb8zc7epsIKQPpuG1xF0Lv1uqR3V4YN0u/DMyaMD6Z5fL2zT5j2LE26Ku6S32Ymrmvzqwi/qLXIlxF9OBebxzMbyYSb/XXWgGCCsNgAkscT6/F2AM0OSJq2sl2jvrJ95HZJwl98SGD4koss3O2ylUqFQEgycSJ4HuiG8q20nSJFROyggfWWAa/ceQs0eO9lkVXRsFxYQAnS60FZa1Z2Dhw7jAGEF0u0qjZwL1twvglwu5XmWNWfRbSF3RyqJQ== [email protected]

barman/Dockerfile

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
FROM postgres:10
2+
3+
RUN apt-get update \
4+
&& apt-get install -y --no-install-recommends wget cron gcc libpq-dev libpython-dev logrotate openssh-client rsync python3-pip \
5+
&& rm -rf /var/lib/apt/lists/* \
6+
&& rm -f /etc/crontab /etc/cron.*/* \
7+
&& sed -i 's/\(.*pam_loginuid.so\)/#\1/' /etc/pam.d/cron
8+
9+
ENV \
10+
BARMAN_VERSION=2.1 \
11+
BARMAN_DATA_DIR=/var/lib/barman \
12+
BARMAN_LOG_DIR=/var/log/barman
13+
14+
RUN pip3 install barman==${BARMAN_VERSION} requests==2.13.0
15+
16+
RUN useradd --system --shell /bin/bash barman
17+
RUN install -d -m 0700 -o barman -g barman /home/barman/.ssh
18+
19+
COPY barman.pem /home/barman/.ssh/id_rsa
20+
RUN chmod 600 /home/barman/.ssh/id_rsa
21+
RUN chown barman:barman /home/barman/.ssh/id_rsa
22+
RUN gosu barman bash -c 'echo -e "Host *\n\tCheckHostIP no" > ~/.ssh/config'
23+
24+
ENV \
25+
BARMAN_USER=barman \
26+
BARMAN_PASSWORD= \
27+
BARMAN_SLOT_NAME=barman \
28+
STREAMING_USER=streaming_barman \
29+
STREAMING_PASSWORD= \
30+
PRIMARY_NODE=
31+
32+
RUN rm -f /etc/logrotate.d/*
33+
34+
COPY logrotate.conf /etc/logrotate.conf
35+
RUN chown root:root /etc/logrotate.conf
36+
RUN chmod 644 /etc/logrotate.conf
37+
38+
COPY basebackup.cron /etc/cron.d/basebackup
39+
RUN chown root:root /etc/cron.d/basebackup
40+
RUN chmod 644 /etc/cron.d/basebackup
41+
42+
COPY crontab /etc/crontab
43+
RUN chown root:root /etc/crontab
44+
RUN chmod 644 /etc/crontab
45+
46+
COPY etc /etc/barman
47+
48+
VOLUME /var/log/barman
49+
50+
ENTRYPOINT ["/entrypoint.sh"]
51+
CMD ["cron", "-L", "0", "-f"]
52+
53+
COPY entrypoint.sh /
54+
55+
WORKDIR ${BARMAN_DATA_DIR}

barman/barman.pem

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-----BEGIN OPENSSH PRIVATE KEY-----
2+
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAACFwAAAAdzc2gtcn
3+
NhAAAAAwEAAQAAAgEAwygT7G3ufFWbjYQqwCjFGg97oXBf/mkpeTC8f2giGrCO9pg2Wu3k
4+
5JlqG91LrP9WKLQudlEC6IVEGOm+MuJ8kjtLlwkNa14/gvKdbKDKxpa9D9k7j/NPG2MAWl
5+
MRYlw1MYPp67o8HF86D5MVl3fwpiYuPEjubqCeiAaXiJDJy1lPr0k66GC+TiVzeD7v0imG
6+
2LXygQW/9PfJH6RH0ff3/d95PhMT2v2yoH7xNln9LdkPrO2DxYofXnZOwn7NuAgXkAOHWI
7+
Mqu9llCVBT343IxqtgnrJOx++wXWh3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n3nnN+m9ufcs9udL4l5pl5BLv
37+
Z15BvDEvsoJxbL2sdkXQyAmkoH5YFYkKni99X6cmTzwPTufcoEVZhOtufU4+Pdtl7GrlHJ
38+
OvSIOkox7djTRdsAAAEBAOqVmCBGH/UeiYDEaKhUR/ObCc8JhBaKoK5skke/pSVOlzNc8q
39+
kdL/VCJECQUlsKvPnLXwgxRZmmIbqgSY3VeMJ8JErngSRSSBr1tCY3sZVGk9k/nF2+nY9F
40+
IxPJSXCY0SrEoJPsGGUqt3bUTgpRi3xCww7B1VRqFK9roc4CQ3NRWdlJbb7ce9MLj0YtWP
41+
b2vBKlLB3E3vYBOCt7eF94UVyasqlm6eWXGYTisggfEjF4vLnh5qknZW0RS2z9+EaqEi6T
42+
zp4U55CT11DWsG1rttmnB2uQF9gHLB35bJvyXv4B22JLkw23HtnL0CCCl5fIQB4zIAEzhW
43+
WCPiH/rlyn/pkAAAEBANT5BwdAn7xEbHJoapLRRDvM1pz+n/pELGQsubKHAwCog1Gq5haZ
44+
w14cBzUSmg3vMh2+7jilKH24l6k6c7Vr38jbKQ6skCoc0zOdkW+Vce+/7x4oE0Krrp85DO
45+
pbVTfLTtHf9yIpGC7TN5psO6IdI66nEpMZuSDghwQg4ynxuefLfSZKq5XlmEhRgZr+Auj9
46+
zkMnNvZ6Wq2+S/T75PRm+phmbfFkIkWitMEH5JRbfgCGHLbyGn1TKzkG21NK+bTcddg9gc
47+
5F6ypwtwhPDGEcFNehv0CBuOtk7rWG+l//4DtF5ZhGfQr+mgYYbhiH6yNlOvT+kKo3yN4y
48+
Yqe1wXooO20AAAAWaWVubGl2ZW5AbWVyY3VyeS5sb2NhbAECAwQF
49+
-----END OPENSSH PRIVATE KEY-----

barman/basebackup.cron

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0 4 * * 0 barman /usr/local/bin/barman backup all >>/var/log/barman/backup.log 2>&1

barman/crontab

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
* * * * * barman /usr/local/bin/barman -q cron
2+
@daily root /usr/sbin/logrotate /etc/logrotate.conf

barman/entrypoint.sh

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -eo pipefail
4+
5+
echo "${PRIMARY_NODE}:*:*:${BARMAN_USER}:${BARMAN_PASSWORD}" > /home/barman/.pgpass
6+
echo "${PRIMARY_NODE}:*:*:${STREAMING_USER}:${STREAMING_PASSWORD}" >> /home/barman/.pgpass
7+
8+
chmod 600 /home/barman/.pgpass
9+
chown barman:barman /home/barman/.pgpass
10+
11+
install -d -m 0755 -o barman -g barman "${BARMAN_LOG_DIR}"
12+
install -d -m 0700 -o barman -g barman "${BARMAN_DATA_DIR}"
13+
14+
sed -i "s#\$BARMAN_LOG_DIR#$BARMAN_LOG_DIR#g" /etc/logrotate.conf
15+
16+
sed -i "s#\$PRIMARY_NODE#$PRIMARY_NODE#g" /etc/barman/barman.d/pg.conf
17+
sed -i "s#\$BARMAN_USER#$BARMAN_USER#g" /etc/barman/barman.d/pg.conf
18+
sed -i "s#\$BARMAN_SLOT_NAME#$BARMAN_SLOT_NAME#g" /etc/barman/barman.d/pg.conf
19+
sed -i "s#\$STREAMING_USER#$STREAMING_USER#g" /etc/barman/barman.d/pg.conf
20+
21+
exec "$@"

barman/etc/barman.conf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; Commented lines show the default values
2+
3+
[barman]
4+
; archiver = off
5+
; backup_method = rsync
6+
; backup_directory = %(barman_home)s/%(name)s
7+
8+
; This must be set to the BARMAN_DATA_DIR environment variable
9+
barman_home = /var/lib/barman
10+
11+
; barman_lock_directory = %(barman_home)s
12+
compression = gzip
13+
configuration_files_directory = /etc/barman/barman.d
14+
last_backup_maximum_age = 1 week
15+
log_file = /var/lib/barman/barman.log
16+
log_level = DEBUG
17+
minimum_redundancy = 1
18+
network_compression = true
19+
retention_policy = RECOVERY WINDOW of 4 WEEKS
20+
; retention_policy_mode = auto
21+
reuse_backup = link
22+
streaming_archiver = on
23+
; wal_retention_policy = main

barman/etc/barman.d/pg.conf

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[$PRIMARY_NODE]
2+
; active = true
3+
; archiver = off
4+
; archiver_batch_size = 0
5+
; backup_directory = %(barman_home)s/%(name)s
6+
; backup_method = rsync
7+
; backup_options =
8+
; basebackup_retry_sleep = 30
9+
; basebackup_retry_times = 0
10+
; basebackups_directory = %(backup_directory)s/base
11+
; check_timeout = 30
12+
conninfo = host=$PRIMARY_NODE user=$BARMAN_USER dbname=postgres
13+
description = 'Test database'
14+
; disabled = false
15+
; errors_directory = %(backup_directory)s/errors
16+
; immediate_checkpoint = false
17+
; incoming_wals_directory = %(backup_directory)s/incoming
18+
; minimum_redundancy = 0
19+
; network_compression = false
20+
path_prefix = /usr/lib/postgresql/10
21+
; recovery_options =
22+
; retention_policy_mode = auto
23+
ssh_command = 'ssh postgres@$PRIMARY_NODE'
24+
slot_name = $BARMAN_SLOT_NAME
25+
; streaming_archiver = off
26+
; streaming_archiver_batch_size = 0
27+
; streaming_archiver_name = barman_receive_wal
28+
; streaming_backup_name = barman_streaming_backup
29+
streaming_conninfo = host=$PRIMARY_NODE user=$STREAMING_USER dbname=postgres
30+
; streaming_wals_directory = %(backup_directory)s/streaming
31+
; wal_retention_policy = main
32+
; wals_directory = %(backup_directory)s/wals

0 commit comments

Comments
 (0)