사바이캅

php 스크립트로 간단히 mysql 백업하기

phpmyadmin 들어가지않고 
php로 간단하게 sql 백업 받는 소스입니다.

backup_tables('localhost','ID','PW','DB');  의 코드를 본인의 계정에 맞게
수정하셔서 쓰시면 됩니다. 
코드를 실행하면 20121111.sql 처럼 파일이 생성됩니다.

[code]
<?php
backup_tables('localhost','ID','PW','DB');



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);
  }
  

  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";
  }
  

  $handle = fopen('db-backup-'.time().'-'.(md5(implode(',',$tables))).'.sql','w+');
  fwrite($handle,$return);
  fclose($handle);
}
?>
[/code]
|

댓글 5개

좋네요
유용하게 쓰겠습니다~
엌 편하네요 감사합니다!
감사합니다.
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

+
제목 글쓴이 날짜 조회
13년 전 조회 5,191
13년 전 조회 6,719
13년 전 조회 2,409
13년 전 조회 3,614
13년 전 조회 2,750
13년 전 조회 1,729
13년 전 조회 3,459
13년 전 조회 3,077
13년 전 조회 1,930
13년 전 조회 1,737
13년 전 조회 4,328
13년 전 조회 2,743
13년 전 조회 1,667
13년 전 조회 1,641
13년 전 조회 9,265
13년 전 조회 5,783
13년 전 조회 8,866
13년 전 조회 1,821
13년 전 조회 2,538
13년 전 조회 2,920
13년 전 조회 2,400
13년 전 조회 3,835
13년 전 조회 2,132
13년 전 조회 2,279
13년 전 조회 1,668
13년 전 조회 2,327
13년 전 조회 2,428
13년 전 조회 1.4만
13년 전 조회 2,248
13년 전 조회 5,703
13년 전 조회 2,756
13년 전 조회 1,813
13년 전 조회 2,143
13년 전 조회 4,940
13년 전 조회 2,082
13년 전 조회 5,713
13년 전 조회 1,799
13년 전 조회 1,981
13년 전 조회 2,257
13년 전 조회 9,828
13년 전 조회 2,617
13년 전 조회 3,730
13년 전 조회 2,843
13년 전 조회 3,019
13년 전 조회 3,174
13년 전 조회 2,100
13년 전 조회 2,295
13년 전 조회 1.9만
13년 전 조회 3,520
13년 전 조회 5,526
13년 전 조회 2,750
13년 전 조회 3,285
13년 전 조회 3,169
13년 전 조회 1.7만
13년 전 조회 2,078
13년 전 조회 2,062
13년 전 조회 1,839
13년 전 조회 2,784
13년 전 조회 4,339
13년 전 조회 4,449
13년 전 조회 3,320
13년 전 조회 6,698
13년 전 조회 3,978
13년 전 조회 7,071
13년 전 조회 2,804
13년 전 조회 2.2만
13년 전 조회 2,064
13년 전 조회 2,579
13년 전 조회 1.8만
13년 전 조회 3,776
13년 전 조회 2,139
13년 전 조회 4,781
13년 전 조회 2,333
13년 전 조회 2,493
13년 전 조회 1,913
13년 전 조회 3,677
13년 전 조회 2,667
13년 전 조회 1,818
13년 전 조회 1,927
13년 전 조회 8,496
13년 전 조회 3,414
13년 전 조회 2,111
13년 전 조회 2,324
13년 전 조회 3,952
13년 전 조회 2,174
13년 전 조회 2만
13년 전 조회 3,993
13년 전 조회 3,847
13년 전 조회 5,890
13년 전 조회 3,125
13년 전 조회 1,946
13년 전 조회 2,140
13년 전 조회 1.7만
13년 전 조회 1,931
13년 전 조회 3,211
13년 전 조회 2,782
13년 전 조회 2,417
13년 전 조회 2,288
13년 전 조회 2,437
13년 전 조회 2.4만