LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Seding email after successful mysqldump (https://www.linuxquestions.org/questions/linux-newbie-8/seding-email-after-successful-mysqldump-876468/)

moyorakkhi 04-22-2011 04:10 AM

Seding email after successful mysqldump
 
Hello,

I've this script which takes backup of mysqldb through mysqldump. I need to send an automated email after success/failure mysqldump. How can add this feature in the script? Here's the script:

Code:

#!/bin/sh
BACKUP=/data/backup/sql2/new_backup/daily
cd $BACKUP
mkdir `date '+%d-%m-%Y'`
NOW=$(date +"%d-%m-%Y")

MUSER="root"
MPASS="45nflrbt"
MHOST="simpedia-sql4"
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"

DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
for db in $DBS
do
 FILE=$BACKUP/$NOW/mysql-$db.$NOW-$(date +"%T").sql.gz
 $MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS --lock-all-tables $db | $GZIP -9 > $FILE
done


zhjim 04-22-2011 04:36 AM

You could go with the mail prog if its installed. Something like this should do the trick
Code:

mail -s "Dump completed" your-mail@address.com

choogendyk 04-22-2011 07:06 AM

For a bit more detail in your email, you can do, e.g., the following

Code:

( echo "Subject: Dump completed";
  echo "Backup of $DBS";
  echo "completed on `hostname` at $BACKUP/$NOW" ) | mail -t your-mail@example.com

The statements in the parentheses become a subprocess in the script whose output is piped to the mail command. That allows you to fill out the Subject (and other headers if you want) plus some informational content in the body of the message.

choogendyk 04-22-2011 07:12 AM

Separate comment (and really a nitpick), you should reverse the order of:

mkdir `date '+%d-%m-%Y'`
NOW=$(date +"%d-%m-%Y")

And use the variable $NOW in the mkdir. These will happen so quickly that any problem is highly unlikely, but, in principle, the time could change between the two commands. Then your $NOW would fail to point to the directory that had been created. Partly it's just a matter of style and doing things the right way. In this case it's probably a one in a million chance of failure.


All times are GMT -5. The time now is 06:32 AM.