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