Search code examples
phpmysqlcronbackup

Strict Standards: Only variables should be passed by reference in /main_dir/sub_dir/backup.php


I am able to run the script from the browser and backs up my mysql database, but when I try to do it with a cron job, I am getting the error:

Strict Standards: Only variables should be passed by reference in /main_dir/sub_dir/backup.php Warning: Using a password on the command line interface can be insecure.

Any suggestions? And, why the password warning?

<?php
//Enter your database information here and the name of the backup file
$mysqlDatabaseName ='xxxxxxxxxxxxxx';
$mysqlUserName ='xxxxxxxxxxxxxx';
$mysqlPassword ='xxxxxxxxxxxxxx_';
$mysqlHostName ='xxxxxxxxxxxxxx';
$mysqlExportPath ='xxxxxxxxxxxxxx.sql';

//Please do not change the following points
//Export of the database and output of the status
$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' > ' .$mysqlExportPath;
exec($command,$output=array(),$worked);
switch($worked){
    case 0:
        echo 'The database <b>' .$mysqlDatabaseName .'</b> was successfully stored in the following path '.getcwd().'/' .$mysqlExportPath .'</b>';
        break;
    case 1:
        echo 'An error occurred when exporting <b>' .$mysqlDatabaseName .'</b> zu '.getcwd().'/' .$mysqlExportPath .'</b>';
        break;
    case 2:
        echo 'An export error has occurred, please check the following information: <br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
        break;
}
?>

Should allow me to make a mysql db backup using cron jobs.


Solution

  • For the first error, change

    exec($command,$output=array(),$worked);
    

    to

    $output = array();
    exec($command,$output,$worked);
    

    Since the second parameter to exec() is a reference parameter, it has to be a variable, not an expression.

    See Suppress warning messages using mysql from within Terminal, but password written in bash script for lots of ways to prevent the warning about using a password on the command line.