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 | |
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. ⚠️
-rw-r--r-- | tasks/postgres-cluster.yml | 6 | ||||
-rw-r--r-- | tasks/postgres-standby-barman.yml | 4 | ||||
-rw-r--r-- | tasks/postgres-standby-basebackup.yml | 4 | ||||
-rwxr-xr-x | templates/standby-barman-clone.sh.j2 (renamed from templates/standby-clone.sh.j2) | 0 | ||||
-rwxr-xr-x | templates/standby-pg_basebackup-clone.sh.j2 | 49 |
5 files changed, 60 insertions, 3 deletions
diff --git a/tasks/postgres-cluster.yml b/tasks/postgres-cluster.yml index a8e96c1..4f62d31 100644 --- a/tasks/postgres-cluster.yml +++ b/tasks/postgres-cluster.yml | |||
@@ -60,7 +60,11 @@ | |||
60 | 60 | ||
61 | - name: Add standby clone from barman script | 61 | - name: Add standby clone from barman script |
62 | include: postgres-standby-barman.yml | 62 | include: postgres-standby-barman.yml |
63 | when: postgres_barman_server is defined | 63 | when: postgres_barman_server is defined and postgres_primary.restore_barman_directory is defined |
64 | |||
65 | - name: Add standby clone from pg_basebackup | ||
66 | include: postgres-standby-basebackup.yml | ||
67 | when: postgres_primary and postgres_primary.pg_basebackup is defined | ||
64 | 68 | ||
65 | - name: Determine SSD or rotational disks | 69 | - name: Determine SSD or rotational disks |
66 | raw: 'lsblk --noheadings --nodeps --raw --output=rota | grep -q 1' | 70 | raw: 'lsblk --noheadings --nodeps --raw --output=rota | grep -q 1' |
diff --git a/tasks/postgres-standby-barman.yml b/tasks/postgres-standby-barman.yml index 6a94b75..fb71d32 100644 --- a/tasks/postgres-standby-barman.yml +++ b/tasks/postgres-standby-barman.yml | |||
@@ -11,8 +11,8 @@ | |||
11 | path: "{{ postgres_barman_path_prefix | default('~') }}" | 11 | path: "{{ postgres_barman_path_prefix | default('~') }}" |
12 | rsync_password_file: "{{ postgres_barman_rsync_enabled | ternary(' --password-file=/var/lib/postgresql/.rsync_pass ', '') }}" | 12 | rsync_password_file: "{{ postgres_barman_rsync_enabled | ternary(' --password-file=/var/lib/postgresql/.rsync_pass ', '') }}" |
13 | 13 | ||
14 | - name: Copy secondary script | 14 | - name: Copy secondary barman clone script |
15 | template: src=standby-clone.sh.j2 dest=/root/standby-clone-{{ postgres_version }}-{{ postgres_cluster_name }}.sh mode=0755 | 15 | template: src=standby-barman-clone.sh.j2 dest=/root/standby-clone-{{ postgres_version }}-{{ postgres_cluster_name }}.sh mode=0755 |
16 | 16 | ||
17 | - name: Copy rsync password file | 17 | - name: Copy rsync password file |
18 | copy: | 18 | copy: |
diff --git a/tasks/postgres-standby-basebackup.yml b/tasks/postgres-standby-basebackup.yml new file mode 100644 index 0000000..db52d55 --- /dev/null +++ b/tasks/postgres-standby-basebackup.yml | |||
@@ -0,0 +1,4 @@ | |||
1 | --- | ||
2 | - name: Copy secondary pg_basebackup clone script | ||
3 | template: src=standby-pg_basebackup-clone.sh.j2 dest=/root/standby-clone-{{ postgres_version }}-{{ postgres_cluster_name }}.sh mode=0755 | ||
4 | no_log: true | ||
diff --git a/templates/standby-clone.sh.j2 b/templates/standby-barman-clone.sh.j2 index 2db44b6..2db44b6 100755 --- a/templates/standby-clone.sh.j2 +++ b/templates/standby-barman-clone.sh.j2 | |||
diff --git a/templates/standby-pg_basebackup-clone.sh.j2 b/templates/standby-pg_basebackup-clone.sh.j2 new file mode 100755 index 0000000..ca34a05 --- /dev/null +++ b/templates/standby-pg_basebackup-clone.sh.j2 | |||
@@ -0,0 +1,49 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | # {{ ansible_managed }} | ||
3 | |||
4 | set -eo pipefail | ||
5 | |||
6 | CLUSTER_VERSION="{{ postgres_version }}" | ||
7 | CLUSTER_NAME="{{ postgres_cluster_name }}" | ||
8 | PRIMARY_CLUSTER_HOST="{{ postgres_primary.host }}" | ||
9 | PRIMARY_CLUSTER_PORT="{{ postgres_primary.port }}" | ||
10 | PRIMARY_CLUSTER_USER="{{ postgres_primary.replication_user }}" | ||
11 | PRIMARY_CLUSTER_PASSWORD="{{ postgres_primary.replication_password }}" | ||
12 | {% if postgres_primary.replication_dbname is defined %} | ||
13 | PRIMARY_CLUSTER_DBNAME=" dbname='{{ postgres_primary.replication_dbname }}'" | ||
14 | {% endif %} | ||
15 | |||
16 | read -p "You are about to clone the primary cluster (${PRIMARY_CLUSTER_HOST}:${PRIMARY_CLUSTER_PORT}) with pg_basebackup. This will replace the local ${CLUSTER_VERSION}/${CLUSTER_NAME} cluster. Are you sure? [y/n] " -r go | ||
17 | |||
18 | if [ "$go" != "y" ]; then | ||
19 | echo "Aborted." | ||
20 | exit 1 | ||
21 | fi | ||
22 | |||
23 | BACKUP_DATE=$(date +%s) | ||
24 | |||
25 | echo "Stopping PostgreSQL" | ||
26 | pg_ctlcluster "${CLUSTER_VERSION}" "${CLUSTER_NAME}" stop || true | ||
27 | |||
28 | echo "Cleaning up old cluster directory" | ||
29 | sudo -u postgres mv "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}"{,"_${BACKUP_DATE}"} | ||
30 | |||
31 | echo "Creating new directory" | ||
32 | sudo -u postgres mkdir -p "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}" | ||
33 | |||
34 | echo "Get backup from primary server" | ||
35 | sudo -u postgres \ | ||
36 | time "/usr/lib/postgresql/${CLUSTER_VERSION}/bin/pg_basebackup" --format=p -R \ | ||
37 | -d "host='${PRIMARY_CLUSTER_HOST}' port='${PRIMARY_CLUSTER_PORT}' user='${PRIMARY_CLUSTER_USER}' password='${PRIMARY_CLUSTER_PASSWORD}' ${PRIMARY_CLUSTER_DBNAME} sslmode=require" \ | ||
38 | -D "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}/" | ||
39 | |||
40 | echo "Restoring .conf and server certificate" | ||
41 | sudo -u postgres cp -a "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}_${BACKUP_DATE}/"{*.conf,server.crt,server.key} "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}/" | ||
42 | |||
43 | echo "Ensure rights are correcly set" | ||
44 | chown -R postgres:postgres "/var/lib/postgresql/${CLUSTER_VERSION}/" | ||
45 | chmod 0700 "/var/lib/postgresql/${CLUSTER_VERSION}/" | ||
46 | chmod -R o-rwx "/var/lib/postgresql/${CLUSTER_VERSION}/" | ||
47 | |||
48 | echo "Starting PostgreSQL" | ||
49 | sudo pg_ctlcluster "${CLUSTER_VERSION}" "${CLUSTER_NAME}" start | ||