사바이캅

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,246
13년 전 조회 5,769
13년 전 조회 1,398
13년 전 조회 2,670
13년 전 조회 1,778
13년 전 조회 748
13년 전 조회 2,458
13년 전 조회 2,201
13년 전 조회 896
13년 전 조회 836
13년 전 조회 3,337
13년 전 조회 1,749
13년 전 조회 772
13년 전 조회 612
13년 전 조회 8,254
13년 전 조회 4,762
13년 전 조회 7,953
13년 전 조회 905
13년 전 조회 1,544
13년 전 조회 1,955
13년 전 조회 1,365
13년 전 조회 2,863
13년 전 조회 1,069
13년 전 조회 1,283
13년 전 조회 769
13년 전 조회 1,294
13년 전 조회 1,495
13년 전 조회 1.3만
13년 전 조회 1,210
13년 전 조회 4,741
13년 전 조회 1,717
13년 전 조회 758
13년 전 조회 1,061
13년 전 조회 3,875
13년 전 조회 1,048
13년 전 조회 4,697
13년 전 조회 792
13년 전 조회 946
13년 전 조회 1,358
13년 전 조회 8,885
13년 전 조회 1,593
13년 전 조회 2,636
13년 전 조회 1,791
13년 전 조회 2,034
13년 전 조회 2,127
13년 전 조회 1,204
13년 전 조회 1,387
13년 전 조회 1.8만
13년 전 조회 2,443
13년 전 조회 4,487
13년 전 조회 1,810
13년 전 조회 2,222
13년 전 조회 2,281
13년 전 조회 1.6만
13년 전 조회 1,201
13년 전 조회 1,049
13년 전 조회 836
13년 전 조회 1,727
13년 전 조회 3,317
13년 전 조회 3,548
13년 전 조회 2,237
13년 전 조회 5,780
13년 전 조회 2,931
13년 전 조회 6,140
13년 전 조회 1,750
13년 전 조회 2.1만
13년 전 조회 1,037
13년 전 조회 1,525
13년 전 조회 1.7만
13년 전 조회 2,894
13년 전 조회 1,148
13년 전 조회 3,862
13년 전 조회 1,311
13년 전 조회 1,460
13년 전 조회 907
13년 전 조회 2,645
13년 전 조회 1,631
13년 전 조회 772
13년 전 조회 1,028
13년 전 조회 7,582
13년 전 조회 2,524
13년 전 조회 1,055
13년 전 조회 1,420
13년 전 조회 2,990
13년 전 조회 1,143
13년 전 조회 1.9만
13년 전 조회 3,063
13년 전 조회 2,786
13년 전 조회 4,938
13년 전 조회 2,052
13년 전 조회 930
13년 전 조회 1,115
13년 전 조회 1.6만
13년 전 조회 907
13년 전 조회 2,332
13년 전 조회 1,878
13년 전 조회 1,414
13년 전 조회 1,279
13년 전 조회 1,417
13년 전 조회 2.4만
🐛 버그신고