FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 5b4a8757 authored by Dr Adam Thorn's avatar Dr Adam Thorn
Browse files

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.
parent e01d7ebc
No related branches found
No related tags found
No related merge requests found
......@@ -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')
#!/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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment