Add a script to switch the primary PostgreSQL server

Usage: scripts/postgresql/switch_primary.sh <old_primary_hostname> <new_primary_hostname>
This commit is contained in:
Greg 2021-01-23 19:13:54 +01:00
parent 7e61e9cb45
commit 112eb903ec
1 changed files with 19 additions and 0 deletions

View File

@ -0,0 +1,19 @@
#!/bin/bash
set -e
if [[ $# -ne 2 ]] ; then
echo "Usage: scripts/postgresql/switch_primary.sh <old_primary_hostname> <new_primary_hostname>"
exit 1;
fi
old_primary_hostname=$1
new_primary_hostname=$2
new_primary_ip=`bundle exec knife node show $new_primary_hostname -a knife_zero.host | grep -E "([0-9]{1,3}[\.]){3}[0-9]{1,3}" -o`
echo "Switching primary to $new_primary_hostname ($new_primary_ip)"
bundle exec knife ssh name:$new_primary_hostname -a knife_zero.host "sudo su - postgres -c '/usr/lib/postgresql/12/bin/pg_ctl promote -D /var/lib/postgresql/12/main'"
echo "Replacing pg.kosmos.local host entries with $new_primary_ip"
bundle exec knife ssh roles:postgresql_client -a knife_zero.host "sudo sed -r \"s/^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\s(pg.kosmos.local)/$new_primary_ip\t\1/\" -i /etc/hosts"
echo "Stopping postgresql on $old_primary_hostname"
bundle exec knife ssh name:$old_primary_hostname -a knife_zero.host "sudo systemctl stop postgresql@12-main.service"