Monday 3 February 2020

LDAP Backup Scripts

#!/bin/bash
#
# version 1.0,

#-------------------------------------------------------------
# directory to backup to
BACKDIR="/backups"
BACKDIR_physical="$BACKDIR/physical"
BACKDIR_logical="$BACKDIR/logical"
#  your LDAP server's name
##SERVER=cadir.nic.in
# date format that is appended to filename
DATE=`date +'%m-%d-%Y-T-%H:%M:%S'`
# your tape device
tape_device=/dev/st0
#tape_device=/dev/null
#DEST=" "
#-------------------------------------------------------------
# Source function library.
. /etc/init.d/functions

#slapd=/usr/sbin/slapd
#slurpd=/usr/sbin/slurpd
#[ -x ${slapd} ] || exit 0
RETVAL=0
LDAPFILES=/var/lib/ldap/

#-------------------Starting LDAP service-----------------------
function start() {
        # Start daemons.
        echo -n "Starting slapd:"
/etc/init.d/ldap start
        #daemon ${slapd}
        RETVAL=$?
        echo
        return $RETVAL
}

#----------------Stop LDAP service-----------------------------
function stop() {
        # Stop daemons.
echo -e "This is a test message"
        echo -e "Shutting down ldap: "
        #killproc ${slapd}
/etc/init.d/ldap stop
        RETVAL=$?
        return $RETVAL
}

function cptotape() {
        # Copying data to Tape.
        echo -n "Please check tape is inserted ..."
  #echo $tape_device
  #echo $DEST
        #tar -vzrf $tape_device $DEST
        echo -n "Copying data to Tape ..."
        echo
        return $RETVAL
}

#----------------------Mail Settings--------------------#
# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=N
# email addresses to send backups to, separated by a space
EMAILS="user@gmail.com user@inbox.com user@walla.com user@goowy.com"
# email subject
SUBJECT="LDAP Backup on $SERVER ($DATE)"
#----------------------FTP Settings--------------------#
# set "FTP=y" if you want to enable FTP backups
FTP=N
# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="user"
FTPPASS="password"
# directory to backup to. if it doesn't exist, file will be uploaded to
# first logged-in directory
FTPDIR="backups"
#-------------------Deletion Settings-------------------#
# delete old files?
DELETE=N
# how many days of backups do you want to keep?
DAYS=5
#----------------------End of Settings------------------#
# make sure script is run as root
if [ $(whoami) != "root" ]
then
  echo "You must be root to run this script."
  exit 1
fi
# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
echo Backups directory already exists
else
mkdir $BACKDIR
mkdir $BACKDIR_physical
mkdir $BACKDIR_logical
fi

echo Backing up LDAP entries...
if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! "
ATTACH=`for file in $BACKDIR/*$DATE.ldif; do echo -n "-a ${file} ";  done`

echo "$BODY" | mutt -s "$SUBJECT" $ATTACH $EMAILS
     
echo "Your backup has been emailed to you!"
fi
if  [ $FTP = "y" ]
then
cd $BACKDIR
ATTACH=`for file in *$DATE.ldif; do echo -n -e "put ${file}\n"; done`

ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
fi
if  [ $DELETE = "y" ]
then
find $BACKDIR -name "*.ldif" -mtime $DAYS -exec rm {} \;

if  [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted"
else
echo "The backup from $DAYS days ago has been deleted"
fi
fi
#-------------------------------------------------------------
# See how we were called.
case "$1" in
    Physical)
echo "Preparing for offline physical backup"
        DEST=$BACKDIR_physical/ldapbackup-$DATE.tar.gz
stop
[ $RETVAL -eq 0 ] && tar cvzf $DEST ${LDAPFILES}
cptotape
echo "=============================================="
echo $tape_device
echo $DEST
tar rvf $tape_device $DEST
[ $RETVAL -eq 0 ] && start
echo "==============================================="
;;
    Logical)
echo "Preparing for offline Logical backup in LDIF format"
        DEST=$BACKDIR_logical/ldapbackup-$DATE.ldif
slapcat -l  $DEST
cptotape
echo $tape_device
echo $DEST
tar rvf $tape_device $DEST
;;
 
    *)
        echo "Usage: $0 Physical|Logical}"
        RETVAL=1
esac
echo Your backup is complete!

No comments:

Post a Comment