사바이캅

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,143
13년 전 조회 5,671
13년 전 조회 1,309
13년 전 조회 2,572
13년 전 조회 1,681
13년 전 조회 643
13년 전 조회 2,361
13년 전 조회 2,105
13년 전 조회 792
13년 전 조회 745
13년 전 조회 3,230
13년 전 조회 1,650
13년 전 조회 671
13년 전 조회 527
13년 전 조회 8,163
13년 전 조회 4,661
13년 전 조회 7,861
13년 전 조회 808
13년 전 조회 1,446
13년 전 조회 1,859
13년 전 조회 1,261
13년 전 조회 2,767
13년 전 조회 975
13년 전 조회 1,188
13년 전 조회 677
13년 전 조회 1,207
13년 전 조회 1,391
13년 전 조회 1.3만
13년 전 조회 1,115
13년 전 조회 4,648
13년 전 조회 1,625
13년 전 조회 671
13년 전 조회 960
13년 전 조회 3,771
13년 전 조회 947
13년 전 조회 4,600
13년 전 조회 691
13년 전 조회 849
13년 전 조회 1,258
13년 전 조회 8,782
13년 전 조회 1,498
13년 전 조회 2,530
13년 전 조회 1,680
13년 전 조회 1,941
13년 전 조회 2,026
13년 전 조회 1,099
13년 전 조회 1,291
13년 전 조회 1.8만
13년 전 조회 2,353
13년 전 조회 4,382
13년 전 조회 1,707
13년 전 조회 2,127
13년 전 조회 2,183
13년 전 조회 1.6만
13년 전 조회 1,105
13년 전 조회 959
13년 전 조회 745
13년 전 조회 1,639
13년 전 조회 3,223
13년 전 조회 3,457
13년 전 조회 2,143
13년 전 조회 5,678
13년 전 조회 2,836
13년 전 조회 6,044
13년 전 조회 1,654
13년 전 조회 2.1만
13년 전 조회 926
13년 전 조회 1,425
13년 전 조회 1.7만
13년 전 조회 2,805
13년 전 조회 1,047
13년 전 조회 3,762
13년 전 조회 1,213
13년 전 조회 1,365
13년 전 조회 814
13년 전 조회 2,545
13년 전 조회 1,533
13년 전 조회 681
13년 전 조회 928
13년 전 조회 7,494
13년 전 조회 2,428
13년 전 조회 961
13년 전 조회 1,334
13년 전 조회 2,902
13년 전 조회 1,052
13년 전 조회 1.9만
13년 전 조회 2,964
13년 전 조회 2,678
13년 전 조회 4,845
13년 전 조회 1,960
13년 전 조회 836
13년 전 조회 1,019
13년 전 조회 1.6만
13년 전 조회 818
13년 전 조회 2,242
13년 전 조회 1,792
13년 전 조회 1,331
13년 전 조회 1,186
13년 전 조회 1,319
13년 전 조회 2.3만
🐛 버그신고