1
0
Fork 0
scripts-admin-quickndirty-p.../oldies/script_multi-pg-reutilisation-ancien-master.sh

67 lines
2.9 KiB
Bash
Executable file

#!/bin/sh
# Script en version alpha-brouillon
# À n'utiliser qu'en connaissance de cause et avec
# une parfaite maîtrise du fonctionnement de la SR
# config sur MAITRE inutile ici
MAITRE=""
ESCLAVE="a.b.c.d"
set -e
# on ne peut pas faire le | while read avec du ssh dans la boucle ??
#for NOM_INSTANCE in $( pg_lsclusters -h | sed 's/[[:space:]]\+/ /g' | cut -d ' ' -f 2 ); do
for NOM_INSTANCE in $@; do
# Récupération de la ligne 'pg_lsclusters' complète
LINE="$( pg_lsclusters -h | sed 's/[[:space:]]\+/ /g' | egrep "^[0-9.]+ $NOM_INSTANCE " )"
if [ -z "$LINE" ]; then
echo "ERREUR: instance '$NOM_INSTANCE' inexistante." >&2
exit 1
fi
# Découpage des éléments de ladite ligne
VERSION_POSTGRESQL="$( echo "$LINE" | cut -d ' ' -f 1 )"
POSTGRESQL_PORT="$( echo "$LINE" | cut -d ' ' -f 3 )"
STATUT="$( echo "$LINE" | cut -d ' ' -f 4 )"
POSTGRESQL_PGDATA="$( echo "$LINE" | cut -d ' ' -f 6 )"
# On ne s'occupe que des instances "online" pures
if [ "$( echo "$LINE" | cut -d ' ' -f 4 )" != "online" ]; then
echo "NOTICE: on ignore l'instance '$NOM_INSTANCE' dont le statut ('$STATUT') n'est pas 'online'."
continue
fi
# On vérifie que l'instance en face est bien éteinte
if [ "$( ssh root@$ESCLAVE pg_lsclusters -h | grep -w "$NOM_INSTANCE" | grep online | wc -l )" -ne 0 ]; then
echo "NOTICE: l'instance '$NOM_INSTANCE' semble toujours allumée sur le serveur distant. On l'ignore."
continue
fi
# ...et on vérifie que le fichier recovery.done est dispo
if ! su -c "ssh postgres@$ESCLAVE test -f $POSTGRESQL_PGDATA/recovery.done" postgres; then
echo "NOTICE: le fichier recovery.done ne semble pas dispo pour l'instance distante '$NOM_INSTANCE'."
continue
fi
echo "INFO: resynchro de l'instance '$NOM_INSTANCE'..."
# Marquage
echo "INFO: rsync..."
echo "SELECT pg_start_backup('resynchro_maitre', true);" | su -c "psql -p $POSTGRESQL_PORT -v ON_ERROR_STOP=1" postgres
# Resynchro des fichiers
sleep 1
su -c "rsync -aHc --exclude=/postmaster.pid --exclude=/postmaster.opts --exclude=/server.key --exclude=/server.crt --exclude=/recovery.conf --exclude=/recovery.done --exclude=/archives_from_master --exclude=lost+found /var/lib/postgresql/$VERSION_POSTGRESQL/$NOM_INSTANCE/ postgres@$ESCLAVE:/var/lib/postgresql/$VERSION_POSTGRESQL/$NOM_INSTANCE/" postgres
# Vidange du dossier 'archives_from_master' distant, par prudence
su -c "ssh postgres@$ESCLAVE rm -rvf $POSTGRESQL_PGDATA/archives_from_master/*" postgres
sleep 1
echo "SELECT pg_stop_backup();" | su -c "psql -p $POSTGRESQL_PORT -v ON_ERROR_STOP=1" postgres
# Renommage du fichier recovery.conf distant pour activation
echo "INFO: renommage recovery.done distant en .conf..."
su -c "ssh postgres@$ESCLAVE mv -v $POSTGRESQL_PGDATA/recovery.done $POSTGRESQL_PGDATA/recovery.conf" postgres
# Démarrage instance distante
ssh root@$ESCLAVE pg_ctlcluster "$VERSION_POSTGRESQL" "$NOM_INSTANCE" start
done
echo "The end."