DB-Upload, a database utility

What is it?
DB-Upload is a new database utility for MySQL. This utility allows you to upload large .SQL files, that phpMyAdmin cannot handle. This will allow you to restore database backups that are several hundred MB big.

Why not use command line/shell/SSH?
You should use the MySQL command line client and shell access to restore a large database. It's the only way that's 100% reliable. But face it, not all web hosts allow you shell access. In fact, most web hosts won't give you access. If you need to restore a big database what do you do? phpMyAdmin can't handle sql files that are bigger than 5 MB, so you're pretty much screwed. But with DB-Upload, you can upload much bigger databases. During testing, I have uploaded databases of 90 MB and 500 MB. It can probably do larger databases as well.

How does it work?
DB-Upload executes the sql file in parts (of 100 KB), instead of doing it all at once. This way, it doesn't have the timeout problems that most PHP scripts have. In addition, it doesn't have any memory issues, because each part is only 100 KB.

The downside of this is time. It will take a while to upload a decent size database. It took me about 15 minutes to upload a 90 MB database on my localhost. It probably takes longer on a live server.

Instructions

  1. Download the utility: db-upload.php
 1   
 2  
 3/*   
 4DB-Upload Utility - By Dennis Pallett ([email protected])   
 5http://www.nocertainty.com   
 6  
 7Made available under the GNU General Public License (GPL)   
 8http://www.gnu.org/licenses/gpl.txt   
 9*/   
10  
11ob_start  ();   
12error_reporting  (  E_ALL  ^  E_NOTICE  );   
13  
14// Define file sizes   
15DEFINE  (  "KB"  ,  1024  );   
16DEFINE  (  "MB"  ,  1024  *  1024  );   
17DEFINE  (  "GB"  ,  1024  *  1024  *  1024  );   
18  
19$_GET  =  strip_gpc_slashes  (  $_GET  );   
20  
21// Get settings   
22$filename  =  $_GET  [  'filename'  ];   
23$db_host  =  $_GET  [  'db_host'  ];   
24$db_name  =  $_GET  [  'db_name'  ];   
25$db_user  =  $_GET  [  'db_user'  ];   
26$db_password  =  $_GET  [  'db_password'  ];   
27  
28// Are there any settings?   
29if (empty(  $filename  ) OR empty(  $db_host  ) OR empty(  $db_name  ) OR empty(  $db_password  )) {   
30// Show the settings form:   
 1<html>
 2<head>
 3<title>DB-Upload Utility</title>
 4</head>
 5<style type="text/css">   
 6body {   
 7background-color: #EFEFEF;   
 8font-family: Verdana, Arial, Times New Roman;   
 9}   
10  
11h2 {   
12font-size: 120%;   
13font-weight: normal;   
14margin-left: 15px;   
15}   
16  
17th {   
18font-size: 0.7em;   
19text-align: right;   
20padding-right: 10px;   
21vertical-align: top;   
22}   
23  
24td {   
25font-size: 62.5%; /* Resets 1em to 10px */   
26}   
27  
28div {   
29background-color: #FFF;   
30border: 1px solid black;   
31padding: 15px;   
32padding-bottom: 0px;   
33text-align: center;   
34}   
35  
36#submit {   
37font-family: Verdana;   
38font-weight: bold;   
39font-size: 0.7em;   
40padding: 2px 60px;   
41margin-top: 15px;   
42}   
43</style>
44<body>
45<div>
46<h2>DB-Upload Utility</h2>
47<form method="GET">
48<table border="0">
49<tr>
50<th scope="col">Filepath:</th>
51<td><input name="filename" size="100" type="text"/><br/>Path to the SQL on your server, e.g. /home/dennis/public_html/mydb.sql   
52</td>
53</tr>
54<tr>
55<th scope="col">DB Host:</th>
56<td><input name="db_host" type="text" value="localhost"/></td>
57</tr>
58<tr>
59<th scope="col">DB Name:</th>
60<td><input name="db_name" type="text"/></td>
61</tr>
62<tr>
63<th scope="col">DB User:</th>
64<td><input name="db_user" type="text"/></td>
65</tr>
66<tr>
67<th scope="col">DB Password:</th>
68<td><input name="db_password" type="password"/></td>
69</tr>
70</table>
71<input id="submit" type="submit" value="Upload Database"/>
72</form>
73</div>
74</body>
75</html>
= $filesize ) { $eof = true ; } // Close database mysql_close ( $link ); // Create url for next step $nexturl = $_SERVER [ 'PHP_SELF' ]; $nexturl .= '?filename=' . urlencode ( $filename ); $nexturl .= '&db_host=' . urlencode ( $db_host ); $nexturl .= '&db_name=' . urlencode ( $db_name ); $nexturl .= '&db_user=' . urlencode ( $db_user ); $nexturl .= '&db_password=' . urlencode ( $db_password ); $nexturl .= '&begin=' . $newbegin ; // Display status ?> ```DB-Upload Utility

