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 ]; 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'
47 if [ -z "$vsname" ]; then
48 info "Running on host"
52 info "Running on vserver $vsname"
53 vdir="$VROOTDIR/$vsname"
54 vexec="$VSERVER $vsname exec"
57 [ -f $vdir$conf ] || fatal "slapd config file ($conf) not found"
58 [ -d $backupdir ] || mkdir -p $backupdir
59 [ -d $backupdir ] || fatal "Backup directory '$backupdir'"
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'`)
65 if [ "$ldif" == "yes" ]; then
67 [ -d $dumpdir ] || mkdir -p $dumpdir
69 if [ "$databases" == 'all' ]; then
70 dbcount=`grep '^database' $vdir$conf | wc -l`
71 let "dbcount = dbcount - 1"
72 databases=`seq 0 $dbcount`;
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]/*:/}
82 # some databases don't have suffix (like monitor), skip these
83 if [ "$dbsuffix" == "" ]; then
87 if [ "$method" == "slapcat" ]; then
88 execstr="$SLAPCAT -f $conf -b $dbsuffix"
91 if [ "$tls" == "yes" ]; then
94 if [ -n "$ldaphost" ]; then
95 execstr="$LDAPSEARCH $LDAPARGS -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
97 execstr="$LDAPSEARCH -H $URLBASE://$ldaphost -x -L -b ""$dbsuffix"" -D ""$binddn"" -y $passwordfile"
99 [ -f "$vdir$passwordfile" ] || fatal "Password file $vdir$passwordfile not found. When method is set to ldapsearch, you must also specify a password file."
103 if [ "$restart" == "yes" ]; then
104 debug "Shutting down ldap server..."
105 $vexec /etc/init.d/slapd stop
109 if [ "$compress" == "yes" ]; then
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"
117 if [ "$compress" == "yes" ]; then
118 execstr="$execstr | $GZIP > $dumpdir/$dbsuffix.ldif.gz"
120 execstr="$execstr > $dumpdir/$dbsuffix.ldif"
122 # Run inside the vserver if needed
123 execstr="$vexec $execstr"
125 output=`su root -c "$execstr" 2>&1`
127 if [ "$code" == "0" ]; then
129 info "Successfully finished ldif export of $dbsuffix"
132 warning "Failed ldif export of $dbsuffix"
135 if [ "$restart" == "yes" ]; then
136 debug "Starting ldap server..."
137 $vexec /etc/init.d/slapd start
144 if [ $usevserver = yes ]; then
145 for vserver in $vsnames; do