NuSphere Forums Forum Index
NuSphere Forums
Reply to topic
Database backup only partially completed.


Joined: 09 Dec 2003
Posts: 8
Reply with quote
I have code which, through PHP, performs a backup of my database. I've also downloaded code which performs this function off of the internet.

When debugging, it works perfectly. When running through my webbrowser, ( IE, Opera), the file is truncated after a variable number of bytes, between 2700 and 2700 bytes.

Any guesses why this would be happening?

Attached is the code for the class
Code:
class backupData{
    var $dbserver;
    var $dbuser;
    var $dbpass;
    var $dbname;
    function InitVars( $DBServer, $DBUser, $DBPsswd, $DBName){
       $this->dbserver = $DBServer;
       $this->dbuser = $DBUser;
       $this->dbpass = $DBPsswd;
       $this->dbname= $DBName;
    }
   function GetDBStructure(){
      mysql_connect($this->dbserver, $this->dbuser, $this->dbpass) or die( "cannot log in");
      mysql_select_db($this->dbname) or die( "cannot select your db: --".$this->dbname."--");
      $tables = mysql_list_tables($this->dbname);
      while ($td = mysql_fetch_array($tables)){
         $table = $td[0];
         $r = mysql_query("SHOW CREATE TABLE `$table`");
         if ($r){
            $insert_sql = "";
            $d = mysql_fetch_array($r);
            $d[1] .= ";";
            $SQL[] = str_replace("\n", "", $d[1]);
            $table_query = mysql_query("SELECT * FROM `$table`");
            $num_fields = mysql_num_fields($table_query);
            while ($fetch_row = mysql_fetch_array($table_query)){
               $insert_sql .= "INSERT INTO $table VALUES(";
               for ($n=1;$n<=$num_fields;$n++){
                  $m = $n - 1;
                  $insert_sql .= "\"".$fetch_row[$m]."\", ";
               }
               $insert_sql = substr($insert_sql,0,-2);
               $insert_sql .= ");\n";
            }
            if ($insert_sql != ""){
               $SQL[] = $insert_sql;
            }
         }
      }
      return $SQL;
   }

   function MakeBackup($filename){
      $sql = $this->GetDBStructure();
      header('Content-type: plain/text');
      header('Content-Disposition: attachment; filename="'.$filename.'"');
      $sql = implode("\r", $sql);
      print $sql;
      exit;
   }

   function restoreBackup ($filename){
      mysql_connect($this->dbserver, $this->dbuser, $this->dbpass);
      mysql_select_db($this->dbname);
      $lines = explode("\n", file_get_contents($filename));
      $result = true;
      foreach ($lines as $line)
         mysql_query($line) OR DIE("Error while importing: ".mysql_error());
      //$result = $result && mysql_query($line);
      return $result;
   }
}//end of class definition

And the code to use the class is:
Code:
    $bkp= new backupData();
    $bkp->InitVars( DBHOST, $dbuser, $pswd, "db_name");
    $bkp->MakeBackup( "testingonly.sql");


Again, it works wonderfully when debugging.

Thanks!
View user's profileFind all posts by mmsemajSend private message
Site Admin

Joined: 13 Jul 2003
Posts: 8344
Reply with quote
I'd suggest you enable error logs for php and check them.

BTW, we do support IDE, not php-based solutions Smile
View user's profileFind all posts by dmitriSend private messageVisit poster's website
Database backup only partially completed.
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT - 5 Hours  
Page 1 of 1  

  
  
 Reply to topic