1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # openldap backup handler script for backupninja
6 getconf backupdir /var/backups/ldap
7 getconf conf /etc/ldap/slapd.conf
12 getconf method ldapsearch
20 if [ $ssl = 'yes' ]; then
27 # If vservers are configured, check that the ones listed in $vsnames do exist.
29 if [ $vservers_are_available = yes -a -n "$vsnames" ]; then
30 if [ "$vsnames" = all ]; then
31 vsnames="$found_vservers"
33 if ! vservers_exist "$vsnames" ; then
34 fatal "At least one of the vservers listed in vsnames ($vsnames) does not exist."
37 info "Using vservers '$vsnames'"
40 [ -z "$vsnames" ] || warning 'vservers support disabled in backupninja.conf, vsnames configuration line will be ignored'
43 [ "$restart" = yes -a "$method" = ldapsearch ] && warning 'restart option should not be used with the ldapsearch method.'
47 function make_backup() {
49 if [ -z "$vsname" ]; then
50 info "Running on host"
54 if ! vservers_running "$vsname"; then
55 error "vserver $vsname is not running!"
58 info "Running on vserver $vsname"
59 vdir="$VROOTDIR/$vsname"
60 vexec="$VSERVER $vsname exec"
63 dumpdir="`interpolate "$backupdir" "$vsname"`"
64 info "Backing up to dir '$dumpdir'"
66 [ -f "$vdir$conf" ] || fatal "slapd config file ($conf) not found"
67 [ -d "$dumpdir" ] || mkdir -p "$dumpdir"
68 [ -d "$dumpdir" ] || fatal "Backup directory '$dumpdir'"
70 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'`)
74 if [ "$ldif" == "yes" ]; then
75 if [ "$databases" == 'all' ]; then
76 dbcount=`grep '^database' "$vdir$conf" | wc -l`
77 let "dbcount = dbcount - 1"
78 databases=`seq 0 $dbcount`;
81 for db in $databases; do
82 if [ `expr index "$db" "="` == "0" ]; then
83 # db is a number, get the suffix.
84 dbsuffix=${dbsuffixes[$db]/*:/}
88 # some databases don't have suffix (like monitor), skip these
89 if [ "$dbsuffix" == "" ]; then
93 if [ "$method" == "slapcat" ]; then
94 execstr="$SLAPCAT -f $conf -b $dbsuffix"
97 if [ "$tls" == "yes" ]; then
100 if [ -n "$ldaphost" ]; then
101 execstr="$LDAPSEARCH $LDAPARGS -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
103 execstr="$LDAPSEARCH -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
105 [ -f "$vdir$passwordfile" ] || fatal "Password file $vdir$passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
109 if [ "$restart" == "yes" ]; then
110 debug "Shutting down ldap server..."
111 $vexec /etc/init.d/slapd stop
115 if [ "$compress" == "yes" ]; then
118 touch "$dumpdir/$dbsuffix.ldif$ext"
119 if [ ! -f "$dumpdir/$dbsuffix.ldif$ext" ]; then
120 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif$ext"
123 if [ "$compress" == "yes" ]; then
124 execstr="$execstr | $GZIP > \"$dumpdir/$dbsuffix.ldif.gz\""
126 execstr="$execstr > \"$dumpdir/$dbsuffix.ldif\""
128 # Run inside the vserver if needed
129 execstr="$vexec $execstr"
131 output=`su root -c "$execstr" 2>&1`
133 if [ "$code" == "0" ]; then
135 info "Successfully finished ldif export of $dbsuffix"
138 warning "Failed ldif export of $dbsuffix"
141 if [ "$restart" == "yes" ]; then
142 debug "Starting ldap server..."
143 $vexec /etc/init.d/slapd start
150 if [ $usevserver = yes ]; then
151 for vserver in $vsnames; do
152 make_backup "$vserver"