사바이캅

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,134
13년 전 조회 5,659
13년 전 조회 1,298
13년 전 조회 2,556
13년 전 조회 1,662
13년 전 조회 636
13년 전 조회 2,349
13년 전 조회 2,093
13년 전 조회 782
13년 전 조회 728
13년 전 조회 3,222
13년 전 조회 1,641
13년 전 조회 659
13년 전 조회 514
13년 전 조회 8,154
13년 전 조회 4,645
13년 전 조회 7,847
13년 전 조회 796
13년 전 조회 1,431
13년 전 조회 1,849
13년 전 조회 1,251
13년 전 조회 2,753
13년 전 조회 965
13년 전 조회 1,175
13년 전 조회 666
13년 전 조회 1,197
13년 전 조회 1,382
13년 전 조회 1.3만
13년 전 조회 1,107
13년 전 조회 4,635
13년 전 조회 1,612
13년 전 조회 658
13년 전 조회 947
13년 전 조회 3,754
13년 전 조회 934
13년 전 조회 4,587
13년 전 조회 675
13년 전 조회 838
13년 전 조회 1,247
13년 전 조회 8,772
13년 전 조회 1,486
13년 전 조회 2,519
13년 전 조회 1,667
13년 전 조회 1,932
13년 전 조회 2,016
13년 전 조회 1,088
13년 전 조회 1,280
13년 전 조회 1.8만
13년 전 조회 2,340
13년 전 조회 4,369
13년 전 조회 1,693
13년 전 조회 2,116
13년 전 조회 2,170
13년 전 조회 1.6만
13년 전 조회 1,089
13년 전 조회 945
13년 전 조회 731
13년 전 조회 1,628
13년 전 조회 3,211
13년 전 조회 3,453
13년 전 조회 2,131
13년 전 조회 5,667
13년 전 조회 2,823
13년 전 조회 6,035
13년 전 조회 1,648
13년 전 조회 2.1만
13년 전 조회 915
13년 전 조회 1,410
13년 전 조회 1.7만
13년 전 조회 2,796
13년 전 조회 1,042
13년 전 조회 3,749
13년 전 조회 1,199
13년 전 조회 1,358
13년 전 조회 800
13년 전 조회 2,533
13년 전 조회 1,525
13년 전 조회 668
13년 전 조회 917
13년 전 조회 7,479
13년 전 조회 2,417
13년 전 조회 951
13년 전 조회 1,324
13년 전 조회 2,892
13년 전 조회 1,044
13년 전 조회 1.9만
13년 전 조회 2,952
13년 전 조회 2,668
13년 전 조회 4,829
13년 전 조회 1,950
13년 전 조회 824
13년 전 조회 1,010
13년 전 조회 1.6만
13년 전 조회 815
13년 전 조회 2,228
13년 전 조회 1,782
13년 전 조회 1,321
13년 전 조회 1,176
13년 전 조회 1,313
13년 전 조회 2.3만
🐛 버그신고