Backup database with php

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dj_ThimirA
    replied
    i used this before one year ago..........

    Originally posted by Wintch View Post
    Helo coderz, help me with a single php script to backup my database... Grateful

    PHP Code:
    backup_tables('localhost','username','password','blog');


    /* 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))).'.sql','w+');
      
    fwrite($handle,$return);
      
    fclose($handle);
    } 

    you must hide this from other users....................................
    otherwise .................................................. .
    Last edited by Dj_ThimirA; 15.03.12, 09:10.

    Leave a comment:


  • pmbguy
    replied
    dude, search tool... here you go, posted this a while back... (don't forget to click thanks, lol)

    Leave a comment:


  • Wintch
    replied
    @Pyramid am using xtreemh0zt, iz it dedicated server?

    Leave a comment:


  • shushant
    replied
    Login To PMA >Select Ur DB >click On Export

    Leave a comment:


  • pyramid_raider
    replied
    Originally posted by Wintch View Post
    @Pyramid, when i backup frm cpanel the c0ntent iz n0t of my database. But html c0ntents. Iz n0t w0rking fr0m my cpanel.
    if u r using dedicated server than i can help u on this for sure.... leave ur reply

    Leave a comment:


  • Wintch
    replied
    @Pyramid, when i backup frm cpanel the c0ntent iz n0t of my database. But html c0ntents. Iz n0t w0rking fr0m my cpanel.

    Leave a comment:


  • pyramid_raider
    replied
    try to backup via cpanel.. its very easy.. keep copy of ur db on ur local machine..

    Leave a comment:


  • Wintch
    replied
    Thanx, but did n0t w0rkz...

    Leave a comment:


  • pyramid_raider
    replied
    Backup Your MySQL Database Using PHP

    Leave a comment:


  • Wintch
    started a topic Backup database with php

    Backup database with php

    Helo coderz, help me with a single php script to backup my database... Grateful
Working...
X