#!/bin/sh
#
# BAREOS® - Backup Archiving REcovery Open Sourced
#
# Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
# Copyright (C) 2013-2014 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 Bareos catalog in ASCII format
# It works for MySQL, SQLite, and PostgreSQL
#
#  $1 is the name of the database to be backed up and the name
#     of the output file (default = bareos).
#  $2 is the user name with which to access the database
#     (default = bareos).
#  $3 is the password with which to access the database or "" if no password
#     (default ""). WARNING!!! Passing the password via the command line is
#     insecure and should not be used since any user can display the command
#     line arguments and the environment using ps.  Please consult your
#     MySQL or PostgreSQL manual for secure methods of specifying the
#     password.
#  $4 is the host on which the database is located
#     (default "")
#  $5 is the type of database
#

workging_dir=
db_type=
db_user=
db_passwd=
db_name=
db_host=

#
# Source the Bareos config functions.
#

. /usr/lib/bareos/scripts/bareos-config-lib.sh

if [ -f /etc/bareos/bareos-config-lib.sh ]; then
    . /etc/bareos/bareos-config-lib.sh
fi

[ -z $working_dir ] && working_dir=`get_working_dir`
default_db_type=`get_database_driver_default`

usage() {
    cat<<EOF
usage: $0 [-h]|[-t <sqlite3|mysql|postgresql|ingres> [-u] [-p] [-n] [-a]]

This script extract database content in a format suitable for backup.

OPTIONS:
  -h    Show this message
  -t    database type (sqlite3, mysql, postgresql, ingres)
  -u    database user
  -p    user password
  -n    database name
  -a    database host
EOF
}

while getopts "ht:p:u:n:a:" OPTION
do
    case $OPTION in
        h)
            usage
            exit 0
            ;;
        t)
            db_type=$OPTARG
            ;;
        p)
            db_passwd=$OPTARG
            ;;
        u)
            db_user=$OPTARG
            ;;
        n)
            db_name=$OPTARG
            ;;
        a)
            db_host=$OPTARG
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

#
# If no new db_type is gives use the default db_type.
#
if [ -z "${db_type}" ]; then
   db_type="${default_db_type}"
fi

cd ${working_dir}
rm -f ${db_name}.sql

bindir=`get_database_utility_path ${db_type}`
if [ ! -z "${bindir}" ]; then
   PATH="$bindir:$PATH"
fi

case ${db_type} in
  sqlite3)
    echo ".dump" | sqlite3 ${db_name}.db > ${db_name}.sql
    ;;
  mysql)
    if [ ! -z "${db_passwd}" ]; then
       MYSQLPASSWORD=" --password=${db_passwd}"
    else
       MYSQLPASSWORD=""
    fi
    if [ ! -z "${db_host}" ]; then
       MYSQLHOST=" --host=${db_host}"
    else
       MYSQLHOST=""
    fi
    mysqldump --no-tablespaces -u ${2}${MYSQLPASSWORD}${MYSQLHOST} -f --opt ${db_name} > ${db_name}.sql
    ;;
  postgresql)
    if [ ! -z "${db_passwd}" ]; then
      PGPASSWORD=${db_passwd}
      export PGPASSWORD
    fi
    if [ ! -z "${db_host}" ]; then
      PGHOST=" --host=${db_host}"
    else
      PGHOST=""
    fi
    # you could also add --compress for compression.  See man pg_dump
    pg_dump -c $PGHOST -U ${db_user} ${db_name} > ${db_name}.sql
    ;;
esac
#
#  To read back a MySQL database use:
#     cd /var/lib/bareos
#     rm -f ${BINDIR}/../var/bareos/*
#     mysql <bareos.sql
#
#  To read back a SQLite database use:
#     cd /var/lib/bareos
#     rm -f bareos.db
#     sqlite bareos.db <bareos.sql
#
#  To read back a PostgreSQL database use:
#     cd /var/lib/bareos
#     dropdb bareos
#     createdb bareos -T template0 -E SQL_ASCII
#     psql bareos <bareos.sql
#
