#!/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
#
#

NAME="openldap"
VERSION="$PACKAGE_VERSION"
DESC="Backup OpenLDAP database"
LICENSE="$PACKAGE_LICENSE"
COPYRIGHT="$PACKAGE_COPYRIGHT"
CONTACT="$PACKAGE_BUGREPORT"

# Display help
do_help()
{
	cat << _END
Configuration options:
	DESTDIR		The destination directory (required)
	CNCONFIG	Backup cn=config DB (yes/no - optional - default: no)
_END
}

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

	return 0
}

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

	SLAPCAT=${SLAPCAT:-slapcat}

	DST1="$DESTDIR/database.ldif"
	DST2="$DESTDIR/config.ldif"

	ret=0

	if h_is_true $COMPRESS ; then
		FILTER="gzip"
		SUFFIX=".gz"
	else
		FILTER="cat"
		SUFFIX=""
	fi

	DST1="$DST1$SUFFIX"
	DST2="$DST2$SUFFIX"

	h_msg 6 "Dumping LDAP database to $DST1"
	if ! $SLAPCAT -v | $FILTER > $DST1 ; then
		h_error "Failed to dump LDAP database"
		ret=1
	fi

	if h_is_true "$CNCONFIG" "no" ; then
		h_msg 6 "Dumping LDAP config (cn=config) to $DST2"
		if ! $SLAPCAT -s cn=config -v | $FILTER > $DST2 ; then
			h_error "Failed to dump LDAP config (cn=config)"
			ret=1
		fi
	fi

	return $ret
}

