#!/bin/bash
#
# This file is part of vbackup.
#
# vbackup is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# vbackup is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with vbackup  If not, see <http://www.gnu.org/licenses/>.
#
# $Id$
#
# Description
#
#	Backup a mysql database
#

NAME="mysql"
VERSION="$PACKAGE_VERSION"
DESC="Backup mysql databases"
LICENSE="$PACKAGE_LICENSE"
COPYRIGHT="$PACKAGE_COPYRIGHT"
CONTACT="$PACKAGE_BUGREPORT"

# Display help
do_help()
{
	cat << _END
Configuration options:
	DATABASES	A space separated list of databases to backup.
			Set this to '-' to backup all databases. (required)
	MYUSER		The username to use to connect (required)
	PASSWORD	The password to use to connect (insecure)
	MYCNF		A my.cnf file to (also) read
	MYSQL		The path to mysql executable
	MYSQLDUMP	The path to mysqldump executable
	MYSQLDUMPEXTRA	Extra parameters to pass to MYSQLDUMP
	DESTDIR		The destination directory (required)
	CREATEDB	Include CREATE DATABASE statements (y/N)
_END
}

# Check configuration
# return: 0: ok, 1: error
do_check_conf()
{
	[ -z "$DATABASES" ] && h_error "Missing DATABASES" && return 1
	[ -z "$MYUSER" ] && h_error "Missing MYUSER" && return 1
	[ -z "$DESTDIR" ] && h_error "Missing DESTDIR" && return 1

	return 0
}

# Do backup
do_run()
{
	if [ "x$ABORT" = "x1" ] ; then
		return 0
	fi

	# Initializ
	MYSQL=${MYSQL:-mysql}
	MYSQLDUMP=${MYSQLDUMP:-mysqldump}

	PARAMS=()
	# This has to be before the user
	if [ ! "x$MYCNF" = "x" ] ; then
		PARAMS+=(--defaults-file=$MYCNF)
	fi

	PARAMS+=(-u "$MYUSER")

	if [ ! "x$PASSWORD" = "x" ] ;  then
		PARAMS+=(--password="$PASSWORD")
	fi


	for t in $MYSQL $MYSQLDUMP ; do
		if ! which $t > /dev/null ; then
			h_error "Could not find '$t'"
			return 1
		fi
	done

	# Ignored databases (regexp)
	IGNDB='^(information_schema|performance_schema|cond_instances)$'

	if [ "$DATABASES" = "-" ] ; then
		DATABASES=`$MYSQL "${PARAMS[@]}" \
				--disable-pager --batch \
				-se 'show databases' \
			| egrep -v "$IGNDB"`

		h_msg 6 "Databases to dump: $(echo $DATABASES)"
		if ! [ "$?" = 0 ] ; then
			return 1
		fi
	fi

	if [ ! -z "$CREATEDB" ] ; then
		# PARAMS+=(-B)
		PARAMS+=(--create-options)
	fi

	# Add routines
	PARAMS+=(-R)

	if ! [ -z "$MYSQLDUMPEXTRA" ] ; then
		PARAMS+=($MYSQLDUMP)
	fi

	ret=0
	for db in $DATABASES ; do
		h_msg 6 "Dumping: $db to $DESTDIR/$db.gz"
		if h_is_true $COMPRESS ; then
			$MYSQLDUMP "${PARAMS[@]}" \
				$db \
				2> >(h_filter 6 >&2) \
				| gzip > "$DESTDIR/$db.gz"
		else
			$MYSQLDUMP "${PARAMS[@]}" \
				$db \
				2> >(h_filter 6 >&2) \
				> "$DESTDIR/$db"
		fi
		if ! [ "$?" -eq 0 ] ; then
			h_msg 2 "Failed to backup $db"
			ret=1
		fi
	done

	return $ret
}


