사바이캅

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,425
13년 전 조회 6,974
13년 전 조회 2,754
13년 전 조회 3,874
13년 전 조회 3,112
13년 전 조회 2,077
13년 전 조회 3,770
13년 전 조회 3,341
13년 전 조회 2,248
13년 전 조회 1,998
13년 전 조회 4,701
13년 전 조회 3,081
13년 전 조회 1,928
13년 전 조회 1,976
13년 전 조회 9,589
13년 전 조회 6,147
13년 전 조회 9,142
13년 전 조회 2,081
13년 전 조회 2,901
13년 전 조회 3,166
13년 전 조회 2,704
13년 전 조회 4,089
13년 전 조회 2,473
13년 전 조회 2,610
13년 전 조회 1,930
13년 전 조회 2,670
13년 전 조회 2,681
13년 전 조회 1.4만
13년 전 조회 2,597
13년 전 조회 5,971
13년 전 조회 3,088
13년 전 조회 2,151
13년 전 조회 2,458
13년 전 조회 5,251
13년 전 조회 2,387
13년 전 조회 6,047
13년 전 조회 2,129
13년 전 조회 2,322
13년 전 조회 2,542
13년 전 조회 1만
13년 전 조회 2,966
13년 전 조회 4,038
13년 전 조회 3,169
13년 전 조회 3,383
13년 전 조회 3,487
13년 전 조회 2,350
13년 전 조회 2,562
13년 전 조회 1.9만
13년 전 조회 3,845
13년 전 조회 5,821
13년 전 조회 2,982
13년 전 조회 3,606
13년 전 조회 3,436
13년 전 조회 1.7만
13년 전 조회 2,341
13년 전 조회 2,409
13년 전 조회 2,178
13년 전 조회 3,121
13년 전 조회 4,657
13년 전 조회 4,681
13년 전 조회 3,627
13년 전 조회 6,947
13년 전 조회 4,288
13년 전 조회 7,301
13년 전 조회 3,102
13년 전 조회 2.2만
13년 전 조회 2,351
13년 전 조회 2,854
13년 전 조회 1.8만
13년 전 조회 4,021
13년 전 조회 2,482
13년 전 조회 5,030
13년 전 조회 2,668
13년 전 조회 2,816
13년 전 조회 2,249
13년 전 조회 4,008
13년 전 조회 2,999
13년 전 조회 2,149
13년 전 조회 2,180
13년 전 조회 8,766
13년 전 조회 3,656
13년 전 조회 2,401
13년 전 조회 2,559
13년 전 조회 4,181
13년 전 조회 2,470
13년 전 조회 2.1만
13년 전 조회 4,229
13년 전 조회 4,136
13년 전 조회 6,106
13년 전 조회 3,380
13년 전 조회 2,227
13년 전 조회 2,407
13년 전 조회 1.7만
13년 전 조회 2,217
13년 전 조회 3,445
13년 전 조회 3,002
13년 전 조회 2,683
13년 전 조회 2,579
13년 전 조회 2,737
13년 전 조회 2.5만