사바이캅

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,794
13년 전 조회 6,320
13년 전 조회 1,892
13년 전 조회 3,220
13년 전 조회 2,242
13년 전 조회 1,214
13년 전 조회 2,937
13년 전 조회 2,672
13년 전 조회 1,376
13년 전 조회 1,360
13년 전 조회 3,794
13년 전 조회 2,228
13년 전 조회 1,260
13년 전 조회 1,113
13년 전 조회 8,732
13년 전 조회 5,240
13년 전 조회 8,437
13년 전 조회 1,390
13년 전 조회 2,014
13년 전 조회 2,513
13년 전 조회 1,827
13년 전 조회 3,399
13년 전 조회 1,559
13년 전 조회 1,741
13년 전 조회 1,244
13년 전 조회 1,777
13년 전 조회 1,977
13년 전 조회 1.3만
13년 전 조회 1,707
13년 전 조회 5,290
13년 전 조회 2,227
13년 전 조회 1,258
13년 전 조회 1,547
13년 전 조회 4,443
13년 전 조회 1,544
13년 전 조회 5,171
13년 전 조회 1,277
13년 전 조회 1,422
13년 전 조회 1,865
13년 전 조회 9,412
13년 전 조회 2,083
13년 전 조회 3,150
13년 전 조회 2,324
13년 전 조회 2,527
13년 전 조회 2,659
13년 전 조회 1,699
13년 전 조회 1,874
13년 전 조회 1.8만
13년 전 조회 2,974
13년 전 조회 5,002
13년 전 조회 2,302
13년 전 조회 2,740
13년 전 조회 2,778
13년 전 조회 1.7만
13년 전 조회 1,676
13년 전 조회 1,559
13년 전 조회 1,341
13년 전 조회 2,297
13년 전 조회 3,797
13년 전 조회 4,048
13년 전 조회 2,822
13년 전 조회 6,289
13년 전 조회 3,444
13년 전 조회 6,682
13년 전 조회 2,276
13년 전 조회 2.1만
13년 전 조회 1,544
13년 전 조회 2,058
13년 전 조회 1.7만
13년 전 조회 3,405
13년 전 조회 1,624
13년 전 조회 4,384
13년 전 조회 1,819
13년 전 조회 1,941
13년 전 조회 1,405
13년 전 조회 3,168
13년 전 조회 2,140
13년 전 조회 1,290
13년 전 조회 1,532
13년 전 조회 8,096
13년 전 조회 3,013
13년 전 조회 1,590
13년 전 조회 1,930
13년 전 조회 3,551
13년 전 조회 1,680
13년 전 조회 2만
13년 전 조회 3,634
13년 전 조회 3,278
13년 전 조회 5,503
13년 전 조회 2,598
13년 전 조회 1,427
13년 전 조회 1,632
13년 전 조회 1.7만
13년 전 조회 1,362
13년 전 조회 2,825
13년 전 조회 2,377
13년 전 조회 1,884
13년 전 조회 1,779
13년 전 조회 1,904
13년 전 조회 2.4만