#!/bin/sh #------------------------------------------------------------------------------ # /var/install/bin/ldapserver-backup-database - backup LDAP database # # Copyright (c) 2009-2023 The Eisfair Team, team(at)eisfair(dot)org # # Creation : 2009-11-17 jed # Last Update: $Id$ # # Usage: # ldapserver-backup-database - run in interactive mode. # # ldapserver-backup-database --help - show this help. # # ldapserver-backup-database -backupfile # - create ldif backup file in batch mode. # # ldapserver-backup-database -backuppath # - create ldif backup file in batch mode. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. #------------------------------------------------------------------------------ # read eislib . /var/install/include/eislib #exec 2>/tmp/ldapserver-backup-database-trace-$$.log #set -x # command line parameter batch=0 quiet=0 if [ $# -gt 0 ] then # read parameter(s) while [ $# -gt 0 ] do case $1 in -backupfile|--backupfile ) batch=1 ldap_backup_file="$2" shift; shift ;; -backuppath|--backuppath ) batch=1 ldap_backup_path="`echo "$2" | sed 's#/$##'`" shift; shift ;; -help|--help|-?|/? ) echo echo "Usage:" echo " ldapserver-backup-database - run in interactive mode" echo echo " ldapserver-backup-database --help - show this help" echo echo " ldapserver-backup-database -backupfile " echo " - create ldif backup file in batch mode" echo echo " ldapserver-backup-database -backuppath " echo " - create ldif backup file in batch mode" exit 1 ;; * ) break ;; esac done fi ldap_confpath=/etc/openldap ldap_datapath=/var/lib/openldap tmppath='/tmp' configfile=/etc/config.d/ldapserver ldapserver_logfile=${ldap_datapath}/ldapserver-ldap-backup.log exit_code=0 if [ -f ${configfile} ] then . ${configfile} if [ -z "${ldap_backup_path}" ] then if [ "${LDAPSERVER_BACKUP}" = 'yes' ] then ldap_backup_path="${LDAPSERVER_BACKUP_PATH}" else ldap_backup_path="${tmppath}" fi fi if [ -z "${ldap_backup_file}" ] then # backup path given, set default file name ldap_backup_file=${ldap_backup_path}/`echo ${LDAPSERVER_LDAP_BASEDN} | sed -e 's/dc= *//g' -e 's/[ ,]*//g'`-${EISDATE}-${EISTIME}.ldif fi if [ ${batch} -eq 0 ] then # interactive mode clrhome mecho --info "Backup LDAP database" mecho ldap_backup_file=$(/var/install/bin/ask "Please enter the backup file name (full path) [q]" "${ldap_backup_file}" "+") if [ "${ldap_backup_file}" = "q" -o "${ldap_backup_file}" = "Q" ] then exit 0 fi fi destpath="`dirname ${ldap_backup_file}`" if [ ${batch} -eq 1 -a ! -d "${destpath}" ] then # make sure that directory path exists in batch mode mkdir -p "${destpath}" fi if [ -d "${destpath}" ] then if [ -f ${ldap_backup_file} ] then if [ ${batch} -eq 0 ] then # interactive mode mecho --warn "The LDAP backup file '${ldap_backup_file}' already exists." mecho if /var/install/bin/ask "Do you want to overwrite it" "no" then rm -f ${ldap_backup_file} else mecho --error "LDAP database backup aborted." exit 1 fi else # batch mode mv ${ldap_backup_file} ${ldap_backup_file}.backup fi fi # check if directory path is accessible and writeble touch ${ldap_backup_file}.TEST 2>/dev/null if [ $? -eq 0 ] then # directory path is accessible and writeble, go on ... rm -f ${ldap_backup_file}.TEST # remove previous backup file rm -f ${ldapserver_logfile} echo "date: ${EISDATE} ${EISTIME}" > ${ldapserver_logfile} # shutdown server /usr/sbin/service stop ldapserver 2>&1 | /usr/bin/ansifilter >> ${ldapserver_logfile} echo >> ${ldapserver_logfile} # backup LDAP database /usr/sbin/slapcat -v -f ${ldap_confpath}/slapd.conf -l ${ldap_backup_file} >> ${ldapserver_logfile} 2>> ${ldapserver_logfile} ret=$? if [ ${ret} -eq 0 ] then if [ ${batch} -eq 0 ] then # interactive mode mecho "LDAP directory successfully safed to file '${ldap_backup_file}." fi else mecho --error "Error '${ret}' during backup of LDAP directory tree. Please try again!" exit_code=5 fi echo >> ${ldapserver_logfile} # start server /usr/sbin/service start ldapserver 2>&1 | /usr/bin/ansifilter >> ${ldapserver_logfile} if [ ${batch} -eq 0 ] then # interactive mode # check if show-doc.cui supports colors color='' frame='' if $(grep -qE "^MENU=['\"]/var/install/bin/show-menu['\"]" /etc/config.d/setup) then color='--nocolor' frame='--noframe' fi sleep 3 /var/install/bin/show-doc.cui ${color} ${frame} --follow --title ${ldapserver_logfile} ${ldapserver_logfile} fi else mecho --error "Destination directory '${destpath}' unaccessible!" exit_code=4 fi else mecho --error "Destination directory '${destpath}' doesn't exist!" exit_code=3 fi else mecho --error "Configuration file '${configfile}' doesn't exist!" exit_code=2 fi exit ${exit_code}