1 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
2 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4 # openldap backup handler script for backupninja
7 getconf backupdir /var/backups/ldap
8 getconf conf /etc/ldap/slapd.conf
13 getconf method ldapsearch
20 if [ $ssl = 'yes' ]; then
28 [ -f $conf ] || fatal "slapd config file ($conf) not found"
29 [ -d $backupdir ] || mkdir -p $backupdir
30 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
32 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'`)
36 if [ "$ldif" == "yes" ]; then
38 [ -d $dumpdir ] || mkdir -p $dumpdir
40 if [ "$databases" == 'all' ]; then
41 dbcount=`grep '^database' $conf | wc -l`
42 let "dbcount = dbcount - 1"
43 databases=`seq 0 $dbcount`;
46 for db in $databases; do
47 if [ `expr index "$db" "="` == "0" ]; then
48 # db is a number, get the suffix.
49 dbsuffix=${dbsuffixes[$db]/*:/}
53 # some databases don't have suffix (like monitor), skip these
54 if [ "$dbsuffix" == "" ]; then
58 if [ "$method" == "slapcat" ]; then
59 execstr="$SLAPCAT -f $conf -b $dbsuffix"
62 if [ "$tls" == "yes" ]; then
65 if [ -n "$ldaphost" ]; then
66 execstr="$LDAPSEARCH $LDAPARGS -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
68 execstr="$LDAPSEARCH -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
70 [ -f "$passwordfile" ] || fatal "Password file $passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
74 if [ "$restart" == "yes" ]; then
75 debug "Shutting down ldap server..."
76 /etc/init.d/slapd stop
80 if [ "$compress" == "yes" ]; then
83 touch $dumpdir/$dbsuffix.ldif$ext
84 if [ ! -f $dumpdir/$dbsuffix.ldif$ext ]; then
85 fatal "Couldn't create ldif dump file: $dumpdir/$dbsuffix.ldif$ext"
88 if [ "$compress" == "yes" ]; then
89 execstr="$execstr | $GZIP --rsyncable > $dumpdir/$dbsuffix.ldif.gz"
91 execstr="$execstr > $dumpdir/$dbsuffix.ldif"
94 output=`su root -c "set -o pipefail ; $execstr" 2>&1`
96 if [ "$code" == "0" ]; then
98 info "Successfully finished ldif export of $dbsuffix"
101 warning "Failed ldif export of $dbsuffix"
104 if [ "$restart" == "yes" ]; then
105 debug "Starting ldap server..."
106 /etc/init.d/slapd start