사바이캅

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년 전 조회 5,397
13년 전 조회 6,943
13년 전 조회 2,667
13년 전 조회 3,827
13년 전 조회 3,018
13년 전 조회 1,994
13년 전 조회 3,687
13년 전 조회 3,305
13년 전 조회 2,159
13년 전 조회 1,959
13년 전 조회 4,607
13년 전 조회 2,979
13년 전 조회 1,890
13년 전 조회 1,893
13년 전 조회 9,500
13년 전 조회 6,042
13년 전 조회 9,110
13년 전 조회 2,040
13년 전 조회 2,791
13년 전 조회 3,131
13년 전 조회 2,610
13년 전 조회 4,055
13년 전 조회 2,393
13년 전 조회 2,519
13년 전 조회 1,893
13년 전 조회 2,588
13년 전 조회 2,635
13년 전 조회 1.4만
13년 전 조회 2,513
13년 전 조회 5,934
13년 전 조회 2,977
13년 전 조회 2,074
13년 전 조회 2,383
13년 전 조회 5,164
13년 전 조회 2,311
13년 전 조회 5,964
13년 전 조회 2,045
13년 전 조회 2,232
13년 전 조회 2,497
13년 전 조회 1만
13년 전 조회 2,874
13년 전 조회 3,951
13년 전 조회 3,079
13년 전 조회 3,291
13년 전 조회 3,413
13년 전 조회 2,318
13년 전 조회 2,529
13년 전 조회 1.9만
13년 전 조회 3,760
13년 전 조회 5,746
13년 전 조회 2,954
13년 전 조회 3,524
13년 전 조회 3,403
13년 전 조회 1.7만
13년 전 조회 2,317
13년 전 조회 2,321
13년 전 조회 2,079
13년 전 조회 3,038
13년 전 조회 4,575
13년 전 조회 4,646
13년 전 조회 3,540
13년 전 조회 6,916
13년 전 조회 4,206
13년 전 조회 7,280
13년 전 조회 3,019
13년 전 조회 2.2만
13년 전 조회 2,275
13년 전 조회 2,789
13년 전 조회 1.8만
13년 전 조회 3,992
13년 전 조회 2,386
13년 전 조회 5,010
13년 전 조회 2,585
13년 전 조회 2,730
13년 전 조회 2,163
13년 전 조회 3,929
13년 전 조회 2,912
13년 전 조회 2,059
13년 전 조회 2,154
13년 전 조회 8,736
13년 전 조회 3,627
13년 전 조회 2,329
13년 전 조회 2,533
13년 전 조회 4,150
13년 전 조회 2,379
13년 전 조회 2.1만
13년 전 조회 4,198
13년 전 조회 4,060
13년 전 조회 6,081
13년 전 조회 3,312
13년 전 조회 2,159
13년 전 조회 2,336
13년 전 조회 1.7만
13년 전 조회 2,144
13년 전 조회 3,415
13년 전 조회 2,969
13년 전 조회 2,615
13년 전 조회 2,500
13년 전 조회 2,657
13년 전 조회 2.5만