Click to See Complete Forum and Search --> : database backup using phpmyadmin?


lukeurtnowski
08-15-2005, 04:55 PM
I'm trying to figure out how to create a php script whose only purpose is to back-up the database (MySQL) which happens to have PHPMyAdmmin on it. How do I create a *.php page (back-up.php) which I could run at night which would somehow back-up my database (create a text file or something) which could then be used to replace the database (in case the databaase gets corrupted somehow)??
Thanks

ShrineDesigns
08-15-2005, 05:33 PM
try this<?php
class mysql_dump
{
var $command = '';

function mysql_dump($username, $password, $database, $file = NULL)
{
if(empty($file))
{
$this->command = "mysqldump --skip -C -u $username -p{$password} $database";
}
else
{
$this->command = "mysqldump --skip -C -u $username -p{$password} $database -r $file";
}
$this->backup();
}
function backup()
{
ob_start();
passthru($this->command, $retval);

if(empty($retval))
{
$sql = preg_replace("/\/\*.+\*\/;\r?\n/", '', ob_get_contents());
ob_end_clean();
header('Content-type: text/plain');
$sql = trim($sql) . "\n";
echo $sql;
return true;
}
return false;
}
}
?>NOTE: for windows the mysql bin tools like mysqldump, mysqlhotcopy, etc. must be register otherwise you will need to change mysqldump to C:\\mysql\\bin\\mysqldump.exe or whatever the full path is on your system
example<?php
// ...
$bak = new mysql_dump('root', '', 'test', '/path/to/backup.sql'); // save to file
$bak = new mysql_dump('root', '', 'test'); // outputs to browser
?>

lukeurtnowski
08-15-2005, 07:44 PM
Um....(I'm sort of new to this so you might have to elaborate). I have an externel php file to connect to the database. Can I include it here to connect to the database or should I create the variables; $username, $password, and $database.
What is the variable, $file?
Finally is it ok if in place of /path/to/backup.sql, I use something like; mysql/Backup/backup.sql? If I do this, what is the absolute path to that file so I can find it using windows explorer? (C:/...../mysql/Backup/backup.sql)
Thanks very much!

ShrineDesigns
08-15-2005, 08:12 PM
you can do this to save it to the same directory as the script$bak = new mysql_dump('root', '', 'test', './backup.sql');a class is similar to a function so you can use variables or strings as the arguments$user = 'root';
$pass = '';
$db = 'test';
$bak = new mysql_dump($user, $pass, $db, './backup.sql');

lukeurtnowski
08-15-2005, 10:05 PM
ok, thanks soo mmuch