사바이캅

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,419
13년 전 조회 6,968
13년 전 조회 2,737
13년 전 조회 3,868
13년 전 조회 3,101
13년 전 조회 2,066
13년 전 조회 3,758
13년 전 조회 3,335
13년 전 조회 2,240
13년 전 조회 1,989
13년 전 조회 4,686
13년 전 조회 3,066
13년 전 조회 1,923
13년 전 조회 1,966
13년 전 조회 9,575
13년 전 조회 6,129
13년 전 조회 9,134
13년 전 조회 2,074
13년 전 조회 2,888
13년 전 조회 3,160
13년 전 조회 2,686
13년 전 조회 4,079
13년 전 조회 2,467
13년 전 조회 2,601
13년 전 조회 1,922
13년 전 조회 2,661
13년 전 조회 2,672
13년 전 조회 1.4만
13년 전 조회 2,589
13년 전 조회 5,963
13년 전 조회 3,077
13년 전 조회 2,143
13년 전 조회 2,443
13년 전 조회 5,240
13년 전 조회 2,377
13년 전 조회 6,036
13년 전 조회 2,120
13년 전 조회 2,310
13년 전 조회 2,533
13년 전 조회 1만
13년 전 조회 2,955
13년 전 조회 4,029
13년 전 조회 3,159
13년 전 조회 3,375
13년 전 조회 3,476
13년 전 조회 2,342
13년 전 조회 2,555
13년 전 조회 1.9만
13년 전 조회 3,833
13년 전 조회 5,810
13년 전 조회 2,973
13년 전 조회 3,591
13년 전 조회 3,431
13년 전 조회 1.7만
13년 전 조회 2,336
13년 전 조회 2,401
13년 전 조회 2,171
13년 전 조회 3,112
13년 전 조회 4,642
13년 전 조회 4,678
13년 전 조회 3,614
13년 전 조회 6,938
13년 전 조회 4,280
13년 전 조회 7,300
13년 전 조회 3,092
13년 전 조회 2.2만
13년 전 조회 2,341
13년 전 조회 2,845
13년 전 조회 1.8만
13년 전 조회 4,014
13년 전 조회 2,473
13년 전 조회 5,026
13년 전 조회 2,659
13년 전 조회 2,804
13년 전 조회 2,239
13년 전 조회 3,997
13년 전 조회 2,989
13년 전 조회 2,138
13년 전 조회 2,166
13년 전 조회 8,764
13년 전 조회 3,652
13년 전 조회 2,391
13년 전 조회 2,554
13년 전 조회 4,176
13년 전 조회 2,460
13년 전 조회 2.1만
13년 전 조회 4,223
13년 전 조회 4,128
13년 전 조회 6,103
13년 전 조회 3,371
13년 전 조회 2,220
13년 전 조회 2,397
13년 전 조회 1.7만
13년 전 조회 2,205
13년 전 조회 3,437
13년 전 조회 2,994
13년 전 조회 2,677
13년 전 조회 2,571
13년 전 조회 2,729
13년 전 조회 2.5만