#!/bin/bash
#
# This script dumps your Bacula 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 = bacula).
#  $2 is the user name with which to access the database
#     (default = bacula).
#  $3 is the password with which to access the database or "" if no password
#     (default "")
#
#  To read back a MySQL database use: 
#     cd /var/lib/bacula
#     rm -f /usr/bin/../var/bacula/*
#     mysql <bacula.sql
#
#  To read back a SQLite database use:
#     cd /var/lib/bacula
#     rm -f bacula.db
#     sqlite bacula.db <bacula.sql
#
#  To read back a PostgreSQL database use:
#     cd /var/lib/bacula
#     dropdb bacula
#     psql bacula <bacula.sql
#

RETVAL=0

bindir=/usr/bin

. /etc/sysconfig/bacula

cd /var/lib/bacula
rm -f bacula.sql

case "$BACULA_BACKEND" in
    postgresql)
        $bindir/pg_dump -U $2 $1 >$1.sql
        ;;
    sqlite)
        echo ".dump" | $bindir/sqlite $1.db >$1.sql
        ;;
    mysql)
        $bindir/mysqldump -u $2 -f --opt $1 >$1.sql
        ;;
    *)
        echo "Unknown database type"
        $RETVAL=1
        ;;
esac

exit $RETVAL
