#!/bin/bash # LDAP Backup Cron Script # 26 Nov 2012 # NDP, LLC # Set the name of the LDAP instance export LDAPINSTANCE=tao # set the number of hourly backups to keep export QUOTA=72 # set the path to the native LDAP backup command (i.e. db2bak) export BKUPCMD=/usr/lib64/dirsrv/slapd-$LDAPINSTANCE/db2bak if [ ! -x "$BKUPCMD" ]; then die "Backup command $BKUPCMD does not exist or is not executable" fi # set the location of db2bak backups export BKUPSRCDIR=/var/lib/dirsrv if [ ! -d "$BKUPSRCDIR" ]; then die "Backup source location $BKUPSRCDIR does not exist or is not a directory" fi # set the location where to transfer backups export BKUPTGTDIR=/data/backups/ldap/$LDAPINSTANCE if [ ! -d "$BKUPSTGTDIR" ]; then warn "Backup target location $BKUPTGTDIR does not exist or is not a directory. If possible it will be created." fi # Set the path and args for copy command (i.e. rsync vs cp) export COPYCMD=`which rsync` export COPYARG="-avp" if [ ! -x "$COPYCMD" ]; then die "Backup command $COPYCMD does not exist or is not executable" fi # die function to echo a message and exit function die () { echo "[FATAL] - $@ ...exiting." exit 1 } # warn function for non-catastrophic errors function warn () { echo "[WARN] - $@." } # info function for conditional verbose output function info () { echo "[INFO] - $@" } # truncate function to remove old files in a directory function truncate () { if [ ! -d "$@" ]; then die "$@ is not a directory that can be trimmed as per quota." fi pushd $@ > /dev/null 2>&1 || die "cannot pushd $BKUPSRCDIR/slapd-$LDAPINSTANCE/bak/" info "Maximum number of files to retain in $@: $QUOTA" LOGCOUNT=`ls -1 | wc -l` info "Current number of backup files: $LOGCOUNT" while (( $LOGCOUNT >= $QUOTA )) do oldfile=`ls -1t | tail -1` info "Truncating old backup: $oldfile " rm -rf $oldfile ((LOGCOUNT = LOGCOUNT - 1)) done popd >/dev/null } # Run the builtin backup script, which will backup to the /var filesystem $BKUPCMD # Copy the changes to the dedicated backup disks if (! -d "$BKUPTGTDIR"); then mkdir -p $BKUPTGTDIR || die "Cannot create backup target: $BKUPTGTDIR" fi $COPYCMD $COPYARG $BKUPSRCDIR/slapd-$LDAPINSTANCE/bak/* $BKUPTGTDIR # Delete old backups truncate $BKUPSRCDIR truncate $BKUPTGTDIR info "Backup complete"