사바이캅

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,165
13년 전 조회 5,693
13년 전 조회 1,339
13년 전 조회 2,594
13년 전 조회 1,704
13년 전 조회 666
13년 전 조회 2,381
13년 전 조회 2,129
13년 전 조회 812
13년 전 조회 769
13년 전 조회 3,262
13년 전 조회 1,666
13년 전 조회 695
13년 전 조회 555
13년 전 조회 8,180
13년 전 조회 4,682
13년 전 조회 7,878
13년 전 조회 828
13년 전 조회 1,464
13년 전 조회 1,882
13년 전 조회 1,284
13년 전 조회 2,792
13년 전 조회 996
13년 전 조회 1,208
13년 전 조회 696
13년 전 조회 1,230
13년 전 조회 1,416
13년 전 조회 1.3만
13년 전 조회 1,139
13년 전 조회 4,669
13년 전 조회 1,647
13년 전 조회 693
13년 전 조회 979
13년 전 조회 3,793
13년 전 조회 961
13년 전 조회 4,625
13년 전 조회 710
13년 전 조회 865
13년 전 조회 1,277
13년 전 조회 8,801
13년 전 조회 1,513
13년 전 조회 2,559
13년 전 조회 1,699
13년 전 조회 1,958
13년 전 조회 2,053
13년 전 조회 1,121
13년 전 조회 1,311
13년 전 조회 1.8만
13년 전 조회 2,368
13년 전 조회 4,402
13년 전 조회 1,729
13년 전 조회 2,154
13년 전 조회 2,200
13년 전 조회 1.6만
13년 전 조회 1,123
13년 전 조회 974
13년 전 조회 763
13년 전 조회 1,657
13년 전 조회 3,239
13년 전 조회 3,477
13년 전 조회 2,162
13년 전 조회 5,702
13년 전 조회 2,850
13년 전 조회 6,064
13년 전 조회 1,669
13년 전 조회 2.1만
13년 전 조회 948
13년 전 조회 1,436
13년 전 조회 1.7만
13년 전 조회 2,823
13년 전 조회 1,067
13년 전 조회 3,784
13년 전 조회 1,235
13년 전 조회 1,386
13년 전 조회 827
13년 전 조회 2,564
13년 전 조회 1,549
13년 전 조회 702
13년 전 조회 951
13년 전 조회 7,509
13년 전 조회 2,444
13년 전 조회 976
13년 전 조회 1,353
13년 전 조회 2,919
13년 전 조회 1,066
13년 전 조회 1.9만
13년 전 조회 2,986
13년 전 조회 2,695
13년 전 조회 4,863
13년 전 조회 1,972
13년 전 조회 852
13년 전 조회 1,038
13년 전 조회 1.6만
13년 전 조회 831
13년 전 조회 2,257
13년 전 조회 1,810
13년 전 조회 1,342
13년 전 조회 1,206
13년 전 조회 1,334
13년 전 조회 2.3만
🐛 버그신고