sikas
sikas

Reputation: 5523

PHP script to backup database

I have tried to backup my MySQL databases using PHP, yet when I set the path for the backup I receive another path when running the script.

this is my script:

$baseDir = 'backup/';
$db;
$table;
$server = $_SERVER['HTTP_HOST'] . substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/'));

this is inside the backup function:

global $baseDir;
global $db;
global $server;
$backupFile = $server . "/$baseDir$db/$tab.sql";
$query = mysql_query("SELECT * INTO OUTFILE '$backupFile' FROM $tab");
if(!$query)
    echo "\t" .mysql_error() .PHP_EOL;
else
    echo " backuped." . PHP_EOL;

the path I get is: c:\wamp\bin\mysql\mysql5.1.36\data\localhost\db\backup\contact_keeper\accounts.sql with erroCode: 2 (can`t write/create).

Upvotes: 0

Views: 682

Answers (1)

Marc B
Marc B

Reputation: 360672

SELECT ... INTO OUTFILE is limited to only CREATING a new file. It cannot overwrite an existing file as a security measure. As well, make sure that the account that MySQL is running under in Windows has write permissions on that ...\backup\contact_keeper directory. It may have only read/execute.

Upvotes: 2

Related Questions