📄 dbbackup.php
字号:
$query = 'SELECT `sid` FROM '.$wbTables['users'].' WHERE `username` = "'.wbDB::escape($pageOwner['username']).'" '; $result = wbDB::runQuery($query); $row = mysql_fetch_assoc($result); if( isset($_COOKIE['remember']) ){ $_SESSION['remember'] = $_COOKIE['remember']; } if( $row['sid'] != $_COOKIE['wbsid2'] ){ setcookie('wbsid2',$row['sid'],time()+2592000,$wbDirPrefix.'/'); $sessionCheckSum = true; } } function recoverInstruct(){ global $pageOwner; $link = wbLinks::local('/Admin/'.$pageOwner['username'].'/Backup?cmd=recover','recover script.'); echo '<h3>Recovery Instructions</h3>'; echo '<ol>'; echo '<li>Copy your most recent backup.txt file to <tt style="white-space:nowrap">'.$this->recoverFile.'</tt>.</li>'; echo '<li>Run the '.$link.'</li>'; echo '<li>Delete the backup.txt and temp.txt files from your server.</li>'; echo '</ol>'; } ////////////////////////////////////////////////////////////////////////////////////////// // // Backup // function backupForm(){ global $lang; $i = 0; $classes[] = ' class="tableRowOdd" '; $classes[] = ' class="tableRowEven" '; echo '<table class="tableRows">'; echo '<tr><th colspan="6">'; echo $lang['tables']; echo '</th></tr>'; $temp = array_keys($this->database); if( $this->useTable){ $doneTable = true; }else{ $doneTable = false; } while(count($temp) > 0){ $table = array_shift($temp); if( ($i%3) == 0){ echo '<tr'.$classes[($i%2)].'>'; } echo '<td>'; echo $table; echo '</td><td>'; if($this->useTable == $table){ echo '<img alt="" height="16" width="16" src="'.wbLinks::getDir('/imgs/icons/hourglass.gif').'" />'; echo '<input type="hidden" name="tables[]" value="'.htmlspecialchars($table).'" />'; $doneTable = false; }elseif( $doneTable ){ echo '<img alt="" height="16" width="16" src="'.wbLinks::getDir('/imgs/icons/tick.gif').'" />'; echo '<input type="hidden" name="tables[]" value="'.htmlspecialchars($table).'" />'; }else{ echo '<input type="checkbox" name="tables[]" value="'.htmlspecialchars($table).'" checked="checked" />'; } echo '</td>'; if( ($i%3) == 2){ echo '</tr>'; } $i++; } if( ($i%3) !== 0){ while( ($i%3) !== 0){ echo '<td colspan="2"></td>'; $i++; } echo '</tr>'; } echo '<tr>'; echo '<td colspan="6" style="text-align:right">'; if( $this->useTable ){ echo '<input type="submit" name="cmd" value="'.$lang['continue'].'" />'; echo '<input type="hidden" name="counts" value="'.htmlspecialchars(serialize($this->backupCounts)).'" />'; echo '<input type="hidden" name="num" readonly="readonly" value="'.htmlspecialchars($_POST['num']).'" />'; echo '<input type="hidden" name="offset" readonly="readonly" value="'.htmlspecialchars($_POST['offset']).'" />'; echo '<input type="hidden" name="useTable" readonly="readonly" value="'.htmlspecialchars($this->useTable).'" />'; }else{ echo '<input type="submit" name="cmd" value="'.$lang['backup_now'].'" />'; //onclick="this.type=\'button\';"this.value=\' . . . \'; } echo '</td></tr>'; echo '</table>'; } //empty existing backup file function backupInit(){ global $rootDir; wbData::loadFileFunctions(); if( !file_exists($this->backupFile) ){ saveFile($this->backupFile,$contents); wbChmod($this->backupFile,0660); } if( $this->compress ){ $this->handle = gzopen($this->backupFile,'wb'); }else{ $this->handle = fopen($this->backupFile,'wb'); } // //index file // $tmpFile = $rootDir.'/userfiles/index.html'; if( !file_exists($tmpFile) ){ $contents = ''; saveFile($tmpFile,$contents); } } function backupFinish(){ global $rootDir,$wbAdminUser,$wbTablePrefix,$packageVersion; $data = array(); $data['adminUser'] = $wbAdminUser; $data['tablePrefix'] = $wbTablePrefix; $data['version'] = $packageVersion; $data['tables'] =& $this->database; $data['counts'] =& $this->backupCounts; $contents = "\n\n".serialize($data); if( $this->compress ){ gzwrite($this->handle,$contents); }else{ fwrite($this->handle,$contents); } } function backupStart(){ if( empty($_POST['tables']) ){ message('Please select tables to backup.'); return false; } //whichTables foreach($_POST['tables'] as $i => $table){ if( !isset($this->database[$table]) ){ continue; } $this->tables[] = $table; } //useTable if( isset($_POST['useTable']) && isset($this->database[$_POST['useTable']]) ){ $this->useTable = $_POST['useTable']; }else{ reset($this->tables); $this->useTable = current($this->tables); } //query values if( !isset($_POST['offset']) ){ $_POST['offset'] = 0; } $_POST['offset'] = (int)$_POST['offset']; if( !isset($_POST['num']) ){ $_POST['num'] = $this->startNum; } $_POST['num'] = (int)$_POST['num']; if( isset($_POST['count']) ){ $this->backupCounts = unserialize($_POST['counts']); } header('wb-ping: Pong'); return true; } //files created by mysql are always world writable function backup(){ global $page,$rootDir,$wbUniq,$wbTables,$lang,$wbTablePrefix,$wbCompress,$pageOwner; if( !$this->backupStart() ){ return; } // // file preparation // if( !$this->handle ){ if( $this->compress ){ $this->handle = gzopen($this->backupFile,'ab'); }else{ $this->handle = fopen($this->backupFile,'ab'); } } $startTime = microtime(); if( $this->safeMode ){ do{ $this->backupTable(); $duration = microtime_diff($startTime, microtime()); }while( $this->useTable && ($duration < $this->maxTime) ); }else{ do{ $this->backupTable(); set_time_limit(15); }while( $this->useTable ); } if( !$this->useTable ){ $this->backupFinish(); }else{ $this->backupForm(); } } function downloadNow(){ clearstatcache(); if( !file_exists($this->backupFile) || (filesize($this->backupFile) < 10) ){ message('It appears your backup file has already been erased, run the backup script again to download your backup file.'); return; } if($this->handle ){ if( $this->compress ){ gzclose($this->handle); }else{ fclose($this->handle); } } if( $this->compress ){ //sending the compressed data only seems to work if we end all the buffers while( ob_get_level() > 0){ ob_end_clean(); } header('Content-type: application/x-gzip'); header('Content-Disposition: attachment; filename=backup.txt.gz'); }else{ //and sending the uncompressed data only seems to work if we keep the buffers header('Content-type: text/plain; charset=UTF-8'); header('Content-Disposition: attachment; filename=backup.txt'); } // $size = filesize($this->backupFile); // header('Content-Length: '.$size); readfile($this->backupFile); //rewind and passthru weren't working properly $this->deleteBackup(); die(); } function deleteBackup(){ $fp = fopen($this->backupFile,'wb'); fclose($fp); unlink($this->backupFile); } function backupTable(){ global $wbTablePrefix; // // database info // $data = $this->database[$this->useTable]; $query = 'SHOW INDEX FROM `'.$wbTablePrefix.$this->useTable.'`'; $result = wbDB::runQuery($query); $tempA = array(); while($row = mysql_fetch_assoc($result)){ $tempA[$row['Key_name']]['Non_unique'] = $row['Non_unique']; $tempA[$row['Key_name']]['cols'][(int)$row['Seq_in_index']] = '`'.$row['Column_name'].'`'; if( !empty($row['Sub_part']) ){ $tempA[$row['Key_name']]['cols'][(int)$row['Seq_in_index']] .= '('.$row['Sub_part'].')'; } $tempA[$row['Key_name']]['Index_type'] = $row['Index_type']; } if( isset($tempA['PRIMARY']) ){ $orderBy = ' ORDER BY '.implode(', ',$tempA['PRIMARY']['cols']); }else{ trigger_error('The primary key was not specified for '.$this->useTable); return; } $query = 'SELECT COUNT(*) as `count` '; $query .= ' FROM `'.$wbTablePrefix.$this->useTable.'` '; $result = wbDB::runQuery($query); $row = mysql_fetch_assoc($result); $totalRows = $row['count']; // // Build query // $query = 'SELECT CONCAT("INSERT INTO `'.$wbTablePrefix.$this->useTable.'` SET" '; $comma = ''; foreach($data['columns'] as $column => $sql){ $query .= $comma; $query .= ', " `'.$column.'` = " '; $query .= ', REPLACE(REPLACE(QUOTE(`'.$column.'` ),"\n","\\\n"),"\r","\\\r") '; $comma = ', "," '; } $query .= ',";") as `sql` '; $query .= ' FROM `'.$wbTablePrefix.$this->useTable.'` '; $query .= $orderBy; $query .= ' LIMIT '.$_POST['offset'].','.$_POST['num']; // // run query and save results // $startTime = microtime(); $result = wbDB::runQuery($query); $numRows = mysql_num_rows($result); while( $row = mysql_fetch_assoc($result) ){ if( $this->compress ){ gzwrite($this->handle,$row['sql']."\n"); }else{ fwrite($this->handle,$row['sql']."\n"); } } if( $totalRows == ($_POST['offset']+$numRows)){ $this->backupCounts[$this->useTable] = $totalRows; $_POST['offset'] = 0; $_POST['num'] = $this->startNum; $this->nextTable(); }else{ $_POST['offset'] = ($_POST['offset']+$numRows); $duration = microtime_diff($startTime, microtime()); $timePerRow = $duration/$numRows; $_POST['num'] = floor($this->maxTime/$timePerRow); } } function nextTable(){ $next = false; foreach($this->tables as $table){ if( $next ){ $this->useTable = $table; return; } if( $table == $this->useTable){ $next = true; } } $this->useTable = false; } }new dbBackup();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -