2 # openldap backup handler script for backupninja
5 getconf backupdir /var/backups/ldap
6 getconf conf /etc/ldap/slapd.conf
11 getconf method ldapsearch
17 [ -f $conf ] || fatal "slapd config file ($conf) not found"
18 [ -d $backupdir ] || mkdir -p $backupdir
19 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
21 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'`)
25 if [ "$ldif" == "yes" ]; then
27 [ -d $dumpdir ] || mkdir -p $dumpdir
29 if [ "$databases" == 'all' ]; then
30 dbcount=`grep '^database' $conf | wc -l`
31 let "dbcount = dbcount - 1"
32 databases=`seq 0 $dbcount`;
35 for db in $databases; do
36 if [ `expr index "$dbnum" "="` == "0" ]; then
37 # db is a number, get the suffix.
38 dbsuffix=${dbsuffixes[$db]/*:/}
42 # some databases don't have suffix (like monitor), skip these
43 if [ "$dbsuffix" == "" ]; then
47 if [ "$method" == "slapcat" ]; then
48 execstr="$SLAPCAT -f $conf -b $dbsuffix"
51 execstr="$LDAPSEARCH -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
52 [ -f "$passwordfile" ] || fatal "Password file $passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
56 if [ "$restart" == "yes" ]; then
57 debug "Shutting down ldap server..."
58 /etc/init.d/slapd stop
61 touch $dumpdir/$dbsuffix.ldif
62 if [ ! -f $dumpdir/$dbsuffix.ldif ]; then
63 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif"
66 output=`$execstr > $dumpdir/$dbsuffix.ldif`
68 if [ "$code" == "0" ]; then
70 info "Successfully finished ldif export of $dbsuffix"
73 warning "Failed ldif export of $dbsuffix"
75 if [ "$compress" == "yes" ]; then
76 output=`$GZIP -f "$dumpdir/$dbsuffix.ldif" 2>&1`
80 if [ "$restart" == "yes" ]; then
81 debug "Starting ldap server..."
82 /etc/init.d/slapd start