Select Git revision
-
Hahn Axel (hahn) authoredHahn Axel (hahn) authored
ldap.sh 3.91 KiB
#!/bin/bash
# ================================================================================
#
# Backup script to save slapd data
#
# --------------------------------------------------------------------------------
#
# Author: daniel.schueler@iml.unibe.ch
# Location: /root/bin/backup_ldap.sh
#
# --------------------------------------------------------------------------------
# 202_-__-__ ..... v1.0 detect config path
# 2021-12-13 ah v1.1 detect config path
# 2021-12-14 ah v1.2 detect sbin path to execute slapcat without path
# 2022-03-17 v1.3 WIP: add lines with prefix __DB__
# ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then
echo ERROR: you cannot start $(basename $0) directly
rc=$rc+1
exit 1
fi
# --------------------------------------------------------------------------------
# CONFIG
# --------------------------------------------------------------------------------
# detect sbin path (to execute slapcat without path)
for mydir in /sbin /usr/sbin
do
test -d $mydir && export PATH=$PATH:$mydir
done
# detect config path
LDAP_CONF_DIR_PATH=/something/unknown
for mydir in /etc/openldap/slapd.d /etc/ldap/slapd.d
do
test -d $mydir && LDAP_CONF_DIR_PATH=$mydir
done
# --------------------------------------------------------------------------------
# FUNCTIONS
# --------------------------------------------------------------------------------
# ----------------------------------------------------------------------
# ldap backup with slapcat
# param string DN
# param string name of output file
# ----------------------------------------------------------------------
function dump_ldap(){
DN=$1
DUMPFILE=$2
echo -n "__DB__$SERVICENAME backup $LDAP_CONF_DIR_PATH .. DN $DN ... "
slapcat -F $LDAP_CONF_DIR_PATH -b "$DN" -l "$DUMPFILE"
fetchrc >/dev/null
if [ $myrc -ne 0 ]; then
echo "ERROR during backup"
else
db._compressDumpfile "$DUMPFILE"
fi
}
# ----------------------------------------------------------------------
# run ldap backups
# ----------------------------------------------------------------------
function doLdapBackup(){
create_targetdir
echo ----- LDAP BACKUP CONFIG
# for cfgname in `ldapsearch -H ldap:// -x -s base -b "" -LLL "configContext" | grep "configContext" | cut -f 2 -d ":"`
for cfgname in $(ldapsearch -Y EXTERNAL -H ldapi:/// -s base -b '' -LLL configContext | grep "configContext" | cut -f 2 -d ":")
do
echo DN $cfgname
cfg2=$(echo $cfgname | sed "s#[\ =,]#_#g")
outfile=$(hostname)_ldap_olc_config__$(get_outfile ${cfg2}).ldif
dump_ldap "$cfgname" "$BACKUP_TARGETDIR/$outfile"
done
echo ----- LDAP DATA
#for cfgname in `ldapsearch -H ldap:// -x -s base -b "" -LLL "namingContexts" | grep "namingContexts" | cut -f 2 -d ":"`
for cfgname in $(ldapsearch -Y EXTERNAL -H ldapi:/// -s base -b "" -LLL "namingContexts" | grep "namingContexts" | cut -f 2 -d ":")
do
echo DN $cfgname
cfg2=`echo $cfgname | sed "s#[\ =,]#_#g"`
outfile=$(hostname)_ldap_data__$(get_outfile ${cfg2}).ldif
dump_ldap "$cfgname" "$BACKUP_TARGETDIR/$outfile"
done
echo
echo ----- DUMPS
ls -l "$BACKUP_TARGETDIR"/*$BACKUP_DATE*
}
function restoreByFile(){
echo "TODO :-/"
rc=$rc+1
}
# --------------------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------------------
# ----- check requirements
j_requireBinary "ldapsearch" 1
j_requireBinary "slapcat" 1
j_requireProcess "slapd" 1
ls $LDAP_CONF_DIR_PATH >/dev/null 2>&1
if [ $rc -ne 0 ]; then
rc=0
echo "__DB__$SERVICENAME SKIP: LDAP seems not to be here"
else
if [ "$1" = "restore" ]; then
echo
restoreByFile "${2}"
else
doLdapBackup
fi
fi
echo "__DB__$SERVICENAME INFO: $0 $* [ldap] final returncode rc=$rc"
# --------------------------------------------------------------------------------