Location: PHPKode > scripts > Hot or Not Clone > hotornot/control/backup/backup.php
<?
include("../../config.php");
if ($recreate!=1) {
   flush();
   $dbhost=$dbServer;
   $dbuser=$dbuserName;
   $dbpass=$dbpassword;
   $dbname=$dbName;
   $table_names="*";
   $conn = @mysql_connect($dbhost,$dbuser,$dbpass);
   if ($conn==false)  die("password / user or database name wrong");
   $path = $path . "dump/";
   if (!is_dir($path)) mkdir($path, 0766);
   chmod($path, 0777);

   if(!file_exists($path . "backup.sql"))
   {
	$fp2 = fopen ($path."backup.sql","w");
	fwrite ($fp2,"");
	fclose ($fp2);
        chmod($path."backup.sql", 0777);
   }

   if(file_exists($path . "backup.gz"))
   { 
       unlink($path."backup.gz");
   } 
} else {   // recreate dir /dump
   $path = $path . "dump/";
   if (!is_dir($path)) mkdir($path, 0766);
   chmod($path, 0777);
}

function get_def($dbname, $table) {
    global $conn;
    $def = "";
    $def .= "DROP TABLE IF EXISTS $table;#%%\n";
    $def .= "CREATE TABLE $table (\n";
    $result = mysql_db_query($dbname, "SHOW FIELDS FROM $table",$conn) or die("Table $table not existing in database");
    while($row = mysql_fetch_array($result)) {
        $def .= "    $row[Field] $row[Type]";
        if ($row["Default"] != "") $def .= " DEFAULT '$row[Default]'";
        if ($row["Null"] != "YES") $def .= " NOT NULL";
       	if ($row[Extra] != "") $def .= " $row[Extra]";
        	$def .= ",\n";
     }
     $def = ereg_replace(",\n$","", $def);
     $result = mysql_db_query($dbname, "SHOW KEYS FROM $table",$conn);
     while($row = mysql_fetch_array($result)) {
          $kname=$row[Key_name];
          if(($kname != "PRIMARY") && ($row[Non_unique] == 0)) $kname="UNIQUE|$kname";
          if(!isset($index[$kname])) $index[$kname] = array();
          $index[$kname][] = $row[Column_name];
     }
     while(list($x, $columns) = @each($index)) {
          $def .= ",\n";
          if($x == "PRIMARY") $def .= "   PRIMARY KEY (" . implode($columns, ", ") . ")";
          else if (substr($x,0,6) == "UNIQUE") $def .= "   UNIQUE ".substr($x,7)." (" . implode($columns, ", ") . ")";
          else $def .= "   KEY $x (" . implode($columns, ", ") . ")";
     }

     $def .= "\n);#%%";
     return (stripslashes($def));
}

function get_content($dbname, $table) {
     global $conn;
     $content="";
     $result = mysql_db_query($dbname, "SELECT * FROM $table",$conn);
     while($row = mysql_fetch_row($result)) {
         $insert = "INSERT INTO $table VALUES (";
         for($j=0; $j<mysql_num_fields($result);$j++) {
            if(!isset($row[$j])) $insert .= "NULL,";
            else if($row[$j] != "") $insert .= "'".addslashes($row[$j])."',";
            else $insert .= "'',";
         }
         $insert = ereg_replace(",$","",$insert);
         $insert .= ");#%%\n";
         $content .= $insert;
     }
     return $content;
}

if ($recreate!=1) {
$filetype = "sql";

if (!eregi("/restore\.",$PHP_SELF)) {
	
	$cur_time=date("Y-m-d H:i");
	if ($table_names == "*" ) {
	   $tables = mysql_list_tables($dbname,$conn);
	   $num_tables = @mysql_num_rows($tables);
	   $i = 0;
	   while($i < $num_tables) { 
	      $table = mysql_tablename($tables, $i);
	      $newfile .= get_def($dbname,$table);
	      $newfile .= "\n\n";
	      $newfile .= get_content($dbname,$table);
	      $newfile .= "\n\n";
	      $i++;
	   }	
	} else {
	   $i=0;
	   $tables=explode(";",$table_names);
	   if (count($tables) != 0) {
	      while($i < count($tables)) {
	         $newfile .= get_def($dbname,$tables[$i]);
	         $newfile .= "\n\n";
	         $newfile .= get_content($dbname,$tables[$i]);
	         $newfile .= "\n\n";
	         $i++;
	      }
	   }       
	}
	$fp = fopen ($path."backup.$filetype","w");
	fwrite ($fp,$newfile);
	fclose ($fp);
}
}
?>
<link rel="stylesheet" href="../stylesheet.css" type="text/css">
<body bgcolor="#f4f4f4" link="#000000" alink="#000000" vlink="#000000">
<table width="95%" height="10" border="0" cellpadding="0" cellspacing="0" class="mytable">
  <tr> 
    <td><div align="center"> 
        <table border="0" width="100%" height="100%">
          <form enctype="multipart/form-data" name="myform"  method="post" action="addarticle.php" onsubmit="javascript: return checkifvalid();">
            <tr > 
              <td height="19" class="header">Backup Database</td>
            </tr>
            <tr> 
              <td height="19"><font face="Verdana" size="2">Backup done.<a href="../downloadfile.php">Click 
                here</a> to download</font></td>
            </tr>
            <tr> 
              <td height="17"> <p align="center">&nbsp; </td>
            </tr>
          </form>
        </table>
      </div></td>
  </tr>
</table>
<CENTER>
<BR>
<TR>
  <TD height="215" valign="top">&nbsp;</TD>
    </TR>
    
Return current item: Hot or Not Clone