diff options
author | Paul B <paul@bonaud.fr> | 2020-05-28 18:41:49 +0200 |
---|---|---|
committer | Paul B <paul@bonaud.fr> | 2020-06-05 14:37:21 +0200 |
commit | b722248ed7c4a7aba2b047855b983d72a9725209 (patch) | |
tree | c76ddecb3b0434e45f481e8b0585e3b62ae31bb0 /templates/standby-barman-clone.sh.j2 | |
parent | 9e6f9658fbf5db657a2dd8465bf0ae4f41da161e (diff) | |
download | ansible-postgresql-role-b722248ed7c4a7aba2b047855b983d72a9725209.tar.gz ansible-postgresql-role-b722248ed7c4a7aba2b047855b983d72a9725209.tar.zst ansible-postgresql-role-b722248ed7c4a7aba2b047855b983d72a9725209.zip |
standby-clone: Allow cloning of standby server with pg_basebackup
Right now the role assumes you have a base backup available with
Barman. However if you don't have an initial barman backup you might
want to clone the primary server directly to setup your standby
server.
This PR adds a new `primary.pg_basebackup` option to the cluster
configuration which if enabled (set to `true`) will create a
`/root/standby-clone-{{ postgres_version }}-{{ postgres_cluster_name }}.sh`
script on the standby server which helps to initialise a standby
server.
⚠️ Breaking change: the current role behavior which creates a cloning
script fetching the initial backup from barman will not be
enabled by default anymore. You will need to add the new
`primary.restore_barman_directory` option in your role
configuration to do so. ⚠️
Diffstat (limited to 'templates/standby-barman-clone.sh.j2')
-rwxr-xr-x | templates/standby-barman-clone.sh.j2 | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/templates/standby-barman-clone.sh.j2 b/templates/standby-barman-clone.sh.j2 new file mode 100755 index 0000000..2db44b6 --- /dev/null +++ b/templates/standby-barman-clone.sh.j2 | |||
@@ -0,0 +1,45 @@ | |||
1 | #!/bin/bash | ||
2 | # {{ ansible_managed }} | ||
3 | |||
4 | set -eo pipefail | ||
5 | |||
6 | BARMAN_DATABASE=$1 | ||
7 | BARMAN_BACKUP_VERSION=$2 | ||
8 | |||
9 | if [[ -z $BARMAN_DATABASE || -z $BARMAN_BACKUP_VERSION ]] | ||
10 | then | ||
11 | echo "Usage : $0 BARMAN_DATABASE BARMAN_BACKUP_VERSION" >&2 | ||
12 | echo " Example: $0 25kv 20161118T002405" >&2 | ||
13 | exit 1 | ||
14 | fi | ||
15 | |||
16 | BACKUP_DATE=`date +%s` | ||
17 | |||
18 | echo "accept key if necessary" | ||
19 | sudo -u postgres ssh barman@{{ postgres_barman_server }} echo "" | ||
20 | |||
21 | echo Stopping PostgreSQL | ||
22 | pg_ctlcluster {{ postgres_version }} {{ postgres_cluster_name }} stop || true | ||
23 | |||
24 | echo Cleaning up old cluster directory | ||
25 | sudo -u postgres mv /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}{,_$BACKUP_DATE} | ||
26 | |||
27 | echo Creating new directory | ||
28 | sudo -u postgres mkdir -p /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }} | ||
29 | |||
30 | echo Get previous backup from backups server | ||
31 | sudo -u postgres \ | ||
32 | time rsync --progress -pvia --exclude='*.conf' --exclude='server.crt' --exclude='server.key' --delete {{ rsync_options }} \ | ||
33 | {{ barman_remote_url }}/$BARMAN_DATABASE/base/$BARMAN_BACKUP_VERSION/data/ \ | ||
34 | /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}/ | ||
35 | |||
36 | echo Restoring .conf and server certificate | ||
37 | sudo -u postgres cp -a /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}_$BACKUP_DATE/{*.conf,server.crt,server.key} /var/lib/postgresql/{{ postgres_version }}/{{ postgres_cluster_name }}/ | ||
38 | |||
39 | echo Ensure rights are correcly set | ||
40 | chown -R postgres:postgres /var/lib/postgresql/{{ postgres_version }}/ | ||
41 | chmod 0700 /var/lib/postgresql/{{ postgres_version }}/ | ||
42 | chmod -R o-rwx /var/lib/postgresql/{{ postgres_version }}/ | ||
43 | |||
44 | echo Starting PostgreSQL | ||
45 | sudo pg_ctlcluster {{ postgres_version }} {{ postgres_cluster_name }} start | ||