사바이캅

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,131
13년 전 조회 5,656
13년 전 조회 1,293
13년 전 조회 2,555
13년 전 조회 1,660
13년 전 조회 631
13년 전 조회 2,344
13년 전 조회 2,089
13년 전 조회 781
13년 전 조회 727
13년 전 조회 3,218
13년 전 조회 1,634
13년 전 조회 655
13년 전 조회 512
13년 전 조회 8,148
13년 전 조회 4,643
13년 전 조회 7,842
13년 전 조회 791
13년 전 조회 1,428
13년 전 조회 1,846
13년 전 조회 1,244
13년 전 조회 2,750
13년 전 조회 963
13년 전 조회 1,172
13년 전 조회 664
13년 전 조회 1,194
13년 전 조회 1,378
13년 전 조회 1.3만
13년 전 조회 1,104
13년 전 조회 4,634
13년 전 조회 1,608
13년 전 조회 656
13년 전 조회 942
13년 전 조회 3,749
13년 전 조회 930
13년 전 조회 4,585
13년 전 조회 672
13년 전 조회 833
13년 전 조회 1,244
13년 전 조회 8,767
13년 전 조회 1,485
13년 전 조회 2,516
13년 전 조회 1,665
13년 전 조회 1,928
13년 전 조회 2,015
13년 전 조회 1,086
13년 전 조회 1,277
13년 전 조회 1.8만
13년 전 조회 2,337
13년 전 조회 4,366
13년 전 조회 1,692
13년 전 조회 2,114
13년 전 조회 2,169
13년 전 조회 1.6만
13년 전 조회 1,083
13년 전 조회 944
13년 전 조회 730
13년 전 조회 1,626
13년 전 조회 3,206
13년 전 조회 3,452
13년 전 조회 2,128
13년 전 조회 5,665
13년 전 조회 2,821
13년 전 조회 6,031
13년 전 조회 1,645
13년 전 조회 2.1만
13년 전 조회 913
13년 전 조회 1,409
13년 전 조회 1.7만
13년 전 조회 2,793
13년 전 조회 1,038
13년 전 조회 3,744
13년 전 조회 1,198
13년 전 조회 1,354
13년 전 조회 797
13년 전 조회 2,531
13년 전 조회 1,522
13년 전 조회 666
13년 전 조회 915
13년 전 조회 7,476
13년 전 조회 2,415
13년 전 조회 947
13년 전 조회 1,324
13년 전 조회 2,890
13년 전 조회 1,039
13년 전 조회 1.9만
13년 전 조회 2,949
13년 전 조회 2,664
13년 전 조회 4,829
13년 전 조회 1,948
13년 전 조회 823
13년 전 조회 1,006
13년 전 조회 1.6만
13년 전 조회 814
13년 전 조회 2,224
13년 전 조회 1,778
13년 전 조회 1,315
13년 전 조회 1,172
13년 전 조회 1,311
13년 전 조회 2.3만
🐛 버그신고