Home > Software design >  Securily backup database with a shell command from inside PHP script
Securily backup database with a shell command from inside PHP script

Time:12-07

I found that one way would be to run mysqldump from PHP by using proc_open() so that I can avoid using password string directly in the command. The problem is that PHP script is called from shell but I still get Enter password: prompt.

$cmd = "mysqldump -u ".$db_user." -p -h ".$db_host." ".$db_name." > /files/database-backups/db-backup.sql";

$desc = [
    0 => ["pipe", "r"]
];
    
$p = proc_open($cmd, $desc, $pipes);
    
fwrite($pipes[0], $db_pass);
fclose($pipes[0]);
proc_close($p);

I would need it to get the password and finish without the need for any external input. Is it possible? If yes what am I doing wrong here?

If there is a better alternative what is it? I need to read DB details from PHP so a separate shell script with hard coded details would not work.

CodePudding user response:

As soon as i know, you can add the password to the command.

To correct this, try using --password=YOUR_PASSWORD or -p=YOUR_PASSWORD.

CodePudding user response:

Within a bash script file, you can use

mysql -u user -p`cat /folder/user-pwd.txt` dbname <<EOFSCRIPT

put all your mysql commands below followed by

EOFSCRIPT

By keeping the pwd in a secure place (may be under /usr/local/bin or /etc/yourfolder/ or other). This way ps aux can not expose the pwd.

  • Related