diff --git a/scripts/postgresql b/scripts/postgresql index 79f9cae..cdb59af 100755 --- a/scripts/postgresql +++ b/scripts/postgresql @@ -3,36 +3,59 @@ restoreDir=/etc/restore pgsqlDir="$restoreDir/postgresql" +function readConfig() { + while read -r -a vals + do + [[ "${vals[*]}" =~ ^#.*$ ]] && continue + [[ -z "${vals[0]}" ]] && continue + if [[ "${vals[0]}" -ge 10 ]]; then + echo "${vals[@]}" + fi + done < postgresql.cfg +} + +function runBackups() { + local vals + local version + local cluster + + while read -r -a vals + do + version="${vals[0]}" + cluster="${vals[1]}" + + echo "Backing up PostgreSQL $version - $cluster" + pg_backupcluster "$version" "$cluster" createdirectory || return $? + sudo -u postgres pg_backupcluster "$version" "$cluster" basebackup || return $? + sudo -u postgres pg_backupcluster "$version" "$cluster" dump || return $? + sudo -u postgres pg_backupcluster "$version" "$cluster" expirebasebackups 1 || return $? + sudo -u postgres pg_backupcluster "$version" "$cluster" expiredumps 1 || return $? + done < <(readConfig) +} + hook_before() { - if [[ -d "$pgsqlDir" ]]; then - rm -rf "$pgsqlDir" || exit 1 - fi - - state=$(nc 127.0.0.1 5400) + if [[ -d "$pgsqlDir" ]]; then + rm -rf "$pgsqlDir" || exit 1 + fi - if [[ "$state" != "MASTER" ]]; then - #backupDate=$(date +"%Y-%m-%d") - - mkdir -p "$pgsqlDir" || exit 2 - /usr/local/sbin/pg_backup -c /etc/postgresql/9.6/main/pg_backup.config - fi + runBackups || exit $? } hook_after() { - rm -rf "$pgsqlDir" || exit 1 + rm -rf "$pgsqlDir" || exit 1 } hook_fail() { - : + : } hook_final() { - : + : } case "$1" in - before) hook_before || exit $?;; - after) hook_after || exit $?;; - fail) hook_fail || exit $?;; - finally) hook_final || exit $?;; +before) hook_before || exit $? ;; +after) hook_after || exit $? ;; +fail) hook_fail || exit $? ;; +finally) hook_final || exit $? ;; esac diff --git a/templates/config/postgresql.cfg b/templates/config/postgresql.cfg new file mode 100644 index 0000000..f5074f4 --- /dev/null +++ b/templates/config/postgresql.cfg @@ -0,0 +1,4 @@ +# Configuration for pg_backupcluster stylebackups. +# Format per line: VERSION CLUSTER +# Example: 10 main +10 main