Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySqldump success or failure test

Tags:

php

mysql

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;
like image 813
sqlchild Avatar asked Dec 05 '25 13:12

sqlchild


1 Answers

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);
like image 187
Minras Avatar answered Dec 07 '25 03:12

Minras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!