사바이캅

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,258
13년 전 조회 5,776
13년 전 조회 1,409
13년 전 조회 2,684
13년 전 조회 1,789
13년 전 조회 761
13년 전 조회 2,461
13년 전 조회 2,210
13년 전 조회 908
13년 전 조회 846
13년 전 조회 3,346
13년 전 조회 1,761
13년 전 조회 785
13년 전 조회 621
13년 전 조회 8,271
13년 전 조회 4,769
13년 전 조회 7,963
13년 전 조회 915
13년 전 조회 1,556
13년 전 조회 1,966
13년 전 조회 1,374
13년 전 조회 2,870
13년 전 조회 1,077
13년 전 조회 1,293
13년 전 조회 781
13년 전 조회 1,301
13년 전 조회 1,503
13년 전 조회 1.3만
13년 전 조회 1,222
13년 전 조회 4,757
13년 전 조회 1,730
13년 전 조회 769
13년 전 조회 1,070
13년 전 조회 3,885
13년 전 조회 1,055
13년 전 조회 4,704
13년 전 조회 800
13년 전 조회 956
13년 전 조회 1,363
13년 전 조회 8,895
13년 전 조회 1,601
13년 전 조회 2,648
13년 전 조회 1,803
13년 전 조회 2,044
13년 전 조회 2,137
13년 전 조회 1,212
13년 전 조회 1,403
13년 전 조회 1.8만
13년 전 조회 2,452
13년 전 조회 4,491
13년 전 조회 1,822
13년 전 조회 2,228
13년 전 조회 2,290
13년 전 조회 1.6만
13년 전 조회 1,214
13년 전 조회 1,061
13년 전 조회 844
13년 전 조회 1,734
13년 전 조회 3,329
13년 전 조회 3,557
13년 전 조회 2,249
13년 전 조회 5,794
13년 전 조회 2,947
13년 전 조회 6,147
13년 전 조회 1,758
13년 전 조회 2.1만
13년 전 조회 1,047
13년 전 조회 1,534
13년 전 조회 1.7만
13년 전 조회 2,908
13년 전 조회 1,158
13년 전 조회 3,877
13년 전 조회 1,325
13년 전 조회 1,468
13년 전 조회 914
13년 전 조회 2,651
13년 전 조회 1,642
13년 전 조회 790
13년 전 조회 1,041
13년 전 조회 7,598
13년 전 조회 2,534
13년 전 조회 1,065
13년 전 조회 1,435
13년 전 조회 2,999
13년 전 조회 1,155
13년 전 조회 1.9만
13년 전 조회 3,078
13년 전 조회 2,797
13년 전 조회 4,956
13년 전 조회 2,069
13년 전 조회 939
13년 전 조회 1,128
13년 전 조회 1.6만
13년 전 조회 919
13년 전 조회 2,342
13년 전 조회 1,885
13년 전 조회 1,428
13년 전 조회 1,292
13년 전 조회 1,428
13년 전 조회 2.4만
🐛 버그신고