Backup LDAP locally

#!/bin/bash
#Written Andrew Stringer 21/03/2012
#Purpose is to back up LDAP database.
#To maintain maximum compatability, slapcat is used to do a dump in ldif format
#which is text based to enable import to another LDAP server if required.

#This is intended to be run from cron, the output is picked up by bkup03


SLAPCAT=/usr/sbin/slapcat
DATE=`date  +%d-%m-%Y`

OUTPUTFILEPATH=/home/scope/LDAPbackup
OUTPUTFILE=${OUTPUTFILEPATH}/ldapbackup-${DATE}.cfg
FILEOWNER='scope:netops'

LDAPCONF=/etc/openldap/slapd.conf
LDAPBASE='dc=domain,dc=net'

SENDEMAIL=no
EMAILSUBJECT="inet01 - LDAP Backup"

#e-mail distribution list
dist1="me@domain.pri"

TMPMAILFILE=/tmp/LDAPBackup-$$
DEBUGLOG=/tmp/LDAPbackup-debug-$$

#Send test mail on 1st of the month
DATENUM=`date +%d`
#Test to force 1st of the month
#DATENUM=01
if [ $DATENUM == 01 ]
        then
        MONTHTEST=" - 1st of the month test email."
        SENDEMAIL=yes
fi


echo "To: ${dist1}" > ${TMPMAILFILE}
echo "From: LDAPbackup@inet01.domain.pri" >> ${TMPMAILFILE}
echo "Subject: ${EMAILSUBJECT} ${MONTHTEST} " >> ${TMPMAILFILE}

echo "Mail generated by /root/scripts/backupldap.sh" >> ${TMPMAILFILE}
echo "Start of debug log file" > ${DEBUGLOG}




#Do the deed!
echo "${SLAPCAT} -f ${LDAPCONF} -b ${LDAPBASE} > ${OUTPUTFILE}" >> ${DEBUGLOG}
${SLAPCAT} -f ${LDAPCONF} -b ${LDAPBASE} > ${OUTPUTFILE}

#change ownership so we can scp file off to remote server
#and tighten permissions to r-- --- ---
chown ${FILEOWNER} ${OUTPUTFILE}
chmod 400  ${OUTPUTFILE}



#Clean up old files older than 4 weeks
#echo "Housekeeping." >> ${TMPMAILFILE}
#echo "-------------" >> ${TMPMAILFILE}

#How old do the files need to be?
AGE=28

NUMFILES='zero'
NUMFILES=`find ${OUTPUTFILEPATH}/ -mtime +${AGE} | wc -l`

#echo " "
echo "Deleting these files (${NUMFILES}) as they are older than 4 weeks:-" >> ${TMPMAILFILE}


#Do the deed!
find ${OUTPUTFILEPATH} -mtime +${AGE} -exec ls -l {} \; >> ${TMPMAILFILE}
find ${OUTPUTFILEPATH} -mtime +${AGE} -exec rm -f {} \;


echo " " >> ${TMPMAILFILE}
echo "LDAP backup report." >> ${TMPMAILFILE}
echo "-------------------" >> ${TMPMAILFILE}
echo " " >> ${TMPMAILFILE}
echo " " >> ${TMPMAILFILE}
echo "Current backup files in ${OUTPUTFILEPATH}:-" >> ${TMPMAILFILE}
echo " " >> ${TMPMAILFILE}

ls -lrt ${OUTPUTFILEPATH}  >>${TMPMAILFILE}

echo " " >> ${TMPMAILFILE}



#Mail Out
if [  ${SENDEMAIL} == yes ]
then
        echo "Sending notification mail at `date +%H:%M:%S` UTC." >> ${TMPMAILFILE}
        echo " "  >> ${TMPMAILFILE}

        echo " " >> ${TMPMAILFILE}
        /bin/mailx -s "${EMAILSUBJECT} ${MONTHTEST}" ${dist1} < ${TMPMAILFILE}
else
        #don't send mail, so mail command is commented out. Left in for testing
        echo "Sending notification mail at `date +%H:%M:%S` UTC." >> ${TMPMAILFILE}
        #/bin/mailx -s "${EMAILSUBJECT} ${MONTHTEST} - Test!" ${dist1}  < ${TMPMAILFILE}
fi



rm ${TMPMAILFILE}

#Comment if you want to see debug messages.
rm ${DEBUGLOG}

exit 0
#EOF

rb/backupldap1.txt · Last modified: 15/08/2018 23:48 by andrew