Backup Your Database into XML



I can’t stress enough the importance of keeping regular backups of your data. For many a website it doesn’t get more essential than backing up the database. Imagine loosing your entire database, the consequences could be disastrous.

Here’s a helpful snippet of PHP that outputs your database as XML.


<?php
header("Content-type: text/xml"); //We're going to display the XML so lets change the content type

//Create the connection
if (!$link = mysql_connect($host, $user, $pass)) {
 exit('Faild to connect to host.');
}

//Select a database
if (!mysql_select_db($database, $link)) {
 exit('Faild to connect to database.');
}

//get the tables
$sql = "SELECT TABLES FROM {$database}";
if (!$tables = mysql_query($sql, $link)) {
 exit('Faild to retreive tables.');
}

//start the output
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n";
$xml .= "<database name=\"{$database}\">\n";

//loop the tables
while ($table = mysql_fetch_row($tables)) {
 //start the table output
 $xml .= "\t<table name=\"{$table[0]}\">\n";

 //get the rows
 $sql = "SELECT * FROM {$table[0]}";
 if (!$rows = mysql_query($sql, $link)) {
 exit('Faild to retreive rows.');
 }

 //output the columns
 $attributes = array('name', 'blob', 'maxlength', 'multiple_key', 'not_null',
 'numeric', 'primary_key', 'table', 'type', 'default',
 'unique_key', 'unsigned', 'zerofill');

 $xml .= "\t\t<columns>\n";

 for($i = 0; $i < mysql_num_fields($rows); $i++) {

 $field = mysql_fetch_field($result, $i);

 $xml .= "\t\t\t<column ";

 //for every attribute
 foreach ($attributes as $attribute) {
 $xml .= $xml.= $attribute.'="'.$field->$attribute.'" ';
 }

 $xml .= "/>\n";
 }

 $xml .= "\t\t</columns>\n";

 //output the rows
 $xml .= "\t\t<records>\n";

 while($row = mysql_fetch_assoc($rows)) {

 $xml .= "\t\t\t<record>\n";

 //for each field
 foreach ($row as $key => $val) {
 $xml .= "\t\t\t\t<{$key}>".htmlspecialchars(stripslashes($val))."</{$key}>\n";
 }

 $xml .= "\t\t\t</record>\n";
 }

 $xml .= "\t\t</records>\n\t</table>\n";

}

$xml .= '</datbase>';

echo $xml;

Simple eh :) now obviously if you’re using this to backup a database you don’t want to output like this, instead you’re going to need to save it to a file. The simplest method is using PHP’s File_put_contents function http://www.php.net/file_put_contents

4 Responses to “Backup Your Database into XML”

  1. jon Bennett says:

    This seems like a bad idea really. What if you had many 1000’s of rows, it would be a very inefficient way to access those tables.

    Much better to use the command line and the ‘mysqldump’ tool. If you just want the data, use the ‘–no-create-info’ option.

    Cheers,

    j

  2. Jon, It depends on the setup and the site.

    Using MySQL via the command line may not be an option for many people. Most web hosts disable PHP “program execution functions” and only more advance packages tend to include shell access.

    Using an XML or SQL (I’m going to be posting and sql version in the next day or so.) backup script allows you to run regular backups with cron jobs.

    I agree to some extent though. This may not be practical for very large databases. I have however had success with this method on databases with over 10,000 rows and the speed is acceptable, but inefficient yes.

    Best Regards, George

  3. Great article . Will definitely apply it to my website

  4. Its so great….
    very helpful indeed

Leave a Reply




We're Listed in the e internet directory .co.uk Under: Website Design | Web Design Company - UK Web Designers & Developers