사바이캅

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,378
13년 전 조회 5,901
13년 전 조회 1,556
13년 전 조회 2,809
13년 전 조회 1,908
13년 전 조회 877
13년 전 조회 2,589
13년 전 조회 2,330
13년 전 조회 1,015
13년 전 조회 971
13년 전 조회 3,460
13년 전 조회 1,885
13년 전 조회 895
13년 전 조회 761
13년 전 조회 8,402
13년 전 조회 4,889
13년 전 조회 8,092
13년 전 조회 1,041
13년 전 조회 1,687
13년 전 조회 2,090
13년 전 조회 1,489
13년 전 조회 2,998
13년 전 조회 1,216
13년 전 조회 1,401
13년 전 조회 895
13년 전 조회 1,440
13년 전 조회 1,620
13년 전 조회 1.3만
13년 전 조회 1,353
13년 전 조회 4,897
13년 전 조회 1,863
13년 전 조회 912
13년 전 조회 1,193
13년 전 조회 4,005
13년 전 조회 1,176
13년 전 조회 4,824
13년 전 조회 926
13년 전 조회 1,076
13년 전 조회 1,505
13년 전 조회 9,034
13년 전 조회 1,747
13년 전 조회 2,781
13년 전 조회 1,925
13년 전 조회 2,176
13년 전 조회 2,270
13년 전 조회 1,333
13년 전 조회 1,529
13년 전 조회 1.8만
13년 전 조회 2,598
13년 전 조회 4,619
13년 전 조회 1,945
13년 전 조회 2,368
13년 전 조회 2,438
13년 전 조회 1.6만
13년 전 조회 1,332
13년 전 조회 1,203
13년 전 조회 989
13년 전 조회 1,887
13년 전 조회 3,455
13년 전 조회 3,670
13년 전 조회 2,387
13년 전 조회 5,933
13년 전 조회 3,067
13년 전 조회 6,287
13년 전 조회 1,899
13년 전 조회 2.1만
13년 전 조회 1,178
13년 전 조회 1,674
13년 전 조회 1.7만
13년 전 조회 3,032
13년 전 조회 1,297
13년 전 조회 4,007
13년 전 조회 1,454
13년 전 조회 1,595
13년 전 조회 1,054
13년 전 조회 2,790
13년 전 조회 1,768
13년 전 조회 932
13년 전 조회 1,169
13년 전 조회 7,737
13년 전 조회 2,665
13년 전 조회 1,210
13년 전 조회 1,571
13년 전 조회 3,131
13년 전 조회 1,293
13년 전 조회 2만
13년 전 조회 3,203
13년 전 조회 2,922
13년 전 조회 5,082
13년 전 조회 2,217
13년 전 조회 1,085
13년 전 조회 1,262
13년 전 조회 1.6만
13년 전 조회 1,026
13년 전 조회 2,470
13년 전 조회 2,022
13년 전 조회 1,561
13년 전 조회 1,439
13년 전 조회 1,565
13년 전 조회 2.4만