사바이캅

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,123
13년 전 조회 5,652
13년 전 조회 1,288
13년 전 조회 2,549
13년 전 조회 1,656
13년 전 조회 625
13년 전 조회 2,337
13년 전 조회 2,084
13년 전 조회 775
13년 전 조회 721
13년 전 조회 3,210
13년 전 조회 1,630
13년 전 조회 647
13년 전 조회 506
13년 전 조회 8,143
13년 전 조회 4,638
13년 전 조회 7,836
13년 전 조회 786
13년 전 조회 1,422
13년 전 조회 1,842
13년 전 조회 1,242
13년 전 조회 2,742
13년 전 조회 957
13년 전 조회 1,167
13년 전 조회 658
13년 전 조회 1,186
13년 전 조회 1,372
13년 전 조회 1.3만
13년 전 조회 1,097
13년 전 조회 4,624
13년 전 조회 1,600
13년 전 조회 649
13년 전 조회 938
13년 전 조회 3,748
13년 전 조회 925
13년 전 조회 4,579
13년 전 조회 666
13년 전 조회 830
13년 전 조회 1,236
13년 전 조회 8,762
13년 전 조회 1,480
13년 전 조회 2,510
13년 전 조회 1,658
13년 전 조회 1,922
13년 전 조회 2,009
13년 전 조회 1,077
13년 전 조회 1,267
13년 전 조회 1.8만
13년 전 조회 2,330
13년 전 조회 4,358
13년 전 조회 1,684
13년 전 조회 2,109
13년 전 조회 2,161
13년 전 조회 1.6만
13년 전 조회 1,079
13년 전 조회 939
13년 전 조회 725
13년 전 조회 1,621
13년 전 조회 3,201
13년 전 조회 3,451
13년 전 조회 2,122
13년 전 조회 5,658
13년 전 조회 2,815
13년 전 조회 6,025
13년 전 조회 1,638
13년 전 조회 2.1만
13년 전 조회 908
13년 전 조회 1,402
13년 전 조회 1.7만
13년 전 조회 2,786
13년 전 조회 1,028
13년 전 조회 3,741
13년 전 조회 1,192
13년 전 조회 1,350
13년 전 조회 791
13년 전 조회 2,525
13년 전 조회 1,516
13년 전 조회 662
13년 전 조회 909
13년 전 조회 7,472
13년 전 조회 2,409
13년 전 조회 941
13년 전 조회 1,315
13년 전 조회 2,883
13년 전 조회 1,036
13년 전 조회 1.9만
13년 전 조회 2,942
13년 전 조회 2,656
13년 전 조회 4,824
13년 전 조회 1,941
13년 전 조회 819
13년 전 조회 1,001
13년 전 조회 1.6만
13년 전 조회 811
13년 전 조회 2,220
13년 전 조회 1,773
13년 전 조회 1,312
13년 전 조회 1,168
13년 전 조회 1,304
13년 전 조회 2.3만
🐛 버그신고