1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # openldap backup handler script for backupninja
6 getconf backupdir /var/backups/ldap
7 getconf conf /etc/ldap/slapd.conf
12 getconf method ldapsearch
18 [ -f $conf ] || fatal "slapd config file ($conf) not found"
19 [ -d $backupdir ] || mkdir -p $backupdir
20 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
22 dbsuffixes=(`awk 'BEGIN {OFS=":"} /[:space:]*^database[:space:]*\w*/ {db=$2}; /^[:space:]*suffix[:space:]*\w*/ {if (db=="bdb"||db=="ldbm") print db,$2}' $conf|sed -e 's/[" ]//g'`)
26 if [ "$ldif" == "yes" ]; then
28 [ -d $dumpdir ] || mkdir -p $dumpdir
30 if [ "$databases" == 'all' ]; then
31 dbcount=`grep '^database' $conf | wc -l`
32 let "dbcount = dbcount - 1"
33 databases=`seq 0 $dbcount`;
36 for db in $databases; do
37 if [ `expr index "$dbnum" "="` == "0" ]; then
38 # db is a number, get the suffix.
39 dbsuffix=${dbsuffixes[$db]/*:/}
43 # some databases don't have suffix (like monitor), skip these
44 if [ "$dbsuffix" == "" ]; then
48 if [ "$method" == "slapcat" ]; then
49 execstr="$SLAPCAT -f $conf -b $dbsuffix"
52 execstr="$LDAPSEARCH -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
53 [ -f "$passwordfile" ] || fatal "Password file $passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
57 if [ "$restart" == "yes" ]; then
58 debug "Shutting down ldap server..."
59 /etc/init.d/slapd stop
62 touch $dumpdir/$dbsuffix.ldif
63 if [ ! -f $dumpdir/$dbsuffix.ldif ]; then
64 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif"
67 output=`$execstr > $dumpdir/$dbsuffix.ldif`
69 if [ "$code" == "0" ]; then
71 info "Successfully finished ldif export of $dbsuffix"
74 warning "Failed ldif export of $dbsuffix"
76 if [ "$compress" == "yes" ]; then
77 output=`$GZIP -f "$dumpdir/$dbsuffix.ldif" 2>&1`
81 if [ "$restart" == "yes" ]; then
82 debug "Starting ldap server..."
83 /etc/init.d/slapd start