사바이캅

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,326
13년 전 조회 5,851
13년 전 조회 1,503
13년 전 조회 2,754
13년 전 조회 1,857
13년 전 조회 813
13년 전 조회 2,546
13년 전 조회 2,277
13년 전 조회 973
13년 전 조회 931
13년 전 조회 3,411
13년 전 조회 1,831
13년 전 조회 847
13년 전 조회 699
13년 전 조회 8,352
13년 전 조회 4,845
13년 전 조회 8,042
13년 전 조회 987
13년 전 조회 1,625
13년 전 조회 2,040
13년 전 조회 1,434
13년 전 조회 2,944
13년 전 조회 1,154
13년 전 조회 1,353
13년 전 조회 850
13년 전 조회 1,378
13년 전 조회 1,576
13년 전 조회 1.3만
13년 전 조회 1,302
13년 전 조회 4,840
13년 전 조회 1,806
13년 전 조회 846
13년 전 조회 1,139
13년 전 조회 3,955
13년 전 조회 1,122
13년 전 조회 4,779
13년 전 조회 868
13년 전 조회 1,022
13년 전 조회 1,447
13년 전 조회 8,969
13년 전 조회 1,683
13년 전 조회 2,717
13년 전 조회 1,862
13년 전 조회 2,114
13년 전 조회 2,216
13년 전 조회 1,287
13년 전 조회 1,485
13년 전 조회 1.8만
13년 전 조회 2,535
13년 전 조회 4,552
13년 전 조회 1,892
13년 전 조회 2,300
13년 전 조회 2,368
13년 전 조회 1.6만
13년 전 조회 1,283
13년 전 조회 1,135
13년 전 조회 935
13년 전 조회 1,826
13년 전 조회 3,399
13년 전 조회 3,628
13년 전 조회 2,331
13년 전 조회 5,873
13년 전 조회 3,011
13년 전 조회 6,230
13년 전 조회 1,848
13년 전 조회 2.1만
13년 전 조회 1,120
13년 전 조회 1,616
13년 전 조회 1.7만
13년 전 조회 2,984
13년 전 조회 1,233
13년 전 조회 3,947
13년 전 조회 1,403
13년 전 조회 1,545
13년 전 조회 992
13년 전 조회 2,733
13년 전 조회 1,709
13년 전 조회 863
13년 전 조회 1,110
13년 전 조회 7,681
13년 전 조회 2,609
13년 전 조회 1,142
13년 전 조회 1,509
13년 전 조회 3,083
13년 전 조회 1,238
13년 전 조회 2만
13년 전 조회 3,145
13년 전 조회 2,873
13년 전 조회 5,031
13년 전 조회 2,150
13년 전 조회 1,023
13년 전 조회 1,199
13년 전 조회 1.6만
13년 전 조회 988
13년 전 조회 2,416
13년 전 조회 1,964
13년 전 조회 1,502
13년 전 조회 1,380
13년 전 조회 1,516
13년 전 조회 2.4만