사바이캅

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,118
13년 전 조회 5,648
13년 전 조회 1,283
13년 전 조회 2,545
13년 전 조회 1,653
13년 전 조회 620
13년 전 조회 2,331
13년 전 조회 2,083
13년 전 조회 770
13년 전 조회 717
13년 전 조회 3,208
13년 전 조회 1,625
13년 전 조회 644
13년 전 조회 503
13년 전 조회 8,136
13년 전 조회 4,636
13년 전 조회 7,834
13년 전 조회 781
13년 전 조회 1,420
13년 전 조회 1,837
13년 전 조회 1,240
13년 전 조회 2,740
13년 전 조회 953
13년 전 조회 1,162
13년 전 조회 654
13년 전 조회 1,182
13년 전 조회 1,369
13년 전 조회 1.3만
13년 전 조회 1,091
13년 전 조회 4,621
13년 전 조회 1,597
13년 전 조회 645
13년 전 조회 932
13년 전 조회 3,743
13년 전 조회 920
13년 전 조회 4,577
13년 전 조회 661
13년 전 조회 828
13년 전 조회 1,233
13년 전 조회 8,755
13년 전 조회 1,477
13년 전 조회 2,505
13년 전 조회 1,655
13년 전 조회 1,919
13년 전 조회 2,005
13년 전 조회 1,076
13년 전 조회 1,266
13년 전 조회 1.8만
13년 전 조회 2,326
13년 전 조회 4,354
13년 전 조회 1,679
13년 전 조회 2,104
13년 전 조회 2,157
13년 전 조회 1.6만
13년 전 조회 1,077
13년 전 조회 933
13년 전 조회 721
13년 전 조회 1,615
13년 전 조회 3,198
13년 전 조회 3,450
13년 전 조회 2,120
13년 전 조회 5,654
13년 전 조회 2,812
13년 전 조회 6,023
13년 전 조회 1,634
13년 전 조회 2.1만
13년 전 조회 904
13년 전 조회 1,398
13년 전 조회 1.7만
13년 전 조회 2,783
13년 전 조회 1,027
13년 전 조회 3,737
13년 전 조회 1,189
13년 전 조회 1,345
13년 전 조회 788
13년 전 조회 2,521
13년 전 조회 1,514
13년 전 조회 657
13년 전 조회 904
13년 전 조회 7,468
13년 전 조회 2,404
13년 전 조회 938
13년 전 조회 1,311
13년 전 조회 2,879
13년 전 조회 1,031
13년 전 조회 1.9만
13년 전 조회 2,940
13년 전 조회 2,653
13년 전 조회 4,822
13년 전 조회 1,938
13년 전 조회 812
13년 전 조회 997
13년 전 조회 1.6만
13년 전 조회 810
13년 전 조회 2,216
13년 전 조회 1,769
13년 전 조회 1,307
13년 전 조회 1,164
13년 전 조회 1,298
13년 전 조회 2.3만
🐛 버그신고