사바이캅

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년 전 조회 5,478
13년 전 조회 7,014
13년 전 조회 2,861
13년 전 조회 3,920
13년 전 조회 3,217
13년 전 조회 2,169
13년 전 조회 3,874
13년 전 조회 3,377
13년 전 조회 2,355
13년 전 조회 2,040
13년 전 조회 4,786
13년 전 조회 3,179
13년 전 조회 1,963
13년 전 조회 2,063
13년 전 조회 9,708
13년 전 조회 6,248
13년 전 조회 9,185
13년 전 조회 2,116
13년 전 조회 2,995
13년 전 조회 3,214
13년 전 조회 2,808
13년 전 조회 4,131
13년 전 조회 2,579
13년 전 조회 2,703
13년 전 조회 1,965
13년 전 조회 2,770
13년 전 조회 2,722
13년 전 조회 1.4만
13년 전 조회 2,705
13년 전 조회 6,010
13년 전 조회 3,211
13년 전 조회 2,253
13년 전 조회 2,568
13년 전 조회 5,356
13년 전 조회 2,494
13년 전 조회 6,164
13년 전 조회 2,230
13년 전 조회 2,441
13년 전 조회 2,574
13년 전 조회 1만
13년 전 조회 3,055
13년 전 조회 4,138
13년 전 조회 3,271
13년 전 조회 3,495
13년 전 조회 3,606
13년 전 조회 2,396
13년 전 조회 2,609
13년 전 조회 1.9만
13년 전 조회 3,955
13년 전 조회 5,926
13년 전 조회 3,023
13년 전 조회 3,713
13년 전 조회 3,468
13년 전 조회 1.7만
13년 전 조회 2,387
13년 전 조회 2,518
13년 전 조회 2,292
13년 전 조회 3,238
13년 전 조회 4,772
13년 전 조회 4,722
13년 전 조회 3,744
13년 전 조회 6,982
13년 전 조회 4,398
13년 전 조회 7,340
13년 전 조회 3,217
13년 전 조회 2.2만
13년 전 조회 2,455
13년 전 조회 2,971
13년 전 조회 1.8만
13년 전 조회 4,059
13년 전 조회 2,590
13년 전 조회 5,070
13년 전 조회 2,762
13년 전 조회 2,918
13년 전 조회 2,368
13년 전 조회 4,132
13년 전 조회 3,099
13년 전 조회 2,239
13년 전 조회 2,210
13년 전 조회 8,799
13년 전 조회 3,685
13년 전 조회 2,509
13년 전 조회 2,582
13년 전 조회 4,224
13년 전 조회 2,562
13년 전 조회 2.1만
13년 전 조회 4,264
13년 전 조회 4,228
13년 전 조회 6,145
13년 전 조회 3,489
13년 전 조회 2,351
13년 전 조회 2,515
13년 전 조회 1.7만
13년 전 조회 2,312
13년 전 조회 3,479
13년 전 조회 3,031
13년 전 조회 2,775
13년 전 조회 2,691
13년 전 조회 2,843
13년 전 조회 2.5만