#!/bin/bash
#
# File: ldap-dump-config
# Description: This scripts dumps the cn=config configuration
# Author: Bill MacAllister <bill@ca-zephyr.org>
# Copyright: 2023 CZ Software

if [ "$1" = "help" ]
then
    echo "Usage: ldap-dump-config [help|manual]"
    exit 1
fi
if [ "$1" = "manual" ]
then
    pod2text $0
    exit 1
fi
if [ "$1" = "force" ]
then
    forceSW="-f"
fi

THIS_DATE=`date +%Y%m%d`
THIS_YEAR=`date +%Y`
THIS_MONTH=`date +%m`
OUT_DIR="/afs/.@cell/backup/ldap/${THIS_YEAR}/${THIS_MONTH}"
THIS_HOST=`hostname`
CONFIG_DB="${THIS_HOST%%.*}-${THIS_DATE}-config.ldif"

# Read a configuration file if there is one
conf_file=/etc/ldap-backup.conf
if [ -e $conf_file ]
then
    source $conf_file
fi

outFile="/tmp/cn-config.ldif"
echo "Writing to $outFile"
/usr/sbin/slapcat -o ldif-wrap=no -F /etc/ldap/slapd.d -b cn=config > $outFile

echo "Copying to archive directory ..."
if [ ! -e $OUT_DIR ]
then
    mkdir -v -p $OUT_DIR
fi
cp -v $forceSW $outFile $OUT_DIR/$CONFIG_DB

##############################################################################
# Documentation
##############################################################################

DOCS=<<__END_OF_DOCS__
=head1 NAME

ldap-dump-config - Dump the cn=config OpenLDAP configuration

=head1 SYNOPSIS

ldap-dump-config [help|manual|force]

=head1 DESCRIPTION

This script dumps the cn=config configuration in LDIF format to the
file /tmp/cn-config.ldif.  A date stamped copy of the file is also
save in the /afs/cz/backup/ldap/<year>/<month> AFS directory.

The local slapd server is NOT stopped during the dump.

The force action overwrites output files without prompting for 
confirmation.

=head1 CONFIGURATION

The script will source the file /etc/ldap-load.conf if it exists.  The
configuration file can be used to override values for the following
script variables:

    CONFIG_DB="${THIS_HOST%%.*}-${THIS_DATE}-config.ldif"
    OUT_DIR="/afs/.@cell/backup/ldap/${THIS_YEAR}/${THIS_MONTH}"
    THIS_DATE=`date +%Y%m%d`
    THIS_YEAR=`date +%Y`
    THIS_HOST=`hostname`

=head1 OPTIONS

=over 4

=item help

A short help message.

=item manual

This documentation.

=back

=head1 AUTHOR

Bill MacAllister <bill@ca-zephyr.org>

=head1 COPYRIGHT

Copyright 2023 CZ Software

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut
__END_OF_DOCS__
