ee46831092d10688be9d67d09401c7329346aeb5
[matthijs/upstream/backupninja-vserver.git] / 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          if [ "$compress" == "yes" ]; then
50             execstr="$SLAPCAT -f $conf -b $dbsuffix | $GZIP"
51          else
52             execstr="$SLAPCAT -f $conf -b $dbsuffix"
53          fi
54          debug "$execstr"
55       else
56          if [ "$compress" == "yes" ]; then
57             execstr="$LDAPSEARCH -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile | $GZIP"
58          else
59             execstr="$LDAPSEARCH -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
60          fi
61          [ -f "$passwordfile" ] || fatal "Password file $passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
62          debug "$execstr"
63       fi
64       if [ ! $test ]; then
65          if [ "$restart" == "yes" ]; then
66             debug "Shutting down ldap server..."
67             /etc/init.d/slapd stop
68          fi
69          
70          touch $dumpdir/$dbsuffix.ldif
71          if [ ! -f $dumpdir/$dbsuffix.ldif ]; then
72             fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif"
73          fi
74          
75          if [ "$compress" == "yes" ]; then
76             output=`$execstr > $dumpdir/$dbsuffix.ldif.gz`
77          else
78             output=`$execstr > $dumpdir/$dbsuffix.ldif`
79          fi
80          code=$?
81          if [ "$code" == "0" ]; then
82             debug $output
83             info "Successfully finished ldif export of $dbsuffix"
84          else
85             warning $output
86             warning "Failed ldif export of $dbsuffix"
87          fi
88          
89          if [ "$restart" == "yes" ]; then
90             debug "Starting ldap server..."
91             /etc/init.d/slapd start
92          fi
93       fi
94    done 
95 fi
96
97 return 0