사바이캅

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,391
13년 전 조회 6,937
13년 전 조회 2,631
13년 전 조회 3,820
13년 전 조회 2,982
13년 전 조회 1,965
13년 전 조회 3,651
13년 전 조회 3,296
13년 전 조회 2,130
13년 전 조회 1,953
13년 전 조회 4,572
13년 전 조회 2,951
13년 전 조회 1,883
13년 전 조회 1,857
13년 전 조회 9,464
13년 전 조회 6,005
13년 전 조회 9,101
13년 전 조회 2,031
13년 전 조회 2,751
13년 전 조회 3,126
13년 전 조회 2,587
13년 전 조회 4,042
13년 전 조회 2,361
13년 전 조회 2,486
13년 전 조회 1,883
13년 전 조회 2,552
13년 전 조회 2,627
13년 전 조회 1.4만
13년 전 조회 2,476
13년 전 조회 5,930
13년 전 조회 2,939
13년 전 조회 2,035
13년 전 조회 2,349
13년 전 조회 5,133
13년 전 조회 2,279
13년 전 조회 5,931
13년 전 조회 2,006
13년 전 조회 2,202
13년 전 조회 2,491
13년 전 조회 1만
13년 전 조회 2,836
13년 전 조회 3,923
13년 전 조회 3,052
13년 전 조회 3,256
13년 전 조회 3,376
13년 전 조회 2,314
13년 전 조회 2,519
13년 전 조회 1.9만
13년 전 조회 3,733
13년 전 조회 5,717
13년 전 조회 2,949
13년 전 조회 3,497
13년 전 조회 3,398
13년 전 조회 1.7만
13년 전 조회 2,311
13년 전 조회 2,286
13년 전 조회 2,041
13년 전 조회 3,002
13년 전 조회 4,548
13년 전 조회 4,639
13년 전 조회 3,511
13년 전 조회 6,912
13년 전 조회 4,180
13년 전 조회 7,277
13년 전 조회 2,993
13년 전 조회 2.2만
13년 전 조회 2,248
13년 전 조회 2,761
13년 전 조회 1.8만
13년 전 조회 3,987
13년 전 조회 2,367
13년 전 조회 5,004
13년 전 조회 2,548
13년 전 조회 2,699
13년 전 조회 2,132
13년 전 조회 3,898
13년 전 조회 2,881
13년 전 조회 2,032
13년 전 조회 2,150
13년 전 조회 8,732
13년 전 조회 3,620
13년 전 조회 2,298
13년 전 조회 2,526
13년 전 조회 4,145
13년 전 조회 2,360
13년 전 조회 2.1만
13년 전 조회 4,190
13년 전 조회 4,025
13년 전 조회 6,075
13년 전 조회 3,280
13년 전 조회 2,130
13년 전 조회 2,308
13년 전 조회 1.7만
13년 전 조회 2,123
13년 전 조회 3,407
13년 전 조회 2,963
13년 전 조회 2,591
13년 전 조회 2,469
13년 전 조회 2,628
13년 전 조회 2.5만