#!/bin/bash DATE=$(date +%F) DIRBACKUP="/data/backups" DBUSER="postgres" get_leader() { curl -s 127.0.0.1:8008 | jq -j ".role" } if [ "$(get_leader)" != "master" ]; then echo "I'm not leader. Exit" exit 0 fi getDbName() { psql -lA -x -U ${DBUSER} -p 15432 -d postgres | grep Name | grep -v -P "template0|template1" | cut -d "|" -f 2 } DBLIST=$(getDbName) createBackup() { for db in $DBLIST; do echo "====================================" echo "START create backup database: ${db}" pg_dump -Fc -U ${DBUSER} -p 15432 -d "${db}" >"${DIRBACKUP}/${db}-${DATE}".sql bzip2 -9 "${DIRBACKUP}/${db}-${DATE}".sql echo "END database bzip2: ${db}" done pg_dumpall -p 15432 -U postgres --roles-only > "${DIRBACKUP}/roles-${DATE}".sql } [[ $(ls -A ${DIRBACKUP}) ]] 2>/dev/null && echo "dir ${DIRBACKUP} not empty" && exit 123 createBackup