Added the "Emacs comment line" on top of every shell file.
[matthijs/upstream/backupninja.git] / handlers / ldap
1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 #
3 # openldap backup handler script for backupninja
4 #
5
6 getconf backupdir /var/backups/ldap
7 getconf conf /etc/ldap/slapd.conf
8 getconf databases all
9 getconf compress yes
10 getconf ldif yes
11 getconf restart no
12 getconf method ldapsearch
13 getconf passwordfile
14 getconf binddn
15
16 status="ok"
17
18 [ -f $conf ] || fatal "slapd config file ($conf) not found"
19 [ -d $backupdir ] || mkdir -p $backupdir
20 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
21
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'`)
23
24 ## LDIF DUMP
25
26 if [ "$ldif" == "yes" ]; then
27         dumpdir="$backupdir"
28         [ -d $dumpdir ] || mkdir -p $dumpdir
29
30         if [ "$databases" == 'all' ]; then
31                 dbcount=`grep '^database' $conf | wc -l`
32                 let "dbcount = dbcount - 1"
33                 databases=`seq 0 $dbcount`;
34         fi      
35         
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]/*:/}
40                 else
41                         dbsuffix=$db
42                 fi
43                 # some databases don't have suffix (like monitor), skip these
44                 if [ "$dbsuffix" == "" ]; then
45                         continue;
46                 fi
47
48                 if [ "$method" == "slapcat" ]; then
49                         execstr="$SLAPCAT -f $conf -b $dbsuffix"
50                         debug "$execstr"
51                 else
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."
54                         debug "$execstr"
55                 fi
56                 if [ ! $test ]; then
57                         if [ "$restart" == "yes" ]; then
58                                 debug "Shutting down ldap server..."
59                                 /etc/init.d/slapd stop
60                         fi
61
62                         touch $dumpdir/$dbsuffix.ldif
63                         if [ ! -f $dumpdir/$dbsuffix.ldif ]; then
64                                 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif"
65                         fi
66
67                         output=`$execstr > $dumpdir/$dbsuffix.ldif`
68                         code=$?
69                         if [ "$code" == "0" ]; then
70                                 debug $output
71                                 info "Successfully finished ldif export of $dbsuffix"
72                         else
73                                 warning $output
74                                 warning "Failed ldif export of $dbsuffix"
75                         fi
76                         if [ "$compress" == "yes" ]; then
77                                 output=`$GZIP -f "$dumpdir/$dbsuffix.ldif" 2>&1`
78                                 debug $output
79                         fi
80
81                         if [ "$restart" == "yes" ]; then
82                                 debug "Starting ldap server..."
83                                 /etc/init.d/slapd start
84                         fi
85                 fi
86         done    
87 fi
88
89 return 0