사바이캅

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,111
13년 전 조회 5,640
13년 전 조회 1,275
13년 전 조회 2,539
13년 전 조회 1,642
13년 전 조회 611
13년 전 조회 2,324
13년 전 조회 2,075
13년 전 조회 764
13년 전 조회 711
13년 전 조회 3,203
13년 전 조회 1,620
13년 전 조회 637
13년 전 조회 492
13년 전 조회 8,128
13년 전 조회 4,627
13년 전 조회 7,823
13년 전 조회 775
13년 전 조회 1,412
13년 전 조회 1,829
13년 전 조회 1,236
13년 전 조회 2,735
13년 전 조회 944
13년 전 조회 1,155
13년 전 조회 644
13년 전 조회 1,170
13년 전 조회 1,361
13년 전 조회 1.3만
13년 전 조회 1,086
13년 전 조회 4,613
13년 전 조회 1,588
13년 전 조회 636
13년 전 조회 927
13년 전 조회 3,738
13년 전 조회 914
13년 전 조회 4,568
13년 전 조회 654
13년 전 조회 823
13년 전 조회 1,223
13년 전 조회 8,749
13년 전 조회 1,469
13년 전 조회 2,497
13년 전 조회 1,647
13년 전 조회 1,910
13년 전 조회 1,999
13년 전 조회 1,070
13년 전 조회 1,259
13년 전 조회 1.8만
13년 전 조회 2,315
13년 전 조회 4,346
13년 전 조회 1,672
13년 전 조회 2,095
13년 전 조회 2,149
13년 전 조회 1.6만
13년 전 조회 1,067
13년 전 조회 925
13년 전 조회 714
13년 전 조회 1,609
13년 전 조회 3,189
13년 전 조회 3,445
13년 전 조회 2,112
13년 전 조회 5,647
13년 전 조회 2,802
13년 전 조회 6,017
13년 전 조회 1,627
13년 전 조회 2.1만
13년 전 조회 895
13년 전 조회 1,389
13년 전 조회 1.7만
13년 전 조회 2,776
13년 전 조회 1,019
13년 전 조회 3,729
13년 전 조회 1,183
13년 전 조회 1,338
13년 전 조회 782
13년 전 조회 2,511
13년 전 조회 1,508
13년 전 조회 647
13년 전 조회 896
13년 전 조회 7,460
13년 전 조회 2,395
13년 전 조회 930
13년 전 조회 1,303
13년 전 조회 2,873
13년 전 조회 1,024
13년 전 조회 1.9만
13년 전 조회 2,930
13년 전 조회 2,646
13년 전 조회 4,811
13년 전 조회 1,929
13년 전 조회 807
13년 전 조회 989
13년 전 조회 1.6만
13년 전 조회 805
13년 전 조회 2,208
13년 전 조회 1,765
13년 전 조회 1,297
13년 전 조회 1,155
13년 전 조회 1,290
13년 전 조회 2.3만
🐛 버그신고