#!/bin/sh
#
# 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 "")
#
#
cd /var/lib/bacula
rm -f bacula.sql
if test xsqlite = xsqlite ; then
  echo ".dump" | /usr/bin/sqlite $1.db >$1.sql
else
  if test xmysql = xsqlite ; then
    if test $# -gt 2; then
      MYSQLPASSWORD=" --password=$3"
    else
      MYSQLPASSWORD=""
    fi
    /usr/bin/mysqldump -u $2$MYSQLPASSWORD -f --opt $1 >$1.sql
  else			      
    if test xpostgresql = xsqlite ; then
      if test $# -gt 2; then
        PGPASSWORD=$3
		export PGPASSWORD
      fi
      exec /usr/bin/pg_dump -U $2 $1 >$1.sql
    else
      echo ".dump" | /usr/bin/sqlite3 $1.db >$1.sql
    fi
  fi
fi
#
#  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
#     createdb bacula
#     psql bacula <bacula.sql
#
