From 5b4a875767fe32c46838abb730c729fe53702951 Mon Sep 17 00:00:00 2001
From: Adam Thorn <alt36@cam.ac.uk>
Date: Thu, 8 Jul 2021 14:44:37 +0100
Subject: [PATCH] Add a script to do a postgres backup via pg_dump

At present we use myriad one-off per host scripts to do a pg_dump,
and they all do (or probably should do) the same thing. In combination
with setting options in the host's backup config file, I think
this single script covers all our routine pg backups.
---
 ROOT/DEBIAN/control                           |  2 +-
 .../lib/chem-zfs-backup-server/pg-dump-script | 53 +++++++++++++++++++
 2 files changed, 54 insertions(+), 1 deletion(-)
 create mode 100755 ROOT/usr/lib/chem-zfs-backup-server/pg-dump-script

diff --git a/ROOT/DEBIAN/control b/ROOT/DEBIAN/control
index 7aadd35..8b29adb 100644
--- a/ROOT/DEBIAN/control
+++ b/ROOT/DEBIAN/control
@@ -3,6 +3,6 @@ Priority: optional
 Section: otherosfs
 Maintainer: Chemistry COs <support@ch.cam.ac.uk>
 Architecture: all
-Version: 0.9-ch82
+Version: 0.9-ch83
 Depends: zfs-dkms, postgresql-13 | postgresql-9.5 | postgresql-9.4 , liblockfile-simple-perl, libdbi-perl, libjson-perl, libzfs-perl-chem, libnet-openssh-perl, libdbd-pg-perl, mbuffer, rsync, nfs-kernel-server, pv, libwww-curl-perl
 Description: a backup system using ZFS (repository 'backup-scheduler')
diff --git a/ROOT/usr/lib/chem-zfs-backup-server/pg-dump-script b/ROOT/usr/lib/chem-zfs-backup-server/pg-dump-script
new file mode 100755
index 0000000..8fb9bde
--- /dev/null
+++ b/ROOT/usr/lib/chem-zfs-backup-server/pg-dump-script
@@ -0,0 +1,53 @@
+#!/bin/bash
+set -xv
+# We want any failure to get passed back to the script
+# which called us. Rather than carefully keeping track 
+# of return codes, we bail on error
+set -e
+
+HOSTNAME=$1
+SRCDIR=$2
+ZFSTARGET=$3
+
+CONFDIR=/etc/chem-zfs-backup-server/zfs-rsync.d
+
+TAG=${SRCDIR//\//.}
+
+# The config file must, at a minimum, define PGDUMP_DATABASE.
+# It can also define PGDUMP_EXTRA_OPTIONS to be passed to the
+# pg_dump command later in the script
+. $CONFDIR/${HOSTNAME}_${TAG}
+
+if [ -z "$PGDUMP_DATABASE" ] ; then
+  echo PGDUMP_DATABASE is not set
+  exit 1
+fi
+
+SSHUSER=${SSHUSER:-root}
+
+# Script runs with set -e, so we ensure we tidy up our TEMPDIR on exit
+# no matter how/when the script exits.
+tidytmp() {
+  [ -n "$TEMPDIR" ] && ssh ${SSHUSER}@${HOSTNAME} " [ -d $TEMPDIR ] && rm -r $TEMPDIR "
+}
+trap tidytmp 0
+
+unset PGPASSFILE
+TEMPDIR=$(ssh ${SSHUSER}@${HOSTNAME} mktemp -d /tmp/pgXXXXX)
+
+MOUNT=$(zfs get -H -ovalue mountpoint $ZFSTARGET)
+BACKUP_DEST=$MOUNT/pgdumps/pgdumps-master/
+
+mkdir -p $BACKUP_DEST
+
+# we need this to exist for the rsync part of the backup, so the rsync returns successfully
+ssh ${SSHUSER}@${HOSTNAME} mkdir -p /var/empty
+
+# Dump database to temporary directory. We ensure we exit 1 here on failure
+# so a) the script  exits due to set -e, then b) the error bubbles up to the 
+# script which called us.
+ssh ${SSHUSER}@${HOSTNAME} pg_dump $PGDUMP_EXTRA_OPTIONS --host localhost --blobs --clean --user _pgbackup $PGDUMP_DATABASE -Fd -f $TEMPDIR/pgdump || exit 1
+
+# Rsync to backup destination
+rsync -av --checksum --delete --no-times ${SSHUSER}@${HOSTNAME}:$TEMPDIR/pgdump/ $BACKUP_DEST
+# TEMPDIR will be removed on exit by tidytmp()
-- 
GitLab