<?php
/*
* Author: Mark Satterthwaite
* Date: 21 May 2009
* Email: mark.satterthwaite@gmail.com
*/
class dbbackup
{
/* Database Connection Variables*/
protected $DB_HOST = '';
protected $DB_NAME = '';
protected $DB_USER = '';
protected $DB_PASS = '';
/* Output variable used */
protected $output = '';
/* Create Connection */
function setConnectionInfo($DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
{
$this->DB_HOST = $DB_HOST;
$this->DB_NAME = $DB_NAME;
$this->DB_USER = $DB_USER;
$this->DB_PASS = $DB_PASS;
$connection = mysql_pconnect($this->DB_HOST, $this->DB_USER, $this->DB_PASS) or trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db($this->DB_NAME, $connection);
}
/* Do the backup, put the output to $output*/
function backup()
{
// Get a list of tables
$tables = $this->getTables();
// Create SQL for each table
foreach($tables as $table)
{
// Table Description
$this->output .= $this->getTableDescription($table) . ";";
$this->output .= "\n\n";
// Table Contents
$table_contents = $this->getTableContents($table);
foreach($table_contents as $content)
{
$this->output .= "INSERT INTO `$table` (";
$num_of_keys = sizeof(array_keys($content));
$counter = 0;
foreach(array_keys($content) as $key)
{
$this->output .= "`$key`";
$counter++;
if($counter != $num_of_keys)
$this->output .= ",";
}
$this->output .= ") VALUES (";
$counter = 0;
foreach(array_keys($content) as $key)
{
$this->output .= "'".addslashes($content[$key])."'";
$counter++;
if($counter != $num_of_keys)
$this->output .= ",";
}
$this->output .= ");\n";
}
$this->output .= "\n";
}
}
/* Returns the $output, incase you dont want a file to be written */
function getOutput()
{
return $this->output;
}
/* Write the backup file. if no name is used, I will make one up for you*/
function writeFile($sql_output = null)
{
if($sql_output == null){
$sql_output = $this->DB_NAME."_".time().".sql";
} if ($sql_output != null) {
$sql_output = $sql_output;
}
$fh = fopen($sql_output, 'w+');
fwrite($fh, $this->output);
fclose($fh);
return true;
}
/* Get a list of tables */
protected function getTables()
{
$SQL = "SHOW TABLES FROM `$this->DB_NAME`;";
$query = mysql_query( $SQL );
$results = array();
while($row = mysql_fetch_array( $query ))
array_push($results, $row[0]);
return $results;
}
/* Get the Description of a table */
protected function getTableDescription($table){
$SQL = "SHOW CREATE TABLE `$table`;";
$query = mysql_query( $SQL );
while($row = mysql_fetch_array( $query ))
$results = $row[1];
return $results;
}
/* Get table contents */
protected function getTableContents($table)
{
$SQL = "SELECT * FROM `$table`;";
$query = mysql_query( $SQL );
$results = array();
while($row = mysql_fetch_assoc( $query )){
$contents = array();
$contents = $row;
array_push($results, $contents);
}
return $results;
}
}
?>
And to use the script, something like this will work:
<?php
include 'dbbackup-class.php';
$dbbackup = new dbbackup();
$dbbackup->setConnectionInfo('host','dbname','user','password');
$dbbackup->backup();
$dbbackup->writeFile();
?>
If you want to specify a file name, just put a file name in the writeFile() function call as such:
$dbbackup->writeFile('backup.sql');
It should all be self explanitory, if not do not hesitate to ask.
Feel free to tinker and do with it what you want
This post has been edited by Randommark: 21 May 2009 - 02:23 PM


Help
This topic is locked
MultiQuote













