06e4e288e14de25823067458548ab7f32835cc70
[matthijs/upstream/backupninja-vserver.git] / 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 hotcopy no
11
12 # hot copy is not yet supported
13
14 status="ok"
15
16 [ -f $conf ] || fatal "slapd config file ($conf) not found"
17 [ -d $backupdir ] || mkdir -p $backupdir
18 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
19
20 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'`)
21
22 ## LDIF DUMP
23
24 if [ "$ldif" == "yes" ]; then
25         dumpdir="$backupdir"
26         [ -d $dumpdir ] || mkdir -p $dumpdir
27
28         if [ "$databases" == 'all' ]; then
29                 dbcount=`grep '^database' $conf | wc -l`
30                 let "dbcount = dbcount - 1"
31                 databases=`seq 0 $dbcount`;
32         fi      
33         
34         for db in $databases; do
35                 if [ `expr index "$dbnum" "="` == "0" ]; then
36                         # db is a number, get the suffix.
37                         dbsuffix=${dbsuffixes[$db]/*:/}
38                 else
39                         dbsuffix=$db
40                 fi
41                 # some databases don't have suffix (like monitor), skip these
42                 if [ "$dbsuffix" == "" ]; then
43                         continue;
44                 fi
45                 touch $dumpdir/$dbsuffix.ldif
46                 if [ ! -f $dumpdir/$dbsuffix.ldif ]; then
47                         fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif"
48                 fi
49                 execstr="$SLAPCAT -f $conf -b $dbsuffix -l $dumpdir/$dbsuffix.ldif"
50                 debug 0 "$execstr"
51                 if [ ! $test ]; then
52                         output=`$execstr`
53                         code=$?
54                         if [ "$code" == "0" ]; then
55                                 debug 0 $output
56                                 debug 1 "Successfully finished ldif export of $dbsuffix"
57                         else
58                                 debug 2 $output
59                                 debug 2 "Failed ldif export of $dbsuffix"
60                         fi
61                         if [ "$compress" == "yes" ]; then
62                                 output=`$GZIP -f "$dumpdir/$dbsuffix.ldif" 2>&1`
63                                 debug 0 $output
64                         fi
65                 fi
66         done    
67 fi
68
69 return 0