#! /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.

# Mysql Sunucusunun Adi
SERVER=sunucu_adi

# Backup dizini. Default olarak kullanicinin home dizinidir.
BACKDIR=~/backups

# Backup dosyalarina eklenecek tarih formati.
DATE=`date +'%m-%d-%Y'`

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

# Mysql sunucu bilgileri (IP address is best)
HOST=localhost

# MySQL username
USER=username

# MySQL password
PASS=password

# Backup'i alinacak db'lerin listesi.
# DB'lerinizi dosluk birakarak ekleyebilirsiniz.
DBS="db1 db2"

# Burada y derseniz, mysql sunucusundaki tum db'ler backup alinir.
# ve yukaridaki liste devre disi birakilir.
DUMPALL=n

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

# Backup'larin e-mail ile gonderilmesini istiyorsaniz y diyin.
#(Sunucuda mutt kurulu olmalidir.)
# Ben bu ozelligi n diyerek devre disi birakiyorum.
MAIL=n

# backuplarin iletilecegi e-mail adresleri.
# Adresleri bosluk birakarak ekleyebilirsiniz.

EMAILS="1@gmail.com 2@inbox.com 3@goowy.com"

SUBJECT="MySQL backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# Backup'larin ftp'ye upload edilmesi icin y diyin.

FTP=y

# FTP server bilgileri.
FTPHOST="FTP_Server"
FTPUSER="username"
FTPPASS="password"

# Ftp sunucusundaki backup dizini. Bu dizin bulunmuyorsa, backuplar, login olunan
# ilk dizine upload edilir.
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# Eski backup dosyalarinin silinmesini istiyor musunuz?
DELETE=y

# Kac gun oncesine kadar ki backup dosyalarini saklamak istiyorsunuz?
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

if  [ $DUMPALL = "y" ]
then
        echo "Creating list of all your databases..."

        mysql -h $HOST --user=$USER --password=$PASS -e "show databases;" > dbs_on_$SERVER.txt

        # redefine list of databases to be backed up
        DBS=`sed -e ':a;N;$!ba;s/n/ /g' -e 's/Database //g' dbs_on_$SERVER.txt`

fi

echo "Backing up MySQL databases..."
for database in $DBS

do
        /usr/local/bin/mysqldump -h $HOST --user=$USER --password=$PASS $database > \
$BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
        gzip -f -9 $BACKDIR/$SERVER-mysqlbackup-$database-$DATE.sql
done

# 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 -e "Your backup has been emailed to you! n"

fi

if  [ $FTP = "y" ]

then

echo "Initiating FTP connection..."

cd $BACKDIR
ATTACH=`for file in *$DATE.sql.gz; do echo -n "mput ${file} "; done`

        ftp -nv <<EOF
        open $FTPHOST
        user $FTPUSER $FTPPASS
        cd $FTPDIR
        prompt
	bin
        $ATTACH
        quit

EOF

echo -e  "FTP transfer complete!"

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!

