사바이캅

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,262
13년 전 조회 5,781
13년 전 조회 1,414
13년 전 조회 2,686
13년 전 조회 1,795
13년 전 조회 766
13년 전 조회 2,468
13년 전 조회 2,219
13년 전 조회 913
13년 전 조회 855
13년 전 조회 3,350
13년 전 조회 1,764
13년 전 조회 792
13년 전 조회 627
13년 전 조회 8,275
13년 전 조회 4,772
13년 전 조회 7,969
13년 전 조회 921
13년 전 조회 1,561
13년 전 조회 1,972
13년 전 조회 1,376
13년 전 조회 2,874
13년 전 조회 1,086
13년 전 조회 1,298
13년 전 조회 787
13년 전 조회 1,308
13년 전 조회 1,510
13년 전 조회 1.3만
13년 전 조회 1,226
13년 전 조회 4,763
13년 전 조회 1,734
13년 전 조회 774
13년 전 조회 1,074
13년 전 조회 3,886
13년 전 조회 1,058
13년 전 조회 4,711
13년 전 조회 804
13년 전 조회 961
13년 전 조회 1,370
13년 전 조회 8,901
13년 전 조회 1,607
13년 전 조회 2,654
13년 전 조회 1,805
13년 전 조회 2,048
13년 전 조회 2,139
13년 전 조회 1,215
13년 전 조회 1,407
13년 전 조회 1.8만
13년 전 조회 2,456
13년 전 조회 4,497
13년 전 조회 1,823
13년 전 조회 2,236
13년 전 조회 2,297
13년 전 조회 1.6만
13년 전 조회 1,218
13년 전 조회 1,065
13년 전 조회 851
13년 전 조회 1,740
13년 전 조회 3,333
13년 전 조회 3,561
13년 전 조회 2,254
13년 전 조회 5,799
13년 전 조회 2,951
13년 전 조회 6,155
13년 전 조회 1,766
13년 전 조회 2.1만
13년 전 조회 1,048
13년 전 조회 1,537
13년 전 조회 1.7만
13년 전 조회 2,911
13년 전 조회 1,164
13년 전 조회 3,881
13년 전 조회 1,331
13년 전 조회 1,472
13년 전 조회 920
13년 전 조회 2,656
13년 전 조회 1,645
13년 전 조회 795
13년 전 조회 1,043
13년 전 조회 7,601
13년 전 조회 2,537
13년 전 조회 1,069
13년 전 조회 1,445
13년 전 조회 3,007
13년 전 조회 1,158
13년 전 조회 1.9만
13년 전 조회 3,084
13년 전 조회 2,799
13년 전 조회 4,960
13년 전 조회 2,075
13년 전 조회 952
13년 전 조회 1,132
13년 전 조회 1.6만
13년 전 조회 923
13년 전 조회 2,346
13년 전 조회 1,889
13년 전 조회 1,432
13년 전 조회 1,297
13년 전 조회 1,435
13년 전 조회 2.4만
🐛 버그신고