Sunday, June 10, 2012

Back ups in to XML File & .rar file php

Back ups in to XML File and .rar file php
You can get your "dept_division" in to XML file by using following php scripts file.XML file will be created in your root directory where you store your php file.

<?php
$host='localhost';
$user='root';
$pass='';
$name='my_database';
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);


$query = 'SHOW TABLES FROM '.$name;
$result = mysql_query($query,$link) or die('cannot show tables');
if(mysql_num_rows($result))
{
  
    $tab = "\t";
    $br = "\n";
    $xml = '<?xml version="1.0" encoding="UTF-8"?>'.$br;
    $xml.= '<database name="'.$name.'">'.$br;
  
  
    while($table = mysql_fetch_row($result))
    {
      
        $xml.= $tab.'<table name="'.$table[0].'">'.$br;
      
  
        $query3 = 'SELECT * FROM '.$table[0];
        $records = mysql_query($query3,$link) or die('cannot select from table: '.$table[0]);
      
  
        $attributes = array('name','blob','maxlength','multiple_key','not_null','numeric',
'primary_key','table','type','default','unique_key','unsigned','zerofill');
        $xml.= $tab.$tab.'<columns>'.$br;
        $x = 0;
        while($x < mysql_num_fields($records))
        {
            $meta = mysql_fetch_field($records,$x);
            $xml.= $tab.$tab.$tab.'<column ';
            foreach($attributes as $attribute)
            {
                $xml.= $attribute.'="'.$meta->$attribute.'" ';
            }
            $xml.= '/>'.$br;
            $x++;
        }
        $xml.= $tab.$tab.'</columns>'.$br;
      
        //stick the records
        $xml.= $tab.$tab.'<records>'.$br;
        while($record = mysql_fetch_assoc($records))
        {
            $xml.= $tab.$tab.$tab.'<record>'.$br;
            foreach($record as $key=>$value)
            {
                $xml.= $tab.$tab.$tab.$tab.'<'.$key.'>'.htmlspecialchars(stripslashes($value)).'</'.$key.'>'.$br;
            }
            $xml.= $tab.$tab.$tab.'</record>'.$br;
        }
        $xml.= $tab.$tab.'</records>'.$br;
        $xml.= $tab.'</table>'.$br;
    }
    $xml.= '</database>';
  
  
    $handle = fopen($name.'-backup-'.time().'.xml','w+');
    fwrite($handle,$xml);
    fclose($handle);
echo "xml file created";
}
?>

By using following scrips you can even create .rar file too.

<?php
backup_tables('localhost','root','','my_database');


/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '')
{
  
    $link = mysql_connect($host,$user,$pass);
    mysql_select_db($name,$link);
  
    //get all of the tables
    if($tables == '')
    {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while($row = mysql_fetch_row($result))
        {
            $tables[] = $row[0];
        }
    }
    else
    {
        $tables = is_array($tables) ? $tables : explode(',',$tables);
    }
  
    //cycle through
    foreach($tables as $table)
    {
        $result = mysql_query('SELECT * FROM '.$table);
        $num_fields = mysql_num_fields($result);
      
        $return.= 'DROP TABLE '.$table.';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
        $return.= "\n\n".$row2[1].";\n\n";
      
        for ($i = 0; $i < $num_fields; $i++)
        {
            while($row = mysql_fetch_row($result))
            {
                $return.= 'INSERT INTO '.$table.' VALUES(';
                for($j=0; $j<$num_fields; $j++)
                {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = ereg_replace("\n","\\n",$row[$j]);
                    if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
                    if ($j<($num_fields-1)) { $return.= ','; }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n";
    }
  
    //save file
    $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.rar','w+');
    fwrite($handle,$return);
    fclose($handle);
echo " back up Created";
}
?>
Output file in your web root look is like bellow

No comments:

Post a Comment