aboutsummaryrefslogtreecommitdiffhomepage
path: root/templates/standby-pg_basebackup-clone.sh.j2
diff options
context:
space:
mode:
authorPaul B <paul@bonaud.fr>2020-06-05 14:39:43 +0200
committerPaul B <paul@bonaud.fr>2020-06-05 14:39:43 +0200
commit0ca1cf044b84bdae8c47e2d764715dbd5e7e9d69 (patch)
treec76ddecb3b0434e45f481e8b0585e3b62ae31bb0 /templates/standby-pg_basebackup-clone.sh.j2
parent9e6f9658fbf5db657a2dd8465bf0ae4f41da161e (diff)
parentb722248ed7c4a7aba2b047855b983d72a9725209 (diff)
downloadansible-postgresql-role-master.tar.gz
ansible-postgresql-role-master.tar.zst
ansible-postgresql-role-master.zip
Merge branch 'standby-clone-pg_basebackup'HEADmaster
Diffstat (limited to 'templates/standby-pg_basebackup-clone.sh.j2')
-rwxr-xr-xtemplates/standby-pg_basebackup-clone.sh.j249
1 files changed, 49 insertions, 0 deletions
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
4set -eo pipefail
5
6CLUSTER_VERSION="{{ postgres_version }}"
7CLUSTER_NAME="{{ postgres_cluster_name }}"
8PRIMARY_CLUSTER_HOST="{{ postgres_primary.host }}"
9PRIMARY_CLUSTER_PORT="{{ postgres_primary.port }}"
10PRIMARY_CLUSTER_USER="{{ postgres_primary.replication_user }}"
11PRIMARY_CLUSTER_PASSWORD="{{ postgres_primary.replication_password }}"
12{% if postgres_primary.replication_dbname is defined %}
13PRIMARY_CLUSTER_DBNAME=" dbname='{{ postgres_primary.replication_dbname }}'"
14{% endif %}
15
16read -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
18if [ "$go" != "y" ]; then
19 echo "Aborted."
20 exit 1
21fi
22
23BACKUP_DATE=$(date +%s)
24
25echo "Stopping PostgreSQL"
26pg_ctlcluster "${CLUSTER_VERSION}" "${CLUSTER_NAME}" stop || true
27
28echo "Cleaning up old cluster directory"
29sudo -u postgres mv "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}"{,"_${BACKUP_DATE}"}
30
31echo "Creating new directory"
32sudo -u postgres mkdir -p "/var/lib/postgresql/${CLUSTER_VERSION}/${CLUSTER_NAME}"
33
34echo "Get backup from primary server"
35sudo -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
40echo "Restoring .conf and server certificate"
41sudo -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
43echo "Ensure rights are correcly set"
44chown -R postgres:postgres "/var/lib/postgresql/${CLUSTER_VERSION}/"
45chmod 0700 "/var/lib/postgresql/${CLUSTER_VERSION}/"
46chmod -R o-rwx "/var/lib/postgresql/${CLUSTER_VERSION}/"
47
48echo "Starting PostgreSQL"
49sudo pg_ctlcluster "${CLUSTER_VERSION}" "${CLUSTER_NAME}" start