#!/bin/bash

# This script monitors and fixes the health of the BDB files. -jjt 20030202

CMDDIR=/opt/BerkeleyDB/bin/
BDB1DIR=/opt/openldap-clients-servers/var/openldap-data/bdb1
LOG1DIR=/opt/openldap-clients-servers/var/openldap-data/bdb1
ARC1DIR=/var/tmp/checkpointing/archive-bdb1
BDB2DIR=/opt/openldap-clients-servers/var/openldap-data/bdb2
LOG2DIR=/opt/openldap-clients-servers/var/openldap-data/bdb2
ARC2DIR=/var/tmp/checkpointing/archive-bdb2
LOGFILE=/var/tmp/checkpointing/fulllog.log
TMPDIR=/var/tmp
LOCKFILE=/var/tmp/checkpointing/lockfile



if [ -f "$LOCKFILE" ]
then
        exit 9
fi


touch $LOCKFILE

if [ "$LOGNAME" = "root" ]
then
        echo "`date`: Root Enabled." >> $LOGFILE
        #
        # BDB1: checkpoint
        #
        cd $BDB1DIR
        BEFORE=`/opt/BerkeleyDB/bin/db_archive`
        for BB in $BEFORE
        do
                echo "`date`: BDB1: Moving  (before): $BB" >> $LOGFILE
                mv $LOG1DIR/$BB $ARC1DIR
        done
        if [ "$1" != "-n" ]
        then
                /opt/BerkeleyDB/bin/db_checkpoint -1v >> $LOGFILE
        fi
        AFTER=`/opt/BerkeleyDB/bin/db_archive`
        for AA in $AFTER
        do
                echo "`date`: BDB1: Moving (after): $AA" >> $LOGFILE
                mv $LOG1DIR/$AA $ARC1DIR
        done
        #
        # BDB2: checkpoint
        #
        cd $BDB2DIR
        BEFORE=`/opt/BerkeleyDB/bin/db_archive`
        for BB in $BEFORE
        do
                echo "`date`: BDB2: Moving  (before): $BB" >> $LOGFILE
                mv $LOG2DIR/$BB $ARC2DIR
        done
        if [ "$1" != "-n" ]
        then
                /opt/BerkeleyDB/bin/db_checkpoint -1v >> $LOGFILE
        fi
        AFTER=`/opt/BerkeleyDB/bin/db_archive`
        for AA in $AFTER
        do
                echo "`date`: BDB2: Moving (after): $AA" >> $LOGFILE
                mv $LOG2DIR/$AA $ARC2DIR
        done
else
        echo "You must be root to use this script. -jjt"
        rm -f $LOCKFILE
        exit 9
fi

rm -f $LOCKFILE
