사바이캅

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,459
13년 전 조회 7,001
13년 전 조회 2,825
13년 전 조회 3,899
13년 전 조회 3,172
13년 전 조회 2,129
13년 전 조회 3,839
13년 전 조회 3,369
13년 전 조회 2,305
13년 전 조회 2,025
13년 전 조회 4,741
13년 전 조회 3,146
13년 전 조회 1,948
13년 전 조회 2,037
13년 전 조회 9,668
13년 전 조회 6,210
13년 전 조회 9,172
13년 전 조회 2,103
13년 전 조회 2,948
13년 전 조회 3,198
13년 전 조회 2,770
13년 전 조회 4,115
13년 전 조회 2,533
13년 전 조회 2,663
13년 전 조회 1,957
13년 전 조회 2,730
13년 전 조회 2,705
13년 전 조회 1.4만
13년 전 조회 2,651
13년 전 조회 5,993
13년 전 조회 3,163
13년 전 조회 2,201
13년 전 조회 2,512
13년 전 조회 5,314
13년 전 조회 2,449
13년 전 조회 6,116
13년 전 조회 2,184
13년 전 조회 2,394
13년 전 조회 2,564
13년 전 조회 1만
13년 전 조회 3,019
13년 전 조회 4,087
13년 전 조회 3,218
13년 전 조회 3,438
13년 전 조회 3,554
13년 전 조회 2,382
13년 전 조회 2,586
13년 전 조회 1.9만
13년 전 조회 3,907
13년 전 조회 5,875
13년 전 조회 3,009
13년 전 조회 3,668
13년 전 조회 3,457
13년 전 조회 1.7만
13년 전 조회 2,372
13년 전 조회 2,458
13년 전 조회 2,236
13년 전 조회 3,180
13년 전 조회 4,722
13년 전 조회 4,701
13년 전 조회 3,689
13년 전 조회 6,969
13년 전 조회 4,347
13년 전 조회 7,327
13년 전 조회 3,158
13년 전 조회 2.2만
13년 전 조회 2,410
13년 전 조회 2,915
13년 전 조회 1.8만
13년 전 조회 4,044
13년 전 조회 2,548
13년 전 조회 5,058
13년 전 조회 2,728
13년 전 조회 2,871
13년 전 조회 2,321
13년 전 조회 4,079
13년 전 조회 3,043
13년 전 조회 2,197
13년 전 조회 2,201
13년 전 조회 8,784
13년 전 조회 3,674
13년 전 조회 2,452
13년 전 조회 2,575
13년 전 조회 4,201
13년 전 조회 2,508
13년 전 조회 2.1만
13년 전 조회 4,247
13년 전 조회 4,183
13년 전 조회 6,127
13년 전 조회 3,435
13년 전 조회 2,290
13년 전 조회 2,460
13년 전 조회 1.7만
13년 전 조회 2,262
13년 전 조회 3,467
13년 전 조회 3,023
13년 전 조회 2,737
13년 전 조회 2,637
13년 전 조회 2,792
13년 전 조회 2.5만