Backup database with php

Started on the old forum in November 2011 · view the original in the archive · old links redirect here

9 replies · 542 views · started 15 years ago · 1 min read

5 participants
Wintch Developer · 68 posts #1

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

Wintch Developer · 68 posts #3

Thanx, but did n0t w0rkz...

pyramid_raider Developer · 60 posts #4

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

Wintch Developer · 68 posts #5

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

pyramid_raider Developer · 60 posts #6

Wintch wrote: @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

shushant Developer · 207 posts #7

Login To PMA >Select Ur DB >click On Export

Wintch Developer · 68 posts #8

@Pyramid am using xtreemh0zt, iz it dedicated server?

Dj_ThimirA Newbie · 5 posts #10

i used this before one year ago..........

Wintch wrote: Helo coderz, help me with a single php script to backup my database... Grateful

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 ...................................................

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close