#!/bin/bash
#
# This file is part of vbackup.
#
# vbackup is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# vbackup is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vbackup  If not, see <http://www.gnu.org/licenses/>.
#
# $Id: pgsql 3393 2012-03-06 21:00:03Z v13 $
#
# Description
#
#	Backup a PostgreSQL  database
#
# TODO:
#	Backup BLOBs too. (configuration option?)
#	Full DB backup (one dump) (?)
#

NAME="pgsql"
VERSION="$PACKAGE_VERSION"
DESC="Backup PostgreSQL databases"
LICENSE="$PACKAGE_LICENSE"
COPYRIGHT="$PACKAGE_COPYRIGHT"
CONTACT="$PACKAGE_BUGREPORT"

# Display help
do_help()
{
	cat << _END
Configuration options:
	DATABASES	A space separated list of databases to backup.
			Set this to '-' to backup all databases. (required)
	PGUSER		The username to use to connect
	PASSWORD	The password to use to connect
	PGHOST		The host to connect to (empty for local database)
	PGPORT		The port to connect to (empty for default port)
	PSQL		The path to psql executable
	PGDUMP		The path to pg_dump executable
	PGDUMPALL	The path to pg_dumpall executable
	SUUSER		Do an 'su' to this user (example: postgres)
	DESTDIR		The destination directory (required)
	CREATEDB	Include CREATE DATABASE statements (y/N)
	GLOBALS		Where to dump global objects (Default: "globals")
	ALL		Dump template[1-9] too (Y/n)
_END
}

# Check configuration
# return: 0: ok, 1: error
do_check_conf()
{
	[ -z "$DATABASES" ] && h_error "Missing DATABASES" && return 1
#	[ -z "$USER" ] && echo "Missing USER" && return 1
	[ -z "$DESTDIR" ] && h_error "Missing DESTDIR" && return 1

	return 0
}

# Do backup
do_run()
{
	if [ "x$ABORT" = "x1" ] ; then
		return 0
	fi

	# Initialize
	if [ -z "$PSQL" ] ; then
		PSQL="psql";
	fi

	if [ -z "$PGDUMP" ] ; then
		PGDUMP="pg_dump"
	fi

	if [ -z "$PGDUMPALL" ] ; then
		PGDUMPALL="pg_dumpall"
	fi

	if [ -z "$GLOBALS" ] ; then
		GLOBALS="globals"
	fi

	for t in $PSQL $PGDUMP ; do
		if ! which $t > /dev/null ; then
			h_error "Could not find '$t'"
			return 1
		fi
	done

	if ! [ -z "$PASSWORD" ] ; then
		export PGPASSWORD="$PASSWORD"
	fi

	P_CONN=""
	if ! [ -z "$PGUSER" ] ; then P_CONN="$P_CONN -U $PGUSER" ; fi
	if ! [ -z "$PGHOST" ] ; then P_CONN="$P_CONN -h $PGHOST" ; fi
	if ! [ -z "$PHPORT" ] ; then P_CONN="$P_CONN -p $PGPORT" ; fi

	# Extra parameters for dumping
	P_DUMPPARAMS=""
	if h_is_true "$CREATEDB" "1" ; then
		P_DUMPPARAMS="$P_PARAMS -C"
	fi

	if h_is_true "$ALL" "yes" ; then
		P_FILT1="| grep -v 'template0'"
	else
		P_FILT1="| grep -v 'template[0-9]'"
	fi

	if [ "$DATABASES" = "-" ] ; then
		CMD="$PSQL -F , -A $P_CONN -P tuples_only -P pager=off -l | \
		     grep ',.*,' | cut -f 1 -d, $P_FILT1"
		if [ -z "$SUUSER" ] ; then
			DATABASES=`$CMD`
		else
			DATABASES=`su - $SUUSER -c "$CMD"`
		fi
	fi

	FIRST=1
	for db in $DATABASES ; do
		CMD="$PGDUMP $P_CONN $P_DUMPPARAMS $db"

		if h_is_true $COMPRESS ; then
			h_msg 6 "Dumping: $db to $DESTDIR/$db.gz"
			if [ -z "$SUUSER" ] ; then
				$CMD | gzip > "$DESTDIR/$db.gz"
			else
				su - $SUUSER -c "$CMD" | gzip > "$DESTDIR/$db.gz"
			fi
		else
			h_msg 6 "Dumping: $db to $DESTDIR/$db"
			if [ -z "$SUUSER" ] ; then
				$CMD > "$DESTDIR/$db"
			else
				su - $SUUSER -c "$CMD" > "$DESTDIR/$db"
			fi
		fi
	done

	if [ ! "x$GLOBALS" = "x-" ] ; then
		CMD="$PGDUMPALL $P_CONN -g"
		if h_is_true $COMPRESS ; then
			h_msg 6 "Dumping globals to $DESTDIR/$GLOBALS.gz"
			if [ -z "$SUUSER" ] ; then
				$CMD | gzip > "$DESTDIR/$GLOBALS.gz"
			else
				su - $SUUSER -c "$CMD" | gzip \
					> "$DESTDIR/$GLOBALS.gz"
			fi
		else
			h_msg 6 "Dumping globals to $DESTDIR/$GLOBALS"
			if [ -z "$SUUSER" ] ; then
				$CMD > "$DESTDIR/$GLOBALS"
			else
				su - $SUUSER -c "$CMD" > "$DESTDIR/$GLOBALS"
			fi
		fi
	fi
}

