사바이캅

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,024
13년 전 조회 6,559
13년 전 조회 2,161
13년 전 조회 3,418
13년 전 조회 2,505
13년 전 조회 1,482
13년 전 조회 3,209
13년 전 조회 2,860
13년 전 조회 1,664
13년 전 조회 1,550
13년 전 조회 4,089
13년 전 조회 2,506
13년 전 조회 1,450
13년 전 조회 1,387
13년 전 조회 9,020
13년 전 조회 5,523
13년 전 조회 8,642
13년 전 조회 1,601
13년 전 조회 2,299
13년 전 조회 2,740
13년 전 조회 2,130
13년 전 조회 3,642
13년 전 조회 1,859
13년 전 조회 2,012
13년 전 조회 1,447
13년 전 조회 2,049
13년 전 조회 2,169
13년 전 조회 1.3만
13년 전 조회 1,984
13년 전 조회 5,477
13년 전 조회 2,490
13년 전 조회 1,542
13년 전 조회 1,861
13년 전 조회 4,747
13년 전 조회 1,820
13년 전 조회 5,465
13년 전 조회 1,540
13년 전 조회 1,723
13년 전 조회 2,043
13년 전 조회 9,545
13년 전 조회 2,370
13년 전 조회 3,433
13년 전 조회 2,619
13년 전 조회 2,775
13년 전 조회 2,937
13년 전 조회 1,882
13년 전 조회 2,069
13년 전 조회 1.8만
13년 전 조회 3,252
13년 전 조회 5,285
13년 전 조회 2,513
13년 전 조회 3,034
13년 전 조회 2,981
13년 전 조회 1.7만
13년 전 조회 1,876
13년 전 조회 1,820
13년 전 조회 1,608
13년 전 조회 2,617
13년 전 조회 4,099
13년 전 조회 4,228
13년 전 조회 3,106
13년 전 조회 6,477
13년 전 조회 3,740
13년 전 조회 6,867
13년 전 조회 2,564
13년 전 조회 2.2만
13년 전 조회 1,805
13년 전 조회 2,341
13년 전 조회 1.8만
13년 전 조회 3,570
13년 전 조회 1,896
13년 전 조회 4,573
13년 전 조회 2,086
13년 전 조회 2,229
13년 전 조회 1,680
13년 전 조회 3,431
13년 전 조회 2,395
13년 전 조회 1,562
13년 전 조회 1,712
13년 전 조회 8,291
13년 전 조회 3,190
13년 전 조회 1,879
13년 전 조회 2,104
13년 전 조회 3,771
13년 전 조회 1,943
13년 전 조회 2만
13년 전 조회 3,834
13년 전 조회 3,587
13년 전 조회 5,709
13년 전 조회 2,846
13년 전 조회 1,695
13년 전 조회 1,894
13년 전 조회 1.7만
13년 전 조회 1,630
13년 전 조회 3,014
13년 전 조회 2,574
13년 전 조회 2,157
13년 전 조회 2,033
13년 전 조회 2,179
13년 전 조회 2.4만