사바이캅

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,486
13년 전 조회 7,022
13년 전 조회 2,898
13년 전 조회 3,930
13년 전 조회 3,252
13년 전 조회 2,200
13년 전 조회 3,903
13년 전 조회 3,389
13년 전 조회 2,388
13년 전 조회 2,051
13년 전 조회 4,829
13년 전 조회 3,217
13년 전 조회 1,968
13년 전 조회 2,093
13년 전 조회 9,746
13년 전 조회 6,287
13년 전 조회 9,197
13년 전 조회 2,121
13년 전 조회 3,020
13년 전 조회 3,223
13년 전 조회 2,834
13년 전 조회 4,141
13년 전 조회 2,607
13년 전 조회 2,738
13년 전 조회 1,971
13년 전 조회 2,800
13년 전 조회 2,733
13년 전 조회 1.4만
13년 전 조회 2,734
13년 전 조회 6,020
13년 전 조회 3,243
13년 전 조회 2,288
13년 전 조회 2,599
13년 전 조회 5,386
13년 전 조회 2,523
13년 전 조회 6,203
13년 전 조회 2,266
13년 전 조회 2,470
13년 전 조회 2,585
13년 전 조회 1만
13년 전 조회 3,093
13년 전 조회 4,173
13년 전 조회 3,311
13년 전 조회 3,541
13년 전 조회 3,630
13년 전 조회 2,402
13년 전 조회 2,618
13년 전 조회 1.9만
13년 전 조회 3,978
13년 전 조회 5,963
13년 전 조회 3,031
13년 전 조회 3,746
13년 전 조회 3,475
13년 전 조회 1.7만
13년 전 조회 2,396
13년 전 조회 2,562
13년 전 조회 2,327
13년 전 조회 3,289
13년 전 조회 4,816
13년 전 조회 4,729
13년 전 조회 3,787
13년 전 조회 6,990
13년 전 조회 4,437
13년 전 조회 7,349
13년 전 조회 3,259
13년 전 조회 2.2만
13년 전 조회 2,492
13년 전 조회 3,008
13년 전 조회 1.8만
13년 전 조회 4,075
13년 전 조회 2,619
13년 전 조회 5,080
13년 전 조회 2,806
13년 전 조회 2,952
13년 전 조회 2,416
13년 전 조회 4,179
13년 전 조회 3,141
13년 전 조회 2,289
13년 전 조회 2,213
13년 전 조회 8,816
13년 전 조회 3,700
13년 전 조회 2,543
13년 전 조회 2,592
13년 전 조회 4,237
13년 전 조회 2,599
13년 전 조회 2.1만
13년 전 조회 4,277
13년 전 조회 4,268
13년 전 조회 6,153
13년 전 조회 3,528
13년 전 조회 2,386
13년 전 조회 2,555
13년 전 조회 1.7만
13년 전 조회 2,349
13년 전 조회 3,494
13년 전 조회 3,036
13년 전 조회 2,821
13년 전 조회 2,733
13년 전 조회 2,883
13년 전 조회 2.5만