사바이캅

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년 전 조회 4,141
13년 전 조회 5,666
13년 전 조회 1,308
13년 전 조회 2,567
13년 전 조회 1,675
13년 전 조회 642
13년 전 조회 2,360
13년 전 조회 2,102
13년 전 조회 790
13년 전 조회 741
13년 전 조회 3,229
13년 전 조회 1,648
13년 전 조회 668
13년 전 조회 525
13년 전 조회 8,158
13년 전 조회 4,659
13년 전 조회 7,860
13년 전 조회 807
13년 전 조회 1,442
13년 전 조회 1,857
13년 전 조회 1,258
13년 전 조회 2,765
13년 전 조회 973
13년 전 조회 1,185
13년 전 조회 675
13년 전 조회 1,206
13년 전 조회 1,388
13년 전 조회 1.3만
13년 전 조회 1,114
13년 전 조회 4,645
13년 전 조회 1,623
13년 전 조회 670
13년 전 조회 955
13년 전 조회 3,767
13년 전 조회 945
13년 전 조회 4,598
13년 전 조회 687
13년 전 조회 847
13년 전 조회 1,254
13년 전 조회 8,780
13년 전 조회 1,496
13년 전 조회 2,528
13년 전 조회 1,676
13년 전 조회 1,937
13년 전 조회 2,024
13년 전 조회 1,095
13년 전 조회 1,288
13년 전 조회 1.8만
13년 전 조회 2,350
13년 전 조회 4,379
13년 전 조회 1,703
13년 전 조회 2,125
13년 전 조회 2,179
13년 전 조회 1.6만
13년 전 조회 1,100
13년 전 조회 956
13년 전 조회 744
13년 전 조회 1,635
13년 전 조회 3,221
13년 전 조회 3,456
13년 전 조회 2,140
13년 전 조회 5,675
13년 전 조회 2,834
13년 전 조회 6,043
13년 전 조회 1,652
13년 전 조회 2.1만
13년 전 조회 924
13년 전 조회 1,422
13년 전 조회 1.7만
13년 전 조회 2,805
13년 전 조회 1,047
13년 전 조회 3,758
13년 전 조회 1,209
13년 전 조회 1,364
13년 전 조회 812
13년 전 조회 2,542
13년 전 조회 1,530
13년 전 조회 677
13년 전 조회 927
13년 전 조회 7,492
13년 전 조회 2,426
13년 전 조회 959
13년 전 조회 1,330
13년 전 조회 2,901
13년 전 조회 1,051
13년 전 조회 1.9만
13년 전 조회 2,961
13년 전 조회 2,677
13년 전 조회 4,842
13년 전 조회 1,958
13년 전 조회 834
13년 전 조회 1,017
13년 전 조회 1.6만
13년 전 조회 817
13년 전 조회 2,241
13년 전 조회 1,790
13년 전 조회 1,328
13년 전 조회 1,184
13년 전 조회 1,319
13년 전 조회 2.3만
🐛 버그신고