#!/bin/bash
if [ -n "$1" ];
then
        cd /var/www/fusion/$1
        DATE=`date +"%Y%m%d-%H%M"`
        if [ -f "config.php" ]
                then
                DBHOSTNAME=`grep -i "db_host_name" config.php | cut -d '>' -f2 | cut -d "'" -f2`
                USERNAME=`grep -i "db_user_name" config.php | cut -d '>' -f2 | cut -d "'" -f2`
                PASSWORD=`grep -i "db_password" config.php | cut -d '>' -f2 | cut -d "'" -f2`
                DBNAME=`grep -i "db_name" config.php | cut -d '>' -f2 | cut -d "'" -f2`
                echo -e "Database Name : $DBNAME"
                if [ ! -d "db" ];
                then
                        mkdir db
                fi
                cd db
                echo -e "Database Backup Process Started"
                mysqldump -h$DBHOSTNAME -u$USERNAME -p$PASSWORD --routines --events $DBNAME > $DBNAME.sql
                echo -e "Database Backup Process Finished"
        fi
        cd /var/www/fusion
        echo -e "Backup File Name : $1_$DATE.tar.gz"
        echo -e "Tar File Creation Started"
#        tar -zcf $1_$DATE.tar.gz $1 --exclude="$1/cache/*" --exclude="$1/upload/*" 
        tar -zcf $1_$DATE.tar.gz $1 
        echo -e "Tar File Creation Finished"
        SIZE=`du -h $1_$DATE.tar.gz | awk '{print$1}'`
        cd /var/www/fusion/$1
        rm -rf db/
        echo -e "Process Completed"
        echo -e "Backup File Size : $SIZE\n"
else
        echo -e "\nPlease specify the CRM instance name !\n"
fi