DB-Upload Utility: Status

Size Read:``` echo $newbegin ; ``` out of ``` echo ( $filesize ); ``` bytes ( ``` echo round ( $newbegin / MB , 2 ); ``` out of ``` echo round ( $filesize / MB , 2 ); ``` MB)
Size Executed:``` echo $newbegin ; ``` out of ``` echo ( $filesize ); ``` bytes ( ``` echo round ( $newbegin / MB , 2 ); ``` out of ``` echo round ( $filesize / MB , 2 ); ``` MB)
Percentage Completed:``` echo round (( $newbegin / $filesize )* 100 , 2 ); ``` %
``` if ( $eof === true ) { ```
All Done!

Your SQL file has been uploaded, and all the queries have been successfully executed.

``` } else { ``` ``` // Javascript redirect to next page js_redirect ( $nexturl , 0.5 ); } ```
``` \n" ; echo " \n" ; echo " \n" ; return true ; } // Executes a SQL query function query ( $sql ) { $link = $GLOBALS [ 'link' ]; // do query (and surpress errors); @ mysql_query ( $sql , $link ); } // remove_date_before // Removes all the data before the begin position function remove_data_before () { $fp = $GLOBALS [ 'fp' ]; $begin = $GLOBALS [ 'begin' ]; $size = $GLOBALS [ 'size' ]; // Get number of full steps we can remove $steps = floor ( $begin / $size ); $left = $begin ; // Loop through each block (if there are multiple steps) if ( $begin >= $size ) { for ( $i = 1 ; $i <= $steps ; $i ++) { // Remove 10MB from the begin and file pointer $left = $left \- $size ; fread ( $fp , $size ); } } // Read final bit fread ( $fp , $left ); } // Splits an SQL file/string function PMA_splitSqlFile (& $ret , $sql ) { $sql = trim ( $sql ); $sql_len = strlen ( $sql ); $char = '' ; $string_start = '' ; $in_string = FALSE ; $time0 = time (); for ( $i = 0 ; $i < $sql_len ; ++ $i ) { $char = $sql [ $i ]; // We are in a string, check for not escaped end of strings except for // backquotes that can't be escaped if ( $in_string ) { for (;;) { $i = strpos ( $sql , $string_start , $i ); // No end of string found -> add the current substring to the // returned array if (! $i ) { $ret [] = $sql ; return TRUE ; } // Backquotes or no backslashes before quotes: it's indeed the // end of the string -> exit the loop else if ( $string_start == '`' || $sql [ $i \- 1 ] != '\\\' ) { $string_start = '' ; $in_string = FALSE ; break; } // one or more Backslashes before the presumed end of string... else { // ... first checks for escaped backslashes $j = 2 ; $escaped_backslash = FALSE ; while ( $i \- $j > 0 && $sql [ $i \- $j ] == '\\\' ) { $escaped_backslash = ! $escaped_backslash ; $j ++; } // ... if escaped backslashes: it's really the end of the // string -> exit the loop if ( $escaped_backslash ) { $string_start = '' ; $in_string = FALSE ; break; } // ... else loop else { $i ++; } } // end if...elseif...else } // end for } // end if (in string) // We are not in a string, first check for delimiter... else if ( $char == ';' ) { // if delimiter found, add the parsed part to the returned array $ret [] = substr ( $sql , 0 , $i ); $sql = ltrim ( substr ( $sql , min ( $i \+ 1 , $sql_len ))); $sql_len = strlen ( $sql ); if ( $sql_len ) { $i = - 1 ; } else { // The submited statement(s) end(s) here return TRUE ; } } // end else if (is delimiter) // ... then check for start of a string,... else if (( $char == '"' ) || ( $char == '\'' ) || ( $char == '`' )) { $in_string = TRUE ; $string_start = $char ; } // end else if (is start of string) // ... for start of a comment (and remove this comment if found)... else if ( $char == '#' || ( $char == ' ' && $i > 1 && $sql [ $i \- 2 ] . $sql [ $i \- 1 ] == '--' )) { // starting position of the comment depends on the comment type $start_of_comment = (( $sql [ $i ] == '#' ) ? $i : $i \- 2 ); // if no "\n" exits in the remaining string, checks for "\r" // (Mac eol style) $end_of_comment = ( strpos ( ' ' . $sql , "\012" , $i \+ 2 )) ? strpos ( ' ' . $sql , "\012" , $i \+ 2 ) : strpos
Published At
Categories with Web编程
Tagged with
comments powered by Disqus