사바이캅

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,482
13년 전 조회 7,017
13년 전 조회 2,878
13년 전 조회 3,922
13년 전 조회 3,229
13년 전 조회 2,181
13년 전 조회 3,879
13년 전 조회 3,380
13년 전 조회 2,366
13년 전 조회 2,043
13년 전 조회 4,797
13년 전 조회 3,188
13년 전 조회 1,967
13년 전 조회 2,075
13년 전 조회 9,726
13년 전 조회 6,260
13년 전 조회 9,189
13년 전 조회 2,119
13년 전 조회 3,003
13년 전 조회 3,220
13년 전 조회 2,819
13년 전 조회 4,135
13년 전 조회 2,584
13년 전 조회 2,711
13년 전 조회 1,966
13년 전 조회 2,776
13년 전 조회 2,728
13년 전 조회 1.4만
13년 전 조회 2,712
13년 전 조회 6,013
13년 전 조회 3,225
13년 전 조회 2,266
13년 전 조회 2,582
13년 전 조회 5,362
13년 전 조회 2,501
13년 전 조회 6,175
13년 전 조회 2,246
13년 전 조회 2,451
13년 전 조회 2,579
13년 전 조회 1만
13년 전 조회 3,073
13년 전 조회 4,150
13년 전 조회 3,283
13년 전 조회 3,512
13년 전 조회 3,617
13년 전 조회 2,399
13년 전 조회 2,613
13년 전 조회 1.9만
13년 전 조회 3,962
13년 전 조회 5,939
13년 전 조회 3,028
13년 전 조회 3,726
13년 전 조회 3,473
13년 전 조회 1.7만
13년 전 조회 2,390
13년 전 조회 2,533
13년 전 조회 2,305
13년 전 조회 3,259
13년 전 조회 4,786
13년 전 조회 4,726
13년 전 조회 3,764
13년 전 조회 6,986
13년 전 조회 4,416
13년 전 조회 7,345
13년 전 조회 3,232
13년 전 조회 2.2만
13년 전 조회 2,466
13년 전 조회 2,987
13년 전 조회 1.8만
13년 전 조회 4,067
13년 전 조회 2,598
13년 전 조회 5,076
13년 전 조회 2,779
13년 전 조회 2,929
13년 전 조회 2,387
13년 전 조회 4,147
13년 전 조회 3,115
13년 전 조회 2,252
13년 전 조회 2,212
13년 전 조회 8,806
13년 전 조회 3,691
13년 전 조회 2,523
13년 전 조회 2,587
13년 전 조회 4,228
13년 전 조회 2,574
13년 전 조회 2.1만
13년 전 조회 4,273
13년 전 조회 4,240
13년 전 조회 6,150
13년 전 조회 3,512
13년 전 조회 2,362
13년 전 조회 2,530
13년 전 조회 1.7만
13년 전 조회 2,334
13년 전 조회 3,484
13년 전 조회 3,034
13년 전 조회 2,788
13년 전 조회 2,701
13년 전 조회 2,859
13년 전 조회 2.5만