Backup Scripts- README

20 08 2006
If you are using any of the backup scripts on my site, it is useful to know a few things to get them up and running, and to keep existing things on your server sane.

These scripts can be used together; they are friendly to each other and do not mess with one another. It is completely safe (and recommended) to have the same backup directory for all of the scripts.

How to use the scripts:

You can copy the code directly from the site and paste into a plain text editor. Make sure the editor saves files as plain text; i.e. it does no word processing. Save the file and upload it to your server (unless you're working on the server so you won't have to transfer anything), preferably in the root of your home directory (not a big deal, though). It is a good idea to save each of the scripts with similar names like backupmysql.sh, backupldap.sh, etc.

File permissions:
Once the file is on the server, make it executable. If you have ssh or other command-line access, type chmod +x [filename]. If you are using FTP, CHMOD the scripts to make them executable.

Testing the script:
If you have ssh, you can test the script by going to the script's directory and typing ./backupmysql.sh (or whatever the filename is). If the script works with no problems, you can now move forward and create a cron job.

Creating a cron job:
If you are using cPanel, the 'simple' cron creation method is simple enough to run without explanation. Just type the full script's path in the field and select the desired timings.

If you are using the 'advanced' method in cPanel or are entering the cron job using ssh, entering something like 0 0 * ~/backupmysql.sh should do the trick (will run the script every midnight). To edit your cron jobs in ssh, type crontab -e, which will open an editor with your current cron jobs listed. Update that file and save.

If you get the error bad interpreter: No such file or directory, then you probably saved the file in Windows and transferred it over to the server. Windows, Linux, and Macs save files differently, so the shell script can't run unless it is Linux-ready. cPanel is pretty good with this. If you have access to cPanel, all you have to do is open the script in the filemanager and click 'save'. cPanel will save the file in the correct format. If you don't have cPanel access, you can open the file in a text editor in ssh and save it.

[to be finished]

Folder Backup to FTP and Email Shell Script for Cron v2

01 08 2006
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
#-------------------------------------------------------------

# your server's name
SERVER=ameir.net

# directory to backup to
BACKDIR=~/backups

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

# directories to backup, separated by a space
# if this script is a level above a directory you want to backup,
# you can simply enter its name, otherwise, type the absolute path, 
# with or without a trailing slash.
DIRS="/home/user/public_html /home/user/directory2"

# set to 'y' if you want to backup all your folders. this will backup
# all files and folders in the script's parent directory
BACKALL=n

#----------------------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@goowy.com"

# email subject
SUBJECT="Directory 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=3

#----------------------End of Settings------------------#
# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
 echo Backups directory already exists
else
mkdir $BACKDIR
fi


# This is a list of folders to be backed up.
# You can add more entries if you want more
# directories to be backed up. The ${PWD##*/}
# from the first entry gets the base name from
# the current directory and uses it in the filename.
# Format: zip -9 -r (where to save) (what to backup).
# Be sure to include $BACKDIR/ in the beginning
# so that the file is saved in the backup directory.

if  [ $BACKALL = "y" ]
then
echo Backing up entire parent directory...
zip -9 -r $BACKDIR/${PWD##*/}-backup-$SERVER-$DATE.zip ./
else
echo Backing up selected directories...
for directory in $DIRS
do
DIR=`echo $directory |  sed -e 's/^\///g'  -e 's/\/$//g' \
-e 's/~//g' -e 's/\.//g' -e 's/home\///g' -e 's/\//_/g'`
echo Backing up folder named $directory as $DIR...
zip -9 -r $BACKDIR/$DIR-backup-$SERVER-$DATE.zip $directory
done
fi

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.zip; 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.zip; 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 "*.zip" -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 backupdir.sh

MySQL Backup to FTP and Email Shell Script for Cron v2

01 08 2006
Added ability to FTP multiple database backups over to a remote server, as well as with email.
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
#-------------------------------------------------------------

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

# directory to backup to
BACKDIR=~/backups

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

#----------------------MySQL Settings--------------------#

# your MySQL server's location (IP address is best)
HOST=localhost

# MySQL username
USER=username

# MySQL password
PASS=password

# List all of the MySQL databases that you want to backup in here, 
# each seperated by a space
DBS="db1 db2"

# set to 'y' if you want to backup all your databases. this will override
# the database selection above.
DUMPALL=y

# MySQL dump options
OPTIONS=" --quick --add-drop-table --add-locks --extended-insert --lock-tables"



#----------------------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@goowy.com"

SUBJECT="MySQL 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------------------#

# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
echo Backups directory already exists
else
mkdir $BACKDIR
fi

if  [ $DUMPALL = "y" ]
then
echo Dumping all your databases...
mysqldump -h $HOST --user=$USER --password=$PASS $OPTIONS --all-databases > \
$BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-ALL-$DATE.sql
else
echo Backing up MySQL databases...
for database in $DBS
do
mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done
fi

# if you have the mail program 'mutt' installed on
# your server, this script will have mutt attach the backup
# and send it to the email addresses in $EMAILS

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.sql.gz; 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.sql.gz; 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 "*.sql.gz" -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 backupmysql.sh

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