사바이캅

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,223
13년 전 조회 5,742
13년 전 조회 1,373
13년 전 조회 2,643
13년 전 조회 1,757
13년 전 조회 717
13년 전 조회 2,430
13년 전 조회 2,183
13년 전 조회 865
13년 전 조회 816
13년 전 조회 3,311
13년 전 조회 1,721
13년 전 조회 740
13년 전 조회 594
13년 전 조회 8,230
13년 전 조회 4,738
13년 전 조회 7,931
13년 전 조회 880
13년 전 조회 1,519
13년 전 조회 1,934
13년 전 조회 1,337
13년 전 조회 2,838
13년 전 조회 1,044
13년 전 조회 1,258
13년 전 조회 740
13년 전 조회 1,273
13년 전 조회 1,474
13년 전 조회 1.3만
13년 전 조회 1,192
13년 전 조회 4,717
13년 전 조회 1,689
13년 전 조회 732
13년 전 조회 1,030
13년 전 조회 3,840
13년 전 조회 1,024
13년 전 조회 4,680
13년 전 조회 761
13년 전 조회 915
13년 전 조회 1,329
13년 전 조회 8,861
13년 전 조회 1,566
13년 전 조회 2,606
13년 전 조회 1,761
13년 전 조회 2,013
13년 전 조회 2,106
13년 전 조회 1,174
13년 전 조회 1,358
13년 전 조회 1.8만
13년 전 조회 2,420
13년 전 조회 4,454
13년 전 조회 1,782
13년 전 조회 2,198
13년 전 조회 2,256
13년 전 조회 1.6만
13년 전 조회 1,170
13년 전 조회 1,022
13년 전 조회 812
13년 전 조회 1,705
13년 전 조회 3,292
13년 전 조회 3,521
13년 전 조회 2,209
13년 전 조회 5,748
13년 전 조회 2,899
13년 전 조회 6,117
13년 전 조회 1,725
13년 전 조회 2.1만
13년 전 조회 1,005
13년 전 조회 1,499
13년 전 조회 1.7만
13년 전 조회 2,864
13년 전 조회 1,113
13년 전 조회 3,829
13년 전 조회 1,283
13년 전 조회 1,429
13년 전 조회 887
13년 전 조회 2,617
13년 전 조회 1,602
13년 전 조회 742
13년 전 조회 1,005
13년 전 조회 7,550
13년 전 조회 2,497
13년 전 조회 1,025
13년 전 조회 1,402
13년 전 조회 2,974
13년 전 조회 1,123
13년 전 조회 1.9만
13년 전 조회 3,036
13년 전 조회 2,741
13년 전 조회 4,913
13년 전 조회 2,029
13년 전 조회 892
13년 전 조회 1,092
13년 전 조회 1.6만
13년 전 조회 876
13년 전 조회 2,309
13년 전 조회 1,852
13년 전 조회 1,393
13년 전 조회 1,255
13년 전 조회 1,387
13년 전 조회 2.4만
🐛 버그신고