#!/bin/sh # Ce script se base sur l'utilitaire pg_lsclusters de Debian pour # lister les clusters de PostgreSQL actifs et les sauvegarder # via un simple pg_dumpall # Configuration # Le fichier sera nommé BASE_DIR/PREFIXEbasenameSUFFIXE BASE_DIR="/var/backups/postgresql" PREFIXE="sauv_pgsql" SUFFIXE=_$( date +%Y%m%d-%H%M ).sql.gz DUREE_DE_VIE=5 BIN_PG_DUMPALL="pg_dumpall" # Vérifications initiales if [ ! -d "$BASE_DIR" ]; then echo "ERREUR : répertoire de sauvegarde inexistant : $BASE_DIR ." >&2 exit 1 fi if ! which pg_lsclusters >/dev/null 2>&1; then echo "ERREUR : 'pg_lsclusters' inexistant." >&2 exit 1 fi # Sauvegarde des instances # note : le premier qui met un espace dans le nom de son cluster # se prend mon pied au c... pg_lsclusters -h | sed 's/[[:space:]]\+/\t/g' | while read line; do SAUV_PG_VERSION=$( printf "$line" | cut -f 1 ) SAUV_PG_PORT=$( printf "$line" | cut -f 3 ) SAUV_PG_NAME=$( printf "$line" | cut -f 2 ) SAUV_PG_STATUS=$( printf "$line" | cut -f 4 ) #BIN_PG_DUMPALL="/usr/lib/postgresql/$SAUV_PG_VERSION/bin/pg_dumpall" # Finalement, on laisse Debian gérer le choix du pg_dumpall # On liste les instances "online" ou "online,recovery" (= hot standby + streaming replication) if [ "$SAUV_PG_STATUS" = "online" ] || [ "$SAUV_PG_STATUS" = "online,recovery" ]; then su -l -c "$BIN_PG_DUMPALL -p $SAUV_PG_PORT" postgres | gzip -c > "$BASE_DIR"/"$PREFIXE"_"$SAUV_PG_NAME"_"$SAUV_PG_PORT"_"$SUFFIXE" fi done # Suppression des anciennes bases if [ "$1" = "--delete-olds" ]; then find $BASE_DIR -name "$PREFIXE*" -mtime +$DUREE_DE_VIE -print0 | xargs -n 200 -r -0 rm -f fi