2

I am using mysqldump to take the backup of my mysql database and have put it under a cron job . I want to test its success or failure and want it to echo the Success or Failure Message in the cron job email but failing ? Please help me out...

What command to pass ? I did this but failed :

In my php backup script I included:

   $testvar = '
            if [ "$?" -eq 0 ]
then
        echo "Success"
        else
        echo "Mysqldump encountered a problem look in database.err for information"
        fi
            ';

    exec($testvar);

My server says : Unexpected End of File

SCRIPT:

 $creatBackup = 'mysqldump -uabc -p password  mydb > myfile '.       
        'if [ "$?" -eq 0 ]; then
        eho "Success"    

 else  echo "Mysqldump encountered a problem"
fi
    ';



$BackupMessage = exec($creatBackup);

echo $BackupMessage;
sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • this looks to me like a shell error. Could you please post the script and the way you are calling it via cron. – Radix Jan 05 '12 at 15:54
  • @Radix - sir, i have posted the script – sqlchild Jan 05 '12 at 16:10
  • You sholud publish all php backup script. This lines that you post don't include mysqldump command. You should invoke dump ant test in the same exec() php command. – dani herrera Jan 05 '12 at 16:28
  • assuming you are running the php script via cron, are you including the hash bang at the top of the file with the path to your php executable? Also, I note that you put mysql user right next to the -u and a space between the -p and the password -- it should be the other way around, 'mysqldump -u abc -ppassword. – Radix Jan 05 '12 at 16:31

4 Answers4

3

You are trying to run a bash script content using exec. That's not correct; exec is for running commands or script files, not the shell scripts.

I see two ways of fixing that:

1) Save the content of your script to the bash file and run it using exec:

/var/www/scripts/script.sh:

#!/bin/bash
mysqldump -uabc -p password  mydb > myfile
if [ "$?" -eq 0 ]; then
    echo "Success"    
else
    echo "Mysqldump encountered a problem"
fi

PHP:

$creatBackup = '/var/www/scripts/script.sh';
$BackupMessage = exec($creatBackup);
echo $BackupMessage;

2) Another way is to catch not only the script output, but also standard errors (check the third exec parameter):

$creatBackup = 'mysqldump -uabc -p password  mydb > myfile';
exec($creatBackup, $output, $returnVar);
var_dump($output, $returnVar);
Minras
  • 4,136
  • 4
  • 18
  • 18
1

Answered in a similar thread, but with the same issue. Grabbing mysqldumps' error messages via PHP: mysqldump via PHP

Community
  • 1
  • 1
Jon T
  • 118
  • 5
0

If you are including the referenced code in your php backup script, you are incorrectly calling the 'exec' function which requires an external program be referenced. The content of your '$testvar' variable appears to be shell script (sh or bash) rather than an external script name (and filepath).

The following shell script would run the mysqldump command and could be executed via cron to provide you with email notification of success or failure of your database dump. You will need to tweak your PHP script to actually create the backup files for mysqldump.

#!/bin/sh
if /usr/local/mysql/bin/mysqldump -u root -pyourpassword yourdatabasename 
then
    echo 'Success'
else
    echo 'Mysqldump encountered a problem look in database.err for information'
fi

Hope this helps!

Radix
  • 667
  • 5
  • 28
0

I think exit code of mysqldump will help you.It will return 0 on success, any other value on failure.

php function passthru return this value in second parameter.

rkosegi
  • 14,165
  • 5
  • 50
  • 83