Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

3 Answers 3

up vote 2 down vote accepted

You mentioned that you had other methods preceding this that also make use of exec... and thus your logic is flawed. Every time you execute a command using exec, your working in a fresh blank environment, and the previous session's data is no longer available; and as such $? will not have the value you're looking for. You need to stop trying to write bash scripts inside of PHP.

For example, in one of your comments you mentioned the following:

i did this : $Backup = "mysqldump .........."; exec($Backup); $testvar ='.............'; exec($testvar); $createzip ='..........'; exec($createzip);

When instead of trying to execute bash commands as a variable... you should be executing only the necessary commands outside of PHP.

instead of something like

$testvar = '
    /usr/bin/mysqldump -u DBuser -pDBpassword database >database.sql 2>database.err
    if [ "$?" -eq 0 ]
    then
    echo "Success"
    else
    echo "Mysqldump encountered a problem look in database.err for information"
    fi
        ';

$MysqlDumpMsg=exec($testvar);
echo $MysqlDumpMsg;

You should be doing something more like this:

exec("mysqldump .......... 2>database.err",$MysqlDumpMsg,$MysqlDumpCode);
if ($MysqlDumpCode = 0) {
  echo "Success";
} else {
  echo "Mysqldump encountered a problem look in database.err for information";
}

or whatever else you need. If you have some zip'ing command in there someplace... then do it as several steps...

exec("mysqldump .......... >database.sql 2>database.err",$MysqlDumpMsg,$MysqlDumpCode);
if ($MysqlDumpCode = 0) {
  echo "Successfully dumped database.";
  exec("gzip database.sql", $ZipMsg, $ZipReturnCode)
  if ($ZipReturnCode = 0) {
    echo "Successfully zip'd database!";
  } else {
    echo "Failed to compress the database."
  }
} else {
  echo "Mysqldump encountered a problem look in database.err for information";
}
share|improve this answer
    
... customize as needed. –  TheCompWiz Jan 5 '12 at 16:11
    
sir, shall I put the script--'mysqldump......>abc.sql' in a mybackupscript.sh file and run it from my php script file. as the content is a bash script content , and am using EXEC for running it. –  sqlchild Jan 5 '12 at 17:15
    
As I said... customize it as needed. My point being... don't try & write bash scripts inside of PHP... Let PHP do the work. –  TheCompWiz Jan 5 '12 at 17:28

You have a typo in your "if" statement...

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

exec($testvar);

semi-colons are important :D

share|improve this answer
    
Sir, it still says : sh: : command not found sh: -c: line 7: syntax error: unexpected end of file –  sqlchild Jan 5 '12 at 15:34
    
In you environment... do you have a sym-link to your shell in /bin called "sh" ... i.e. /bin/sh -> /bin/bash (or dash/ash/csh/whatever shell) –  TheCompWiz Jan 5 '12 at 15:50
    
sir, am just working through cpanel –  sqlchild Jan 5 '12 at 15:57

Several things going on here

  • You don't appear to be running a mysqldump command.
  • $? is the exit status of the last command, you don't have a command before the test so it's probably not valid.
  • The output from the shell script will be returned to the exec command. You are ignoring it.
  • you are as @TheCompWiz points out missing an important ;

Try something like this

$testvar = '
    /usr/bin/mysqldump -u DBuser -pDBpassword database >database.sql 2>database.err
    if [ "$?" -eq 0 ]
    then
        /usr/bin/zip database.zip database.sql >/dev/null 2>>database.err
        if [ "$?" -eq 0 ]
        then
             echo "Succcess"
        else
             echo "Mysqldump succeeded but zip failed see database.err for details"
        fi
    else
    echo "Mysqldump encountered a problem look in database.err for information"
    fi
        ';

$MysqlDumpMsg=exec($testvar);
echo $MysqlDumpMsg;

This captures the output from the shell script and outputs it so that the cron job should mail it to you.

share|improve this answer
    
sir , i did this : $Backup = "mysqldump .........."; exec($Backup); $testvar ='.............'; exec($testvar); $createzip ='..........'; exec($createzip); –  sqlchild Jan 5 '12 at 15:45
1  
Rather than trying to get the return code on a 2nd command call... (which is what you're doing) ... try using the exec command properly... i.e. exec($Backup,outputText,returnCode); –  TheCompWiz Jan 5 '12 at 15:47
    
Sir, I am having a create zip command after testing the shell variable , will it effect the output? –  sqlchild Jan 5 '12 at 15:49
    
Every time you exec(...) something you are creating a new shell so the $? in your second exec is invalid. –  Iain Jan 5 '12 at 15:50
1  
sqlchild, you're working too hard. the exec statement spawns a new shell and executes the statement. Don't try & make php run bash commands... make php execute single commands and capture the output in a variable and work with the return code... or write a bash script to do everything, and have php call that single bash script. –  TheCompWiz Jan 5 '12 at 15:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.