사바이캅

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,135
13년 전 조회 5,662
13년 전 조회 1,303
13년 전 조회 2,559
13년 전 조회 1,668
13년 전 조회 639
13년 전 조회 2,355
13년 전 조회 2,096
13년 전 조회 783
13년 전 조회 733
13년 전 조회 3,225
13년 전 조회 1,644
13년 전 조회 663
13년 전 조회 516
13년 전 조회 8,154
13년 전 조회 4,651
13년 전 조회 7,852
13년 전 조회 802
13년 전 조회 1,435
13년 전 조회 1,853
13년 전 조회 1,252
13년 전 조회 2,756
13년 전 조회 966
13년 전 조회 1,181
13년 전 조회 670
13년 전 조회 1,202
13년 전 조회 1,383
13년 전 조회 1.3만
13년 전 조회 1,112
13년 전 조회 4,639
13년 전 조회 1,616
13년 전 조회 661
13년 전 조회 952
13년 전 조회 3,758
13년 전 조회 940
13년 전 조회 4,589
13년 전 조회 680
13년 전 조회 843
13년 전 조회 1,251
13년 전 조회 8,777
13년 전 조회 1,489
13년 전 조회 2,523
13년 전 조회 1,669
13년 전 조회 1,935
13년 전 조회 2,019
13년 전 조회 1,091
13년 전 조회 1,283
13년 전 조회 1.8만
13년 전 조회 2,342
13년 전 조회 4,373
13년 전 조회 1,699
13년 전 조회 2,120
13년 전 조회 2,173
13년 전 조회 1.6만
13년 전 조회 1,092
13년 전 조회 949
13년 전 조회 736
13년 전 조회 1,631
13년 전 조회 3,217
13년 전 조회 3,454
13년 전 조회 2,132
13년 전 조회 5,670
13년 전 조회 2,830
13년 전 조회 6,038
13년 전 조회 1,650
13년 전 조회 2.1만
13년 전 조회 916
13년 전 조회 1,416
13년 전 조회 1.7만
13년 전 조회 2,799
13년 전 조회 1,044
13년 전 조회 3,751
13년 전 조회 1,203
13년 전 조회 1,358
13년 전 조회 804
13년 전 조회 2,538
13년 전 조회 1,527
13년 전 조회 672
13년 전 조회 921
13년 전 조회 7,483
13년 전 조회 2,422
13년 전 조회 954
13년 전 조회 1,329
13년 전 조회 2,895
13년 전 조회 1,046
13년 전 조회 1.9만
13년 전 조회 2,956
13년 전 조회 2,669
13년 전 조회 4,836
13년 전 조회 1,952
13년 전 조회 827
13년 전 조회 1,014
13년 전 조회 1.6만
13년 전 조회 816
13년 전 조회 2,232
13년 전 조회 1,783
13년 전 조회 1,323
13년 전 조회 1,177
13년 전 조회 1,315
13년 전 조회 2.3만
🐛 버그신고