LDAP Backup to FTP and Email Shell Script for Cron v2

01 08 2006
Added ability to FTP files to another server and to delete old backups from backup directory.

CODE:
#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# Last modified: August 1, 2006
#-------------------------------------------------------------

# directory to backup to
BACKDIR=~/backups

# your LDAP server's name
SERVER=ameir.net

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`


#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=y

# 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=y

# 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=y

# 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
fi

echo Backing up LDAP entries...

slapcat -l $BACKDIR/$SERVER-ldapbackup-$DATE.ldif

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
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

echo Your backup is complete!


Download backupldap.sh



Trackbacks


No Trackbacks

Comments

Display comments as (Linear | Threaded)
17 10 2007
#1 Andres (Reply)

Thanks, you've saved some time of mine!
Simple and effective ;-)
17 09 2008
#2 Antsa (Reply)

Thank you so much!! You saved my day, God bless!
09 03 2009
#3 Pat (Reply)

Works like a charm !

Thanks a lot.
24 07 2009
#4 dikra61 (Reply)

All works fine ! But it's a good idea modify the mkdir command to mkdir -p to create more than one Directory layer.

Regards

Add Comment


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.
BBCode format allowed

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA