#!/usr/bin/sh
#
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
# Copyright (C) 2013-2022 Bareos GmbH & Co. KG
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of version three of the GNU Affero General Public
# License as published by the Free Software Foundation and included
# in the file LICENSE.
#
# This program 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
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
# This script dumps your PostgreSQL Bareos catalog in ASCII format
#
# $1 is the Catalog Name resource you want to backup
set -e
set -u
#
# Source the Bareos config functions.
#
. /usr/lib/bareos/scripts/bareos-config-lib.sh

#
# Warn user if arguments are more than one
#
if [ $# -ne 1 ]; then
    warn "Script signature has changed: usage $0 CatalogName"
    exit 1
fi

catalog="$1"

set_postgresql_environment_variables "${catalog}"

PGOPTIONS="--client-min-messages=warning"
working_dir="$(get_working_dir)"
DUMP="${working_dir}/${PGDATABASE}.sql"

cd "${working_dir}"
rm -f "${DUMP}"
# Keep export line used on csh
export PGHOST PGPORT PGSOCKET PGDATABASE PGUSER PGPASSWORD PGOPTIONS DUMP
pg_dump --format=plain --encoding=SQL_ASCII --clean --file="${DUMP}" "${PGDATABASE}"

exit $?
