사바이캅

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,112
13년 전 조회 5,640
13년 전 조회 1,276
13년 전 조회 2,541
13년 전 조회 1,648
13년 전 조회 616
13년 전 조회 2,325
13년 전 조회 2,077
13년 전 조회 765
13년 전 조회 712
13년 전 조회 3,205
13년 전 조회 1,624
13년 전 조회 639
13년 전 조회 498
13년 전 조회 8,131
13년 전 조회 4,631
13년 전 조회 7,829
13년 전 조회 777
13년 전 조회 1,416
13년 전 조회 1,831
13년 전 조회 1,237
13년 전 조회 2,737
13년 전 조회 949
13년 전 조회 1,158
13년 전 조회 649
13년 전 조회 1,177
13년 전 조회 1,365
13년 전 조회 1.3만
13년 전 조회 1,088
13년 전 조회 4,617
13년 전 조회 1,591
13년 전 조회 641
13년 전 조회 928
13년 전 조회 3,741
13년 전 조회 917
13년 전 조회 4,572
13년 전 조회 655
13년 전 조회 826
13년 전 조회 1,226
13년 전 조회 8,753
13년 전 조회 1,473
13년 전 조회 2,500
13년 전 조회 1,649
13년 전 조회 1,916
13년 전 조회 2,000
13년 전 조회 1,073
13년 전 조회 1,262
13년 전 조회 1.8만
13년 전 조회 2,321
13년 전 조회 4,348
13년 전 조회 1,675
13년 전 조회 2,098
13년 전 조회 2,154
13년 전 조회 1.6만
13년 전 조회 1,069
13년 전 조회 929
13년 전 조회 719
13년 전 조회 1,610
13년 전 조회 3,192
13년 전 조회 3,447
13년 전 조회 2,115
13년 전 조회 5,649
13년 전 조회 2,807
13년 전 조회 6,021
13년 전 조회 1,630
13년 전 조회 2.1만
13년 전 조회 899
13년 전 조회 1,395
13년 전 조회 1.7만
13년 전 조회 2,778
13년 전 조회 1,023
13년 전 조회 3,734
13년 전 조회 1,186
13년 전 조회 1,339
13년 전 조회 785
13년 전 조회 2,517
13년 전 조회 1,509
13년 전 조회 654
13년 전 조회 900
13년 전 조회 7,463
13년 전 조회 2,399
13년 전 조회 936
13년 전 조회 1,307
13년 전 조회 2,875
13년 전 조회 1,028
13년 전 조회 1.9만
13년 전 조회 2,936
13년 전 조회 2,648
13년 전 조회 4,816
13년 전 조회 1,934
13년 전 조회 810
13년 전 조회 993
13년 전 조회 1.6만
13년 전 조회 808
13년 전 조회 2,212
13년 전 조회 1,769
13년 전 조회 1,301
13년 전 조회 1,160
13년 전 조회 1,297
13년 전 조회 2.3만
🐛 버그신고