#!/bin/sh
#
# Dump MySQL database contents into a file.
#
# Last modified: Feb 26, 2005
#
# Copyright (c) 2004-2005 Mariusz Zynel.
#
# This  software  is  FREE.  You can use  and/or  redistribute  it for any
# purpose in either,  modified, or unmodified form, under the terms of the
# GNU General Public License as published by the Free Software Foundation.
#
# The above copyright notice and this permission  notice shall be included
# in all copies or substantial portions of this software.
#
# THIS  SOFTWARE IS PROVIDED AS IS AND COME WITH NO WARRANTY  OF ANY KIND,
# EITHER  EXPRESSED OR IMPLIED.  IN NO EVENT WILL THE COPYRIGHT  HOLDER BE
# LIABLE FOR ANY DAMAGES RESULTING FROM THE USE OF THIS SOFTWARE.

SELF=`basename $0`

PATH=/usr/bin:/opt/cfw/bin

USER=root

if [ $# -eq 1 ]; then
    DATABASE=$1
else
    if [ $# -eq 2 ]; then
        DATABASE=$1
        TABLE=$2
    else
        if [ $# -eq 4 -a "$1" = "-u" ]; then
            USER=$2
            DATABASE=$3
            TABLE=$4
        else
            echo "Usage: $SELF [-u user] database [table]"
            exit 1;
        fi
    fi
fi

BACKUP=${DATABASE}${TABLE}.sql

stty -echo
echo "Enter MySQL user $USER password:"
read PASSWD
stty echo

echo "SET FOREIGN_KEY_CHECKS=0; SET UNIQUE_CHECKS=0; SET NAMES LATIN2;" > $BACKUP
echo >> $BACKUP
if mysqldump -u $USER -p$PASSWD --opt $DATABASE $TABLE >> $BACKUP
then
    echo "Database dumped to file: $BACKUP"
fi
