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
18 if [ $tls = 'yes' ]; then
26 [ -f $conf ] || fatal "slapd config file ($conf) not found"
27 [ -d $backupdir ] || mkdir -p $backupdir
28 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
30 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'`)
34 if [ "$ldif" == "yes" ]; then
36 [ -d $dumpdir ] || mkdir -p $dumpdir
38 if [ "$databases" == 'all' ]; then
39 dbcount=`grep '^database' $conf | wc -l`
40 let "dbcount = dbcount - 1"
41 databases=`seq 0 $dbcount`;
44 for db in $databases; do
45 if [ `expr index "$db" "="` == "0" ]; then
46 # db is a number, get the suffix.
47 dbsuffix=${dbsuffixes[$db]/*:/}
51 # some databases don't have suffix (like monitor), skip these
52 if [ "$dbsuffix" == "" ]; then
56 if [ "$method" == "slapcat" ]; then
57 execstr="$SLAPCAT -f $conf -b $dbsuffix"
59 if [ -n "$ldaphost" ]; then
60 execstr="$LDAPSEARCH -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
62 execstr="$LDAPSEARCH -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
64 [ -f "$passwordfile" ] || fatal "Password file $passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
68 if [ "$restart" == "yes" ]; then
69 debug "Shutting down ldap server..."
70 /etc/init.d/slapd stop
74 if [ "$compress" == "yes" ]; then
77 touch $dumpdir/$dbsuffix.ldif$ext
78 if [ ! -f $dumpdir/$dbsuffix.ldif$ext ]; then
79 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif$ext"
82 if [ "$compress" == "yes" ]; then
83 execstr="$execstr | $GZIP > $dumpdir/$dbsuffix.ldif.gz"
85 execstr="$execstr > $dumpdir/$dbsuffix.ldif"
88 output=`su root -c "$execstr" 2>&1`
90 if [ "$code" == "0" ]; then
92 info "Successfully finished ldif export of $dbsuffix"
95 warning "Failed ldif export of $dbsuffix"
98 if [ "$restart" == "yes" ]; then
99 debug "Starting ldap server..."
100 /etc/init.d/slapd start