Ticket #219: wp-db-backup.diff
| File wp-db-backup.diff, 11.9 KB (added by ringmaster, 7 years ago) |
|---|
-
C:/Documents
42 42 if ('smtp' == $_POST['deliver']) { 43 43 wp_deliver_backup ($backup_file, $_POST['deliver'], $_POST['backup_recipient']); 44 44 } elseif ('http' == $_POST['deliver']) { 45 header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page=wp-db-backup.php&backup=$backup_file"); 45 $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); 46 header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup=$backup_file"); 46 47 } 47 48 // we do this to say we're done. 48 49 $_POST['do_backup'] = 'DONE'; … … 97 98 } // function backquote($a_name, $do_it = TRUE) 98 99 99 100 ///////////////////////////// 100 function backup_table($ table) {101 function backup_table($fp, $table) { 101 102 global $wp_backup_error, $wpdb; 102 103 103 104 /* … … 109 110 to use the WordPress $wpdb object 110 111 */ 111 112 112 $sql_statements = '';113 114 113 // 115 114 // Add SQL statement to drop existing table 116 $sql_statements .= "\n";117 $sql_statements .= "\n";118 $sql_statements .= "#\n";119 $sql_statements .= "# Delete any existing table " . backquote($table) . "\n";120 $sql_statements .= "#\n";121 $sql_statements .= "\n";122 $sql_statements .= "DROP TABLE IF EXISTS " . backquote($table) . ";\n";115 fwrite($fp, "\n"); 116 fwrite($fp, "\n"); 117 fwrite($fp, "#\n"); 118 fwrite($fp, "# Delete any existing table " . backquote($table) . "\n"); 119 fwrite($fp, "#\n"); 120 fwrite($fp, "\n"); 121 fwrite($fp, "DROP TABLE IF EXISTS " . backquote($table) . ";\n"); 123 122 124 123 // 125 124 //Table structure 126 125 // Comment in SQL-file 127 $sql_statements .= "\n";128 $sql_statements .= "\n";129 $sql_statements .= "#\n";130 $sql_statements .= "# Table structure of table " . backquote($table) . "\n";131 $sql_statements .= "#\n";132 $sql_statements .= "\n";126 fwrite($fp, "\n"); 127 fwrite($fp, "\n"); 128 fwrite($fp, "#\n"); 129 fwrite($fp, "# Table structure of table " . backquote($table) . "\n"); 130 fwrite($fp, "#\n"); 131 fwrite($fp, "\n"); 133 132 134 133 $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N); 135 134 if (FALSE === $create_table) { 136 135 $wp_backup_error .= "Error with SHOW CREATE TABLE for $table.\r\n"; 137 return "#\n# Error with SHOW CREATE TABLE for $table!\n#\n";136 fwrite($fp, "#\n# Error with SHOW CREATE TABLE for $table!\n#\n"); 138 137 } 139 $sql_statements .= $create_table[0][1] . ' ;';138 fwrite($fp, $create_table[0][1] . ' ;'); 140 139 141 140 $table_structure = $wpdb->get_results("DESCRIBE $table"); 142 141 if (FALSE === $table_structure) { 143 142 $wp_backup_error .= "Error getting table structure of $table\r\n"; 144 return "#\n# Error getting table structure of $table!\n#\n";143 fwrite($fp, "#\n# Error getting table structure of $table!\n#\n"); 145 144 } 146 145 147 $table_data = $wpdb->get_results("SELECT * FROM $table", ARRAY_A);148 if (FALSE === $table_data) {149 $wp_backup_error .= "Error getting table contents from $table\r\n";150 return "#\n# Error getting table contents fom $table!\n#\n";151 }152 153 146 // 154 147 // Comment in SQL-file 155 $sql_statements .= "\n";156 $sql_statements .= "\n";157 $sql_statements .= "#\n";158 $sql_statements .= '# Data contents of table ' . backquote($table) . "\n";159 $sql_statements .= "#\n";148 fwrite($fp, "\n"); 149 fwrite($fp, "\n"); 150 fwrite($fp, "#\n"); 151 fwrite($fp, '# Data contents of table ' . backquote($table) . "\n"); 152 fwrite($fp, "#\n"); 160 153 161 154 $ints = array(); 162 155 foreach ($table_structure as $struct) { … … 170 163 } 171 164 } 172 165 173 $entries = 'INSERT INTO ' . backquote($table) . ' VALUES ('; 174 // \x08\\x09, not required 175 $search = array("\x00", "\x0a", "\x0d", "\x1a"); 176 $replace = array('\0', '\n', '\r', '\Z'); 177 foreach ($table_data as $row) { 178 $values = array(); 179 foreach ($row as $key => $value) { 180 if ($ints[strtolower($key)]) { 181 $values[] = $value; 182 } else { 183 $values[] = "'" . str_replace($search, $replace, sql_addslashes($value)) . "'"; 166 167 // Batch by $row_inc 168 169 $row_start = 0; 170 $row_inc = 10; 171 172 do { 173 $table_data = $wpdb->get_results("SELECT * FROM $table LIMIT {$row_start}, {$row_inc}", ARRAY_A); 174 175 /* 176 if (FALSE === $table_data) { 177 $wp_backup_error .= "Error getting table contents from $table\r\n"; 178 fwrite($fp, "#\n# Error getting table contents fom $table!\n#\n"); 179 } 180 */ 181 182 $entries = 'INSERT INTO ' . backquote($table) . ' VALUES ('; 183 // \x08\\x09, not required 184 $search = array("\x00", "\x0a", "\x0d", "\x1a"); 185 $replace = array('\0', '\n', '\r', '\Z'); 186 if($table_data) { 187 foreach ($table_data as $row) { 188 $values = array(); 189 foreach ($row as $key => $value) { 190 if ($ints[strtolower($key)]) { 191 $values[] = $value; 192 } else { 193 $values[] = "'" . str_replace($search, $replace, sql_addslashes($value)) . "'"; 194 } 195 } 196 fwrite($fp, " \n" . $entries . implode(', ', $values) . ') ;'); 184 197 } 198 $row_start += $row_inc; 185 199 } 186 $sql_statements .= " \n" . $entries . implode(', ', $values) . ') ;'; 187 } 200 } while(count($table_data) > 0); 188 201 // Create footer/closing comment in SQL-file 189 $sql_statements .= "\n";190 $sql_statements .= "#\n";191 $sql_statements .= "# End of data contents of table " . backquote($table) . "\n";192 $sql_statements .= "# --------------------------------------------------------\n";193 $sql_statements .= "\n";202 fwrite($fp, "\n"); 203 fwrite($fp, "#\n"); 204 fwrite($fp, "# End of data contents of table " . backquote($table) . "\n"); 205 fwrite($fp, "# --------------------------------------------------------\n"); 206 fwrite($fp, "\n"); 194 207 195 return $sql_statements;196 197 208 } // end backup_table() 198 209 199 210 //////////////////////////// … … 204 215 $done = array(); 205 216 206 217 $datum = date("Ymd_B"); 218 $wp_backup_temp = DB_NAME . "_$table_prefix$datum.tmp"; 207 219 $wp_backup_filename = DB_NAME . "_$table_prefix$datum.sql"; 208 220 if ($gzip) { 209 $wp_backup_filename .= '. gz';221 $wp_backup_filename .= '.zip'; 210 222 } 211 223 224 if (is_writable(ABSPATH . $wp_backup_dir)) { 225 if ($gzip) { 226 $fp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_temp, 'w'); 227 } else { 228 $fp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_filename, 'w'); 229 } 230 } else { 231 return false; 232 } 233 212 234 //Begin new backup of MySql 213 $sql = "# WordPress MySQL database backup\n";214 $sql .= "#\n";215 $sql .= "# Generated: " . date("l j. F Y H:i T") . "\n";216 $sql .= "# Hostname: " . DB_HOST . "\n";217 $sql .= "# Database: " . backquote(DB_NAME) . "\n";218 $sql .= "# --------------------------------------------------------\n";235 fwrite($fp, "# WordPress MySQL database backup\n"); 236 fwrite($fp, "#\n"); 237 fwrite($fp, "# Generated: " . date("l j. F Y H:i T") . "\n"); 238 fwrite($fp, "# Hostname: " . DB_HOST . "\n"); 239 fwrite($fp, "# Database: " . backquote(DB_NAME) . "\n"); 240 fwrite($fp, "# --------------------------------------------------------\n"); 219 241 220 242 foreach ($core_tables as $table) { 221 243 if (in_array($table, $done)) { continue; } … … 223 245 if ( !ini_get('safe_mode')) @set_time_limit(15*60); 224 246 //ini_set('memory_limit', '16M'); 225 247 // Create the SQL statements 226 $tbl = "# --------------------------------------------------------\n"; 227 $tbl .= "# Table: " . backquote($table) . "\n"; 228 $tbl .= "# --------------------------------------------------------\n"; 229 $tbl .= backup_table($table); 230 $sql .= $tbl; 248 fwrite($fp, "# --------------------------------------------------------\n"); 249 fwrite($fp, "# Table: " . backquote($table) . "\n"); 250 fwrite($fp, "# --------------------------------------------------------\n"); 251 backup_table($fp, $table); 231 252 $done[] = $table; 232 253 } 233 254 234 255 if (count($other_tables) > 0) { 235 256 foreach ($other_tables as $other_table) { 257 echo "$table<br/>"; 236 258 if (in_array($other_table, $done)) { continue; } 237 259 // Increase script execution time-limit to 15 min for every table. 238 260 if ( !ini_get('safe_mode')) @set_time_limit(15*60); 239 261 //ini_set('memory_limit', '16M'); 240 262 // Create the SQL statements 241 $tbl = "# --------------------------------------------------------\n"; 242 $tbl .= "# Table: " . backquote($other_table) . "\n"; 243 $tbl .= "# --------------------------------------------------------\n"; 244 $tbl .= backup_table($other_table); 245 $sql .= $tbl; 263 fwrite($fp, "# --------------------------------------------------------\n"); 264 fwrite($fp, "# Table: " . backquote($other_table) . "\n"); 265 fwrite($fp, "# --------------------------------------------------------\n"); 266 backup_table($other_table, $fp); 246 267 $done[] = $other_table; 247 268 } // foreach 248 269 } // if other_tables... 249 270 250 if (is_writable(ABSPATH . $wp_backup_dir)) { 251 if ($gzip) { 252 $sql = gzencode($sql); 271 fclose($fp); 272 273 if ($gzip) { 274 $mem_limit = ini_get('memory_limit'); 275 function return_bytes($val) { 276 $val = trim($val); 277 $last = strtolower($val{strlen($val)-1}); 278 switch($last) { 279 // The 'G' modifier is available since PHP 5.1.0 280 case 'g': 281 $val *= 1024; 282 case 'm': 283 $val *= 1024; 284 case 'k': 285 $val *= 1024; 286 } 287 288 return $val; 253 289 } 254 $cachefp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_filename, "w"); 255 fwrite($cachefp, $sql); 256 fclose($cachefp); 290 $mem_limit = return_bytes($mem_limit) - 2000000; // Approx. 2M for running script? 291 if(filesize(ABSPATH . $wp_backup_dir . $wp_backup_temp) > $mem_limit) { 292 // Uh, oh. 293 $wp_backup_error = __('The script to backup your database is too large for PHP to load entirely into memory, and so cannot be compressed within PHP. <strong>Please turn off the gzip compression option.</strong>'); 294 } else { 295 $data = implode("", file(ABSPATH . $wp_backup_dir . $wp_backup_temp)); 296 $gzdata = gzencode($data, 9); 297 $fp = fopen(ABSPATH . $wp_backup_dir . $wp_backup_filename, "w"); 298 fwrite($fp, $gzdata); 299 fclose($fp); 300 } 301 unlink (ABSPATH . $wp_backup_dir . $wp_backup_temp); 257 302 } 258 303 259 304 if ('' == $wp_backup_error) { … … 403 448 } 404 449 echo '</tr></table></fieldset>'; 405 450 echo '<fieldset class="options"><legend>' . __('Backup Options', 'wp_backup') . '</legend><table width="100%" align="center" cellpadding="5" cellspacing="5">'; 406 echo '<tr><td align=" center">';451 echo '<tr><td align="left">'; 407 452 echo __('Deliver backup file by', 'wp_backup') . ":<br />"; 408 echo '< input type="radio" name="deliver" value="none" /> ' . __('None', 'wp_backup') . '   <br />';409 echo '< input type="radio" name="deliver" value="smtp" /> ' . __('Email', 'wp_backup') . ' <br />';410 echo '< input type="radio" name="deliver" value="http" /> ' . __('Download', 'wp_backup');453 echo '<label style="display:block;"><input type="radio" name="deliver" value="none" /> ' . __('None', 'wp_backup') . '</label>'; 454 echo '<label style="display:block;"><input type="radio" name="deliver" value="smtp" /> ' . __('Email', 'wp_backup') . '</label>'; 455 echo '<label style="display:block;"><input type="radio" name="deliver" value="http" /> ' . __('Download', 'wp_backup') . '</label>'; 411 456 echo '</td><td align="left">' . __('Email backup to', 'wp_backup') . ':<br /> <input type="text" name="backup_recipient" size="20" value="' . get_settings('admin_email') . '" /></td></tr>'; 412 echo '<tr class="alternate"><td colspan="2" align="center"> ';457 echo '<tr class="alternate"><td colspan="2" align="center"><label style="display:block;">'; 413 458 if (! $WHOOPS) { 414 echo __('Use gzip compression', 'wp_backup') . '? <input type="checkbox" checked="checked" name="gzip" value="gzip" />< br />';459 echo __('Use gzip compression', 'wp_backup') . '? <input type="checkbox" checked="checked" name="gzip" value="gzip" /></label>'; 415 460 echo '<input type="hidden" name="do_backup" value="backup" />'; 416 461 echo '<input type="submit" name="submit" value="' . __('Backup', 'wp_backup') . '!" / >'; 417 462 } else {
