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
19 if [ $ssl = 'yes' ]; then
27 [ -f $conf ] || fatal "slapd config file ($conf) not found"
28 [ -d $backupdir ] || mkdir -p $backupdir
29 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
31 dbsuffixes=(`@AWK@ 'BEGIN {OFS=":"} /[:space:]*^database[:space:]*\w*/ {db=$2}; /^[:space:]*suffix[:space:]*\w*/ {if (db=="bdb"||db=="hdb"||db="ldbm") print db,$2}' $conf|@SED@ -e 's/[" ]//g'`)
35 if [ "$ldif" == "yes" ]; then
37 [ -d $dumpdir ] || mkdir -p $dumpdir
39 if [ "$databases" == 'all' ]; then
40 dbcount=`grep '^database' $conf | wc -l`
41 let "dbcount = dbcount - 1"
42 databases=`seq 0 $dbcount`;
45 for db in $databases; do
46 if [ `expr index "$db" "="` == "0" ]; then
47 # db is a number, get the suffix.
48 dbsuffix=${dbsuffixes[$db]/*:/}
52 # some databases don't have suffix (like monitor), skip these
53 if [ "$dbsuffix" == "" ]; then
57 if [ "$method" == "slapcat" ]; then
58 execstr="$SLAPCAT -f $conf -b $dbsuffix"
61 if [ "$tls" == "yes" ]; then
64 if [ -n "$ldaphost" ]; then
65 execstr="$LDAPSEARCH $LDAPARGS -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
67 execstr="$LDAPSEARCH -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
69 [ -f "$passwordfile" ] || fatal "Password file $passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
73 if [ "$restart" == "yes" ]; then
74 debug "Shutting down ldap server..."
75 /etc/init.d/slapd stop
79 if [ "$compress" == "yes" ]; then
82 touch $dumpdir/$dbsuffix.ldif$ext
83 if [ ! -f $dumpdir/$dbsuffix.ldif$ext ]; then
84 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif$ext"
87 if [ "$compress" == "yes" ]; then
88 execstr="$execstr | $GZIP > $dumpdir/$dbsuffix.ldif.gz"
90 execstr="$execstr > $dumpdir/$dbsuffix.ldif"
93 output=`su root -c "$execstr" 2>&1`
95 if [ "$code" == "0" ]; then
97 info "Successfully finished ldif export of $dbsuffix"
100 warning "Failed ldif export of $dbsuffix"
103 if [ "$restart" == "yes" ]; then
104 debug "Starting ldap server..."
105 /etc/init.d/slapd start