Forum Moderators: coopster

Message Too Old, No Replies

Restore MySQL database through PHP

         

dbzfyam

12:36 pm on Aug 17, 2007 (gmt 0)

10+ Year Member



I found the first function somewhere which I use to backup my database. I tried modifying it so I can restore the database aswell (second function. But I cannot seem to import the (eventually uploaded by the user) SQL file (always returns false). I don't know if the PHP code is wrong or the mysql command. Help would be greatly appreciated.

Thanks in advance.


function makeBackup() {
global $username, $password, $database;
$BackupFile = './micropower.' . date('d-m-y') . '.backup.sql';
$BackupCommand = 'mysqldump --compact --user=' . $username . ' --password=' . $password . ' --add-drop-table ' . $database . ' > ' . $BackupFile;
if(file_exists($BackupFile)) {return false;}
else{exec($BackupCommand);
return true;
}
}


function restoreBackup() {
global $username, $password, $database;
$BackupFile = 'micropower.' . date('d-m-y') . '.backup.sql';
$myFile = "{$BackupFile}";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $restoreCommand = "mysql -u {$username} -p{$password} {$database} < {$BackupFile}";
if (exec($restoreCommand)) {return true;}
else {return false;}
}

EDIT: Just tried to import the sql file to see if it works, but for some reason, it stops on double quotes ("). Is there a way to backslash those in the mysqldump (makeBackup)?

eelixduppy

1:54 am on Aug 23, 2007 (gmt 0)



Have you tried running any of these commands through the command line manually to see if you get any errors from mysql?