사바이캅

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년 전 조회 4,615
13년 전 조회 6,142
13년 전 조회 1,745
13년 전 조회 3,050
13년 전 조회 2,101
13년 전 조회 1,062
13년 전 조회 2,783
13년 전 조회 2,514
13년 전 조회 1,237
13년 전 조회 1,198
13년 전 조회 3,634
13년 전 조회 2,085
13년 전 조회 1,095
13년 전 조회 963
13년 전 조회 8,575
13년 전 조회 5,084
13년 전 조회 8,273
13년 전 조회 1,230
13년 전 조회 1,866
13년 전 조회 2,331
13년 전 조회 1,684
13년 전 조회 3,227
13년 전 조회 1,415
13년 전 조회 1,599
13년 전 조회 1,078
13년 전 조회 1,633
13년 전 조회 1,816
13년 전 조회 1.3만
13년 전 조회 1,547
13년 전 조회 5,118
13년 전 조회 2,067
13년 전 조회 1,094
13년 전 조회 1,395
13년 전 조회 4,247
13년 전 조회 1,389
13년 전 조회 5,022
13년 전 조회 1,113
13년 전 조회 1,272
13년 전 조회 1,704
13년 전 조회 9,251
13년 전 조회 1,945
13년 전 조회 3,006
13년 전 조회 2,165
13년 전 조회 2,383
13년 전 조회 2,502
13년 전 조회 1,528
13년 전 조회 1,711
13년 전 조회 1.8만
13년 전 조회 2,813
13년 전 조회 4,851
13년 전 조회 2,136
13년 전 조회 2,580
13년 전 조회 2,620
13년 전 조회 1.6만
13년 전 조회 1,524
13년 전 조회 1,399
13년 전 조회 1,183
13년 전 조회 2,133
13년 전 조회 3,646
13년 전 조회 3,894
13년 전 조회 2,632
13년 전 조회 6,121
13년 전 조회 3,288
13년 전 조회 6,539
13년 전 조회 2,134
13년 전 조회 2.1만
13년 전 조회 1,372
13년 전 조회 1,912
13년 전 조회 1.7만
13년 전 조회 3,232
13년 전 조회 1,474
13년 전 조회 4,230
13년 전 조회 1,657
13년 전 조회 1,788
13년 전 조회 1,258
13년 전 조회 2,990
13년 전 조회 1,980
13년 전 조회 1,118
13년 전 조회 1,363
13년 전 조회 7,931
13년 전 조회 2,845
13년 전 조회 1,444
13년 전 조회 1,772
13년 전 조회 3,386
13년 전 조회 1,533
13년 전 조회 2만
13년 전 조회 3,451
13년 전 조회 3,131
13년 전 조회 5,315
13년 전 조회 2,436
13년 전 조회 1,271
13년 전 조회 1,490
13년 전 조회 1.7만
13년 전 조회 1,222
13년 전 조회 2,668
13년 전 조회 2,211
13년 전 조회 1,741
13년 전 조회 1,620
13년 전 조회 1,755
13년 전 조회 2.4만