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

if [ "$1" = "help" ]
then
    echo "Usage: ldap-dump-db [help|manual|force]"
    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`

# Read the required configuration
conf_file=/etc/ldap-backup.conf
if [ ! -e $conf_file ]
then
    echo "ERROR: missing $conf_file"
    exit 1
fi
source $conf_file

if [ "$DBID" = "" ]
then
    echo "ERROR: DBID missing"
    exit 1
fi
if [ "$DBDN" = "" ]
then
    echo "ERROR: DBDN missing"
    exit 1
fi

fullDB="${THIS_HOST%%.*}-${DBID}-${THIS_DATE}.ldif"
dbRoot="db-${DBID}.ldif"

echo "starting dump to /tmp/$fullDB ..."
/usr/sbin/slapcat -o ldif-wrap=no -b $DBDN > /tmp/$fullDB

echo "compressing $fullDB ..."
/bin/gzip $forceSW /tmp/$fullDB
cp -v /tmp/${fullDB}.gz /tmp/${dbRoot}.gz

if [ ! -e $OUT_DIR ]
then
    mkdir -v -p $OUT_DIR
fi
echo "Copying ${fullDB}.gz to $OUT_DIR ..."
cp -v $forceSW /tmp/${fullDB}.gz $OUT_DIR

##############################################################################
# Documentation
##############################################################################
DOCS=<<__END_OF_DOCS__

=head1 NAME

ldap-dump-db - Dump the LDAP database

=head1 SYNOPSIS

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

=head1 DESCRIPTION

This script dumps the LDAP data base in LDIF format and compresses it.
A date stamped copy of the file is also saved 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-backup.conf if it exists.  The
configuration file can be used to override values for the following
script variables:

    DBID                      - required, no default
    DBDN                      - required, the DN of the LDAP database
    OUT_DIR="/afs/.@cell/backup/ldap/${THIS_YEAR}/${THIS_MONTH}"
                              - the output directory
    THIS_DATE=`date +%Y%m%d`  - date stamp for output
    THIS_YEAR=`date +%Y`      - year stamp for output
    THIS_MONTH=`date +%m`     - month stamp for output
    THIS_HOST=`hostname`      - the host stamp for output file

=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__
