ldap: Add support for vservers.
[matthijs/upstream/backupninja-vserver.git] / handlers / ldap.in
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 getconf ldaphost
16 getconf ssl yes
17 getconf tls no
18 getconf vsnames
19
20 if [ $ssl = 'yes' ]; then
21    URLBASE="ldaps"
22 else
23    URLBASE="ldap"
24 fi
25
26 ### VServers
27 # If vservers are configured, check that the ones listed in $vsnames do exist.
28 local usevserver=no
29 if [ $vservers_are_available = yes ]; then
30    if [ "$vsnames" = all ]; then
31       vsnames="$found_vservers"
32    else
33       if ! vservers_exist "$vsnames" ; then
34             fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
35       fi
36    fi
37    info "Using vservers '$vsnames'"
38    usevserver=yes
39 else
40    [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'
41 fi
42
43 status="ok"
44
45 make_backup() {
46    vsname=$1
47    if [ -z "$vsname" ]; then
48       info "Running on host"
49       vdir=""
50       vexec=""
51    else
52       info "Running on vserver $vsname"
53       vdir="$VROOTDIR/$vsname"
54       vexec="$VSERVER $vsname exec"
55    fi
56
57    [ -f $vdir$conf ] || fatal "slapd config file ($conf) not found"
58    [ -d $backupdir ] || mkdir -p $backupdir
59    [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
60
61    dbsuffixes=(`@AWK@ 'BEGIN {OFS=":"} /[:space:]*^database[:space:]*\w*/ {db=$2}; /^[:space:]*suffix[:space:]*\w*/ {if (db=="bdb"||db=="hdb"||db="ldbm") print db,$2}' $vdir$conf|@SED@ -e 's/[" ]//g'`)
62
63    ## LDIF DUMP
64
65    if [ "$ldif" == "yes" ]; then
66       dumpdir="$backupdir"
67       [ -d $dumpdir ] || mkdir -p $dumpdir
68       
69       if [ "$databases" == 'all' ]; then
70          dbcount=`grep '^database' $vdir$conf | wc -l`
71          let "dbcount = dbcount - 1"
72          databases=`seq 0 $dbcount`;
73       fi        
74       
75       for db in $databases; do
76          if [ `expr index "$db" "="` == "0" ]; then
77                            # db is a number, get the suffix.
78             dbsuffix=${dbsuffixes[$db]/*:/}
79          else
80             dbsuffix=$db
81          fi
82                    # some databases don't have suffix (like monitor), skip these
83          if [ "$dbsuffix" == "" ]; then
84             continue;
85          fi
86          
87          if [ "$method" == "slapcat" ]; then
88             execstr="$SLAPCAT -f $conf -b $dbsuffix"
89          else
90             LDAPARGS=""
91             if [ "$tls" == "yes" ]; then
92                LDAPARGS="-ZZ"
93             fi
94             if [ -n "$ldaphost" ]; then
95                execstr="$LDAPSEARCH $LDAPARGS -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
96             else
97                execstr="$LDAPSEARCH -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
98             fi
99             [ -f "$vdir$passwordfile" ] || fatal "Password file $vdir$passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
100             debug "$execstr"
101          fi
102          if [ ! $test ]; then
103             if [ "$restart" == "yes" ]; then
104                debug "Shutting down ldap server..."
105                $vexec /etc/init.d/slapd stop
106             fi
107             
108             ext=
109             if [ "$compress" == "yes" ]; then
110                ext=".gz"
111             fi
112             touch $dumpdir/$dbsuffix.ldif$ext
113             if [ ! -f $dumpdir/$dbsuffix.ldif$ext ]; then
114                fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif$ext"
115             fi
116             
117             if [ "$compress" == "yes" ]; then
118                execstr="$execstr | $GZIP > $dumpdir/$dbsuffix.ldif.gz"
119             else
120                execstr="$execstr > $dumpdir/$dbsuffix.ldif"
121             fi
122             # Run inside the vserver if needed
123             execstr="$vexec $execstr"
124             debug "$execstr"
125             output=`su root -c "$execstr" 2>&1`
126             code=$?
127             if [ "$code" == "0" ]; then
128                debug $output
129                info "Successfully finished ldif export of $dbsuffix"
130             else
131                warning $output
132                warning "Failed ldif export of $dbsuffix"
133             fi
134             
135             if [ "$restart" == "yes" ]; then
136                debug "Starting ldap server..."
137                $vexec /etc/init.d/slapd start
138             fi
139          fi
140       done      
141    fi
142 }
143
144 if [ $usevserver = yes ]; then
145    for vserver in $vsnames; do
146       make_backup $vserver
147    done
148 else
149    make_backup ""
150 fi
151
152 return 0