#!/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 ""). 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 "")
#
#
BINDIR=

cd /var/bacula/working
rm -f $1.sql
if test xsqlite = xbdb ; then
  echo ".dump" | ${BINDIR}/sqlite $1.db >$1.sql
else
  if test xmysql = xbdb ; then
    if test $# -gt 2; then
      MYSQLPASSWORD=" --password=$3"
    else
      MYSQLPASSWORD=""
    fi
    if test $# -gt 3; then
      MYSQLHOST=" --host=$4"
    else
      MYSQLHOST=""
    fi
    ${BINDIR}/mysqldump -u ${2}${MYSQLPASSWORD}${MYSQLHOST} -f --opt $1 >$1.sql
  else			      
    if test xpostgresql = xbdb ; then
      if test $# -gt 2; then
	PGPASSWORD=$3
	export PGPASSWORD
      fi
      if test $# -gt 3; then
	PGHOST=" --host=$4"
      else
	PGHOST=""
      fi
      exec ${BINDIR}/pg_dump -c $PGHOST -U $2 $1 >$1.sql
    else
      echo ".dump" | ${BINDIR}/sqlite3 $1.db >$1.sql
    fi
  fi
fi
#
#  To read back a MySQL database use: 
#     cd /var/bacula/working
#     rm -f ${BINDIR}/../var/bacula/*
#     mysql <bacula.sql
#
#  To read back a SQLite database use:
#     cd /var/bacula/working
#     rm -f bacula.db
#     sqlite bacula.db <bacula.sql
#
#  To read back a PostgreSQL database use:
#     cd /var/bacula/working
#     dropdb bacula
#     createdb bacula
#     psql bacula <bacula.sql
#


syntax highlighted by Code2HTML, v. 0.9.1