pcw Posted March 26, 2009 Share Posted March 26, 2009 Hi, I have this script which is a shortened down version of bigdump. I have split the script over two "pages" using cmd $filename is usually passed to this script and this is why I need it split into two sections. The only problem I am having, is when the link is clicked to confirm that the database should be imported. The filename of the database is posted to the next page, but the second part of the script does not execute. I really am stuck on this and would appreciate any help. <?php $cmd = $_GET['cmd']; if ($cmd=="") { $cmd = "confirm"; } switch($cmd) { case "confirm": // Database configuration $dbhost = 'localhost'; $db = 'moveitho_sitebuilder'; $dbuser = 'myusername'; $dbpass = 'mypassword'; // Other settings (optional) $filename = 'backup/mysql_moveitho_sitebuilder_22_Mar_2009_time_18_41_26.sql.gz'; $linespersession = 3000; $delaypersession = 0; $comment[]='#'; $comment[]='-- '; if ($ajax) ob_start(); define ('DATA_CHUNK_LENGTH',16384); define ('MAX_QUERY_LINES',300); @ini_set('auto_detect_line_endings', true); @set_time_limit(0); foreach ($_REQUEST as $key => $val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i",'', $val); $_REQUEST[$key] = $val; } $error = false; $file = false; if (!$error) { $upload_max_filesize=ini_get("upload_max_filesize"); if (eregi("([0-9]+)K",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024; if (eregi("([0-9]+)M",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024; if (eregi("([0-9]+)G",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024*1024; } $upload_dir=dirname($_SERVER["SCRIPT_FILENAME"]); // Connect to the database if (!$error) { $dbconnection = @mysql_connect($dbhost,$dbuser,$dbpass); if ($dbconnection) $db = mysql_select_db($db); if (!$dbconnection || !$db) { echo ("<p class=\"error\">Database connection failed due to ".mysql_error()."</p>\n"); $error=true; } } else { $dbconnection = false; } // THIS IS THE PAGE WHERE THE BACKUP BUTTON IS LOCATED if (!$error && $filename!="") { echo ("<p><a href=\"bigdump2.php?cmd=restore&start=1&filename=$filename&\">Start Import</a> from $filename into $db at $dbhost</p>\n"); } break; case "restore": print "1: $filename"; // This is just to ensure that the $filename variable was passed to this page if ($filename!="") $curfilename=$filename; // Recognize GZip filename if (eregi("\.gz$",$curfilename)) $gzipmode=true; else $gzipmode=false; if ((!$gzipmode && !$file=@fopen($curfilename,"rt")) || ($gzipmode && !$file=@gzopen($curfilename,"rt"))) { echo ("<p class=\"error\">Can't open ".$curfilename." for import</p>\n"); echo ("<p>Please, check that your dump file name contains only alphanumerical characters, and rename it accordingly, for example: $curfilename.". "<br>Or, specify $filename in bigdump.php with the full filename. ". "<br>Or, you have to upload the $curfilename to the server first.</p>\n"); $error=true; } // START IMPORT SESSION HERE if (!$error && isset($_REQUEST["start"]) && eregi("(\.(gz))$",$curfilename)) { // Start processing queries from $file if (!$error) { $linenumber=$_REQUEST["start"]; $querylines=0; $inparents=false; // Stay processing as long as the $linespersession is not reached or the query is still incomplete while ($linenumber<$_REQUEST["start"]+$linespersession || $query!="") { // Read the whole next line $dumpline = ""; while (!feof($file) && substr ($dumpline, -1) != "\n") { if (!$gzipmode) $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else $dumpline .= gzgets($file, DATA_CHUNK_LENGTH); } if ($dumpline==="") break; // Skip comments and blank lines only if NOT in parents if (!$inparents) { $skipline=false; reset($comment); foreach ($comment as $comment_value) { if (!$inparents && (trim($dumpline)=="" || strpos ($dumpline, $comment_value) === 0)) { $skipline=true; break; } } if ($skipline) { $linenumber++; continue; } } // Remove double back-slashes from the dumpline prior to count the quotes ('\\' can only be within strings) $dumpline_deslashed = str_replace ("\\\\","",$dumpline); // Count ' and \' in the dumpline to avoid query break within a text field ending by ; // Please don't use double quotes ('"')to surround strings, it wont work $parents=substr_count ($dumpline_deslashed, "'")-substr_count ($dumpline_deslashed, "\\'"); if ($parents % 2 != 0) $inparents=!$inparents; // Add the line to query $query .= $dumpline; // Don't count the line if in parents (text fields may include unlimited linebreaks) if (!$inparents) $querylines++; // Stop if query contains more lines as defined by MAX_QUERY_LINES if ($querylines>MAX_QUERY_LINES) { echo ("<p class=\"error\">Stopped at the line $linenumber. </p>"); echo ("<p>At this place the current query includes more than ".MAX_QUERY_LINES." dump lines. That can happen if your dump file was "); echo ("created by some tool which doesn't place a semicolon followed by a linebreak at the end of each query, or if your dump contains "); echo ("extended inserts. Please read the BigDump FAQs for more infos.</p>\n"); $error=true; break; } // Execute query if end of query detected (; as last character) AND NOT in parents if (ereg(";$",trim($dumpline)) && !$inparents) { if (!mysql_query(trim($query), $dbconnection)) { echo ("<p class=\"error\">Error at the line $linenumber: ". trim($dumpline)."</p>\n"); echo ("<p>Query: ".trim(nl2br(htmlentities($query)))."</p>\n"); echo ("<p>MySQL: ".mysql_error()."</p>\n"); $error=true; break; } $totalqueries++; $queries++; $query=""; $querylines=0; } $linenumber++; } } // Finish message and restart the script if ($linenumber<$_REQUEST["start"]+$linespersession) { echo ("<p class=\"successcentr\">MySQL Database: $filename has been restored successfully.</p>\n"); } if ($error) echo ("<p class=\"centr\"><a href=\"".$_SERVER["PHP_SELF"]."\">Start from the beginning</a> (DROP the old tables before restarting)</p>\n"); if ($dbconnection) mysql_close(); if ($file && !$gzipmode) fclose($file); else if ($file && $gzipmode) gzclose($file); } break; } ?> Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/ Share on other sites More sharing options...
pcw Posted March 27, 2009 Author Share Posted March 27, 2009 Can anybody help me with this please? Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795068 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 First of all, what EXACTLY are you importing? Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795081 Share on other sites More sharing options...
pcw Posted March 27, 2009 Author Share Posted March 27, 2009 I am importing the mysql database $filename = 'backup/mysql_moveitho_sitebuilder_22_Mar_2009_time_18_41_26.sql.gz'; as defined in the script Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795082 Share on other sites More sharing options...
Yesideez Posted March 27, 2009 Share Posted March 27, 2009 I must admit to never having restored a database from a backup having not backed up my data before. (Yes, I have paid the price in the past!) Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795110 Share on other sites More sharing options...
pcw Posted March 27, 2009 Author Share Posted March 27, 2009 Ok, so does anybody know how I can fix this? Thanks Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795116 Share on other sites More sharing options...
pcw Posted March 27, 2009 Author Share Posted March 27, 2009 Ok, I have made a slight change but the second part of the script still isnt executing: <?php $cmd = $_GET['cmd']; if ($cmd=="") { $cmd = "confirm"; } switch($cmd) { case "confirm": // Database configuration $dbhost = 'localhost'; $db = 'moveitho_sitebuilder'; $dbuser = 'moveitho_paul'; $dbpass = 'test'; // Other settings (optional) $filename = 'backup/'.$_POST['sqlfile']; $linespersession = 3000; $delaypersession = 0; $comment[]='#'; $comment[]='-- '; if ($ajax) ob_start(); define ('DATA_CHUNK_LENGTH',16384); define ('MAX_QUERY_LINES',300); @ini_set('auto_detect_line_endings', true); @set_time_limit(0); foreach ($_REQUEST as $key => $val) { $val = preg_replace("/[^_A-Za-z0-9-\.&= ]/i",'', $val); $_REQUEST[$key] = $val; } $error = false; $file = false; if (!$error) { $upload_max_filesize=ini_get("upload_max_filesize"); if (eregi("([0-9]+)K",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024; if (eregi("([0-9]+)M",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024; if (eregi("([0-9]+)G",$upload_max_filesize,$tempregs)) $upload_max_filesize=$tempregs[1]*1024*1024*1024; } $upload_dir=dirname($_SERVER["SCRIPT_FILENAME"]); // Connect to the database if (!$error) { $dbconnection = @mysql_connect($dbhost,$dbuser,$dbpass); if ($dbconnection) $db = mysql_select_db($db); if (!$dbconnection || !$db) { echo ("<p class=\"error\">Database connection failed due to ".mysql_error()."</p>\n"); echo ("<p>Edit the database settings in ".$_SERVER["SCRIPT_FILENAME"]." or contact your database provider</p>\n"); $error=true; } } else { $dbconnection = false; } // THIS IS THE PAGE WHERE THE BACKUP BUTTON IS LOCATED if (!$error && $filename!="") { echo "<form action=\"bigdump.php?&start=1&filename=$filename&cmd=restore\" method=\"POST\"> <input type=\"submit\" name=\"start\" value=\"start\"> </form>"; echo ("<p>from $filename into $db at $dbhost</p>\n"); } break; case "restore": // Set current filename ($filename overrides $_REQUEST["fn"] if set) print "1: $filename"; // This confirms that the filename was passed // This part of the script does not execute if ($filename!="") $curfilename=$filename; // Recognize GZip filename if (eregi("\.gz$",$curfilename)) $gzipmode=true; else $gzipmode=false; if ((!$gzipmode && !$file=@fopen($curfilename,"rt")) || ($gzipmode && !$file=@gzopen($curfilename,"rt"))) { echo ("<p class=\"error\">Can't open ".$curfilename." for import</p>\n"); echo ("<p>Please, check that your dump file name contains only alphanumerical characters, and rename it accordingly, for example: $curfilename.". "<br>Or, specify $filename in bigdump.php with the full filename. ". "<br>Or, you have to upload the $curfilename to the server first.</p>\n"); $error=true; } // ******************************************************************************************* // START IMPORT SESSION HERE // ******************************************************************************************* if (!$error && isset($_REQUEST["start"]) && eregi("(\.(gz))$",$curfilename)) { // Start processing queries from $file if (!$error) { $linenumber=$_REQUEST["start"]; $querylines=0; $inparents=false; // Stay processing as long as the $linespersession is not reached or the query is still incomplete while ($linenumber<$_REQUEST["start"]+$linespersession || $query!="") { // Read the whole next line $dumpline = ""; while (!feof($file) && substr ($dumpline, -1) != "\n") { if (!$gzipmode) $dumpline .= fgets($file, DATA_CHUNK_LENGTH); else $dumpline .= gzgets($file, DATA_CHUNK_LENGTH); } if ($dumpline==="") break; // Skip comments and blank lines only if NOT in parents if (!$inparents) { $skipline=false; reset($comment); foreach ($comment as $comment_value) { if (!$inparents && (trim($dumpline)=="" || strpos ($dumpline, $comment_value) === 0)) { $skipline=true; break; } } if ($skipline) { $linenumber++; continue; } } // Remove double back-slashes from the dumpline prior to count the quotes ('\\' can only be within strings) $dumpline_deslashed = str_replace ("\\\\","",$dumpline); // Count ' and \' in the dumpline to avoid query break within a text field ending by ; // Please don't use double quotes ('"')to surround strings, it wont work $parents=substr_count ($dumpline_deslashed, "'")-substr_count ($dumpline_deslashed, "\\'"); if ($parents % 2 != 0) $inparents=!$inparents; // Add the line to query $query .= $dumpline; // Don't count the line if in parents (text fields may include unlimited linebreaks) if (!$inparents) $querylines++; // Stop if query contains more lines as defined by MAX_QUERY_LINES if ($querylines>MAX_QUERY_LINES) { echo ("<p class=\"error\">Stopped at the line $linenumber. </p>"); echo ("<p>At this place the current query includes more than ".MAX_QUERY_LINES." dump lines. That can happen if your dump file was "); echo ("created by some tool which doesn't place a semicolon followed by a linebreak at the end of each query, or if your dump contains "); echo ("extended inserts. Please read the BigDump FAQs for more infos.</p>\n"); $error=true; break; } // Execute query if end of query detected (; as last character) AND NOT in parents if (ereg(";$",trim($dumpline)) && !$inparents) { if (!mysql_query(trim($query), $dbconnection)) { echo ("<p class=\"error\">Error at the line $linenumber: ". trim($dumpline)."</p>\n"); echo ("<p>Query: ".trim(nl2br(htmlentities($query)))."</p>\n"); echo ("<p>MySQL: ".mysql_error()."</p>\n"); $error=true; break; } $totalqueries++; $queries++; $query=""; $querylines=0; } $linenumber++; } } // Finish message and restart the script if ($linenumber<$_REQUEST["start"]+$linespersession) { echo ("<p class=\"successcentr\">MySQL Database: $filename has been restored successfully.</p>\n"); } if ($error) echo ("<p class=\"centr\"><a href=\"".$_SERVER["PHP_SELF"]."\">Start from the beginning</a> (DROP the old tables before restarting)</p>\n"); if ($dbconnection) mysql_close(); if ($file && !$gzipmode) fclose($file); else if ($file && $gzipmode) gzclose($file); } break; } ?> Please help! Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795313 Share on other sites More sharing options...
pcw Posted March 27, 2009 Author Share Posted March 27, 2009 Done it. If anyone needs a similar script, just let me know Link to comment https://forums.phpfreaks.com/topic/151296-solved-mysql-import-db-script-problem/#findComment-795361 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.