사바이캅

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,508
13년 전 조회 6,040
13년 전 조회 1,652
13년 전 조회 2,937
13년 전 조회 2,004
13년 전 조회 969
13년 전 조회 2,682
13년 전 조회 2,416
13년 전 조회 1,148
13년 전 조회 1,086
13년 전 조회 3,541
13년 전 조회 1,972
13년 전 조회 996
13년 전 조회 854
13년 전 조회 8,479
13년 전 조회 4,982
13년 전 조회 8,178
13년 전 조회 1,127
13년 전 조회 1,763
13년 전 조회 2,223
13년 전 조회 1,584
13년 전 조회 3,128
13년 전 조회 1,308
13년 전 조회 1,491
13년 전 조회 990
13년 전 조회 1,535
13년 전 조회 1,707
13년 전 조회 1.3만
13년 전 조회 1,446
13년 전 조회 5,014
13년 전 조회 1,967
13년 전 조회 999
13년 전 조회 1,294
13년 전 조회 4,156
13년 전 조회 1,290
13년 전 조회 4,908
13년 전 조회 1,012
13년 전 조회 1,176
13년 전 조회 1,612
13년 전 조회 9,132
13년 전 조회 1,840
13년 전 조회 2,895
13년 전 조회 2,060
13년 전 조회 2,282
13년 전 조회 2,395
13년 전 조회 1,420
13년 전 조회 1,620
13년 전 조회 1.8만
13년 전 조회 2,706
13년 전 조회 4,747
13년 전 조회 2,039
13년 전 조회 2,474
13년 전 조회 2,523
13년 전 조회 1.6만
13년 전 조회 1,424
13년 전 조회 1,293
13년 전 조회 1,078
13년 전 조회 2,027
13년 전 조회 3,549
13년 전 조회 3,798
13년 전 조회 2,518
13년 전 조회 6,038
13년 전 조회 3,185
13년 전 조회 6,429
13년 전 조회 2,027
13년 전 조회 2.1만
13년 전 조회 1,267
13년 전 조회 1,808
13년 전 조회 1.7만
13년 전 조회 3,118
13년 전 조회 1,386
13년 전 조회 4,122
13년 전 조회 1,559
13년 전 조회 1,699
13년 전 조회 1,153
13년 전 조회 2,873
13년 전 조회 1,871
13년 전 조회 1,019
13년 전 조회 1,256
13년 전 조회 7,838
13년 전 조회 2,756
13년 전 조회 1,337
13년 전 조회 1,679
13년 전 조회 3,276
13년 전 조회 1,430
13년 전 조회 2만
13년 전 조회 3,352
13년 전 조회 3,025
13년 전 조회 5,208
13년 전 조회 2,316
13년 전 조회 1,176
13년 전 조회 1,396
13년 전 조회 1.6만
13년 전 조회 1,133
13년 전 조회 2,566
13년 전 조회 2,107
13년 전 조회 1,643
13년 전 조회 1,529
13년 전 조회 1,662
13년 전 조회 2.4만