#!/bin/sh
#
# Use mysqldump to make a database backup.
#
# Usage: backup_db [output_file]
#
# If not output file is entered, format is MM-DD-YYYY.sql.gz
# 
# Part of the ModerMethod Standard Library
# 
# (C) Copyright 2002 ModernMethod, Inc.

# Pull the desired save filename from the args..
if [ $# -eq 0 ]; then
	output_fname=`date +%m-%d-%Y`.sql.gz
else
	output_fname=$1
fi

# If the filename ends in .gz, we gotta gzip..
echo $output_fname | egrep \.gz$ >/dev/null 
if [ $? -eq 1 ]
then
	do_gzip=0
else
	do_gzip=1
	echo - Using gzip compression
fi

shift

echo - Saving to $output_fname

# Setup a temporary file to backup to.
tmp_fname=/tmp/mysql_backup.$$

. `dirname $0`/parse_vars
load_config_file

if [ "$db_type" != "mysql" ]; then
	echo backup_db only s MySQL presently
	exit 1
fi

# Find a suitable MySQL command line client
for file in /usr/bin/mysqldump /usr/local/bin/mysqldump /usr/local/mysql/bin/mysqldump
do
	if [ -x $file ]; then
		mysqldump=$file
	fi
done

if ! $mysqldump --=$db_ --=$db_pw --host=$db_host $db_db $* > $tmp_fname
then
	echo - mysqldump failed
	exit 1
fi

if test $do_gzip -eq 1; then
	gzip -9 $tmp_fname
	mv $tmp_fname.gz $output_fname
else
	mv $tmp_fname $output_fname
fi
