systemadmin.es > Backup > Backup OpenLDAP

Backup OpenLDAP

En OpenLDAP dependiendo del backend que utilizamos para la base de datos tenemos que utilizar un método de backup diferente.

Para el caso de bdb y hdb podemos utilizar slapcat sin tenener que parar el daemon slapd, pero para mdb no podemos utilizar slapcat pero podemos utilizar mdb_copy con el qual deberemos hacer una copia del datafile en lugar de obtener sus registros.

Mediante el siguiente script podemos hacer backup de la configuración de OpenLDAP (cn=config) y de sus bases de datos con los backends mdb, bdb y hdb:

#!/bin/bash

DESTINATION=/backup

function backup_config
{
	SLAPCAT=$(which slapcat 2>/dev/null)
	if [ -z "$SLAPCAT" ];
	then
		echo "slapcat not found"
	else
		mkdir -p  $DESTINATION/$BACKUPTS
		
		$SLAPCAT -b cn=config > $DESTINATION/$BACKUPTS/config.ldif

		if [ $? -eq 0 ];
		then
			echo OPENLDAPBACKUP: config OK
		else
			echo OPENLDAPBACKUP: FAILED: config
		fi

	fi
}

function backup_bdb_hdb
{
	for DB in $($LDAPSEARCH -LLL -Y EXTERNAL -H ldapi:/// -s sub -b cn=config '(|(olcDatabase=hdb)(olcDatabase=bdb))' dn 2>/dev/null | awk '{ print $NF }')
	do
		echo $DB
		SUFFIX=$($LDAPSEARCH -LLL -Y EXTERNAL -H ldapi:/// -s sub -b "$DB" olcSuffix 2>/dev/null | grep ^olcSuffix | awk '{ print $NF }')
		echo $SUFFIX

		mkdir -p "$DESTINATION/$BACKUPTS/$DB"

		SLAPCAT=$(which slapcat 2>/dev/null)
		if [ -z "$SLAPCAT" ];
		then
			echo "slapcat not found"
		else
			$SLAPCAT -b $SUFFIX > $DESTINATION/$BACKUPTS/$SUFFIX.ldif
			
			if [ $? -eq 0 ];
			then
				echo OPENLDAPBACKUP: $DB OK
			else
				echo OPENLDAPBACKUP: FAILED: $DB
			fi
		fi
	done
}

function backup_mdb
{

	for DB in $($LDAPSEARCH -LLL -Y EXTERNAL -H ldapi:/// -s sub -b cn=config '(olcDatabase=mdb)' dn 2>/dev/null | awk '{ print $NF }')
	do
		DATADIR=$($LDAPSEARCH -LLL -Y EXTERNAL -H ldapi:/// -s sub -b $DB '(&(objectclass=olcMdbConfig)(olcDatabase=mdb))' olcDbDirectory 2>/dev/null | grep "^olcDbDirectory" | awk '{ print $NF }')

		if [ -z "$DATADIR" ];
		then
			echo "MDB: no DATADIR found"
		else
			MDBCOPY=$(which mdb_copy 2>/dev/null)
			if [ -z "$MDBCOPY" ];
			then
				echo "mdb_copy not found, please install lmdb"
			else
	
				mkdir -p "$DESTINATION/$BACKUPTS/$DB"
			
				$MDBCOPY "$DATADIR" "$DESTINATION/$BACKUPTS/$DB"
		
				if [ $? -eq 0 ];
				then
					echo OPENLDAPBACKUP: $DB OK
				else
					echo OPENLDAPBACKUP: FAILED: $DB
				fi
			fi
		fi
	done	
}

LDAPSEARCH=$(which ldapsearch 2>/dev/null)
if [ -z "$LDAPSEARCH" ];
then
	echo "ldapsearch not found"
	exit 1
fi

BACKUPTS=$(date +%Y%m%d%H%M)

mkdir -p $DESTINATION/$BACKUPTS

backup_config

backup_bdb_hdb

backup_mdb

Deja un comentario:

XHTML - Tags permitidos:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>