사바이캅

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,415
13년 전 조회 6,962
13년 전 조회 2,729
13년 전 조회 3,860
13년 전 조회 3,093
13년 전 조회 2,056
13년 전 조회 3,743
13년 전 조회 3,329
13년 전 조회 2,226
13년 전 조회 1,985
13년 전 조회 4,680
13년 전 조회 3,050
13년 전 조회 1,917
13년 전 조회 1,952
13년 전 조회 9,565
13년 전 조회 6,117
13년 전 조회 9,130
13년 전 조회 2,071
13년 전 조회 2,870
13년 전 조회 3,154
13년 전 조회 2,680
13년 전 조회 4,076
13년 전 조회 2,456
13년 전 조회 2,588
13년 전 조회 1,916
13년 전 조회 2,651
13년 전 조회 2,661
13년 전 조회 1.4만
13년 전 조회 2,578
13년 전 조회 5,960
13년 전 조회 3,070
13년 전 조회 2,139
13년 전 조회 2,433
13년 전 조회 5,231
13년 전 조회 2,374
13년 전 조회 6,028
13년 전 조회 2,111
13년 전 조회 2,298
13년 전 조회 2,527
13년 전 조회 1만
13년 전 조회 2,946
13년 전 조회 4,023
13년 전 조회 3,143
13년 전 조회 3,361
13년 전 조회 3,466
13년 전 조회 2,335
13년 전 조회 2,549
13년 전 조회 1.9만
13년 전 조회 3,827
13년 전 조회 5,802
13년 전 조회 2,968
13년 전 조회 3,580
13년 전 조회 3,423
13년 전 조회 1.7만
13년 전 조회 2,331
13년 전 조회 2,395
13년 전 조회 2,158
13년 전 조회 3,105
13년 전 조회 4,627
13년 전 조회 4,673
13년 전 조회 3,606
13년 전 조회 6,937
13년 전 조회 4,270
13년 전 조회 7,298
13년 전 조회 3,081
13년 전 조회 2.2만
13년 전 조회 2,328
13년 전 조회 2,840
13년 전 조회 1.8만
13년 전 조회 4,011
13년 전 조회 2,463
13년 전 조회 5,025
13년 전 조회 2,650
13년 전 조회 2,799
13년 전 조회 2,230
13년 전 조회 3,989
13년 전 조회 2,982
13년 전 조회 2,127
13년 전 조회 2,166
13년 전 조회 8,759
13년 전 조회 3,648
13년 전 조회 2,386
13년 전 조회 2,551
13년 전 조회 4,173
13년 전 조회 2,446
13년 전 조회 2.1만
13년 전 조회 4,220
13년 전 조회 4,121
13년 전 조회 6,100
13년 전 조회 3,369
13년 전 조회 2,215
13년 전 조회 2,390
13년 전 조회 1.7만
13년 전 조회 2,201
13년 전 조회 3,433
13년 전 조회 2,988
13년 전 조회 2,670
13년 전 조회 2,560
13년 전 조회 2,719
13년 전 조회 2.5만