Wednesday, February 8, 2012

How to email backup shared hosting site mysql database automatically


 Here is the bourne shell script on my Centos unix Hostgator shared hosting server placed in the crontab and runs once in a day at 3am my country time.

#!/bin/sh

cd /home/myname/www/tmp/all


mv *.sql j
rm *.gz

for i in  *dump*.sh
do
if ! sh $i
then
echo "Failed for $i"
exit
else
echo "$i success"
fi
done
d=`date|tr " :" --`
echo $d

ls -l *.sql

echo "Total files=" `ls -l *.sql|wc -l`
file="scripts.tar"
if ! tar cvf $file   *.sh *.php
then
echo "tar for file:$file has failed"
exit
fi
if ! gzip -9 $file
then
echo "gzip for file:$file has failed"
exit
fi
echo "...........................Success .... see the file below:"
ls -lh "$file.gz"


for i in *.sql
do
if ! gzip -9 $i
then
echo "gzip for file:$i failed"
exit 1
fi
done


echo "Emailing it"


for i in *.gz
do
if ! php email.php "$i"
then
echo "Email failed for $i"
fi
done

echo "Email sent successfully"
exit 0

-----------------------------------------------

Content on my sample mysqldump_adi.sh

mysqldump -h localhost   -u <dblogin> <dbname> -p<dbpasswd> outfile.sql
-------------------------------------------
Email.php: Here is the source code to send files by attachment using Smtp in Php.

No comments:

Post a Comment