0

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
MrNedas
  • 17
  • 3
  • It probably doesn't like `$output=array()`. Initialize it first, or just pass `array()` or `[]` since you're not using $output at all. – aynber Aug 16 '19 at 19:05
  • For the password, see https://stackoverflow.com/questions/20751352/suppress-warning-messages-using-mysql-from-within-terminal-but-password-written OR https://stackoverflow.com/questions/23762575/bash-script-mysql-warning-using-a-password-on-the-command-line-interface-can-be – aynber Aug 16 '19 at 19:11

1 Answers1

0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612