📄 wp-db-backup.php
字号:
function perform_backup() { // are we backing up any other tables? $also_backup = array(); if (isset($_POST['other_tables'])) { $also_backup = $_POST['other_tables']; } $core_tables = $_POST['core_tables']; $this->backup_file = $this->db_backup($core_tables, $also_backup); if (FALSE !== $this->backup_file) { if ('smtp' == $_POST['deliver']) { $this->deliver_backup ($this->backup_file, $_POST['deliver'], $_POST['backup_recipient']); } elseif ('http' == $_POST['deliver']) { $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup={$this->backup_file}"); } // we do this to say we're done. $this->backup_complete = true; } } /////////////////////////////// function admin_menu() { add_management_page(__('Backup'), __('Backup'), 'import', basename(__FILE__), array(&$this, 'backup_menu')); } function fragment_menu() { add_management_page(__('Backup'), __('Backup'), 'import', basename(__FILE__), array(&$this, 'build_backup_script')); } ///////////////////////////////////////////////////////// function sql_addslashes($a_string = '', $is_like = FALSE) { /* Better addslashes for SQL queries. Taken from phpMyAdmin. */ if ($is_like) { $a_string = str_replace('\\', '\\\\\\\\', $a_string); } else { $a_string = str_replace('\\', '\\\\', $a_string); } $a_string = str_replace('\'', '\\\'', $a_string); return $a_string; } // function sql_addslashes($a_string = '', $is_like = FALSE) /////////////////////////////////////////////////////////// function backquote($a_name) { /* Add backqouotes to tables and db-names in SQL queries. Taken from phpMyAdmin. */ if (!empty($a_name) && $a_name != '*') { if (is_array($a_name)) { $result = array(); reset($a_name); while(list($key, $val) = each($a_name)) { $result[$key] = '`' . $val . '`'; } return $result; } else { return '`' . $a_name . '`'; } } else { return $a_name; } } // function backquote($a_name, $do_it = TRUE) ///////////// function open($filename = '', $mode = 'w') { if ('' == $filename) return false; if ($this->gzip()) { $fp = @gzopen($filename, $mode); } else { $fp = @fopen($filename, $mode); } return $fp; } ////////////// function close($fp) { if ($this->gzip()) { gzclose($fp); } else { fclose($fp); } } ////////////// function stow($query_line) { if ($this->gzip()) { if(@gzwrite($this->fp, $query_line) === FALSE) { backup_error(__('There was an error writing a line to the backup script:')); backup_error(' ' . $query_line); } } else { if(@fwrite($this->fp, $query_line) === FALSE) { backup_error(__('There was an error writing a line to the backup script:')); backup_error(' ' . $query_line); } } } function backup_error($err) { if(count($this->backup_errors) < 20) { $this->backup_errors[] = $err; } elseif(count($this->backup_errors) == 20) { $this->backup_errors[] = __('Subsequent errors have been omitted from this log.'); } } ///////////////////////////// function backup_table($table, $segment = 'none') { global $wpdb; /* Taken partially from phpMyAdmin and partially from Alain Wolf, Zurich - Switzerland Website: http://restkultur.ch/personal/wolf/scripts/db_backup/ Modified by Scott Merril (http://www.skippy.net/) to use the WordPress $wpdb object */ $table_structure = $wpdb->get_results("DESCRIBE $table"); if (! $table_structure) { backup_errors(__('Error getting table details') . ": $table"); return FALSE; } if(($segment == 'none') || ($segment == 0)) { // // Add SQL statement to drop existing table $this->stow("\n\n"); $this->stow("#\n"); $this->stow("# Delete any existing table " . $this->backquote($table) . "\n"); $this->stow("#\n"); $this->stow("\n"); $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n"); // //Table structure // Comment in SQL-file $this->stow("\n\n"); $this->stow("#\n"); $this->stow("# Table structure of table " . $this->backquote($table) . "\n"); $this->stow("#\n"); $this->stow("\n"); $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N); if (FALSE === $create_table) { $this->backup_error(sprintf(__("Error with SHOW CREATE TABLE for %s."), $table)); $this->stow("#\n# Error with SHOW CREATE TABLE for $table!\n#\n"); } $this->stow($create_table[0][1] . ' ;'); if (FALSE === $table_structure) { $this->backup_error(sprintf(__("Error getting table structure of %s"), $table)); $this->stow("#\n# Error getting table structure of $table!\n#\n"); } // // Comment in SQL-file $this->stow("\n\n"); $this->stow("#\n"); $this->stow('# Data contents of table ' . $this->backquote($table) . "\n"); $this->stow("#\n"); } if(($segment == 'none') || ($segment >= 0)) { $ints = array(); foreach ($table_structure as $struct) { if ( (0 === strpos($struct->Type, 'tinyint')) || (0 === strpos(strtolower($struct->Type), 'smallint')) || (0 === strpos(strtolower($struct->Type), 'mediumint')) || (0 === strpos(strtolower($struct->Type), 'int')) || (0 === strpos(strtolower($struct->Type), 'bigint')) || (0 === strpos(strtolower($struct->Type), 'timestamp')) ) { $ints[strtolower($struct->Field)] = "1"; } } // Batch by $row_inc if($segment == 'none') { $row_start = 0; $row_inc = ROWS_PER_SEGMENT; } else { $row_start = $segment * ROWS_PER_SEGMENT; $row_inc = ROWS_PER_SEGMENT; } do { if ( !ini_get('safe_mode')) @set_time_limit(15*60); $table_data = $wpdb->get_results("SELECT * FROM $table LIMIT {$row_start}, {$row_inc}", ARRAY_A); /* if (FALSE === $table_data) { $wp_backup_error .= "Error getting table contents from $table\r\n"; fwrite($fp, "#\n# Error getting table contents fom $table!\n#\n"); } */ $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES ('; // \x08\\x09, not required $search = array("\x00", "\x0a", "\x0d", "\x1a"); $replace = array('\0', '\n', '\r', '\Z'); if($table_data) { foreach ($table_data as $row) { $values = array(); foreach ($row as $key => $value) { if ($ints[strtolower($key)]) { $values[] = $value; } else { $values[] = "'" . str_replace($search, $replace, $this->sql_addslashes($value)) . "'"; } } $this->stow(" \n" . $entries . implode(', ', $values) . ') ;'); } $row_start += $row_inc; } } while((count($table_data) > 0) and ($segment=='none')); } if(($segment == 'none') || ($segment < 0)) { // Create footer/closing comment in SQL-file $this->stow("\n"); $this->stow("#\n"); $this->stow("# End of data contents of table " . $this->backquote($table) . "\n"); $this->stow("# --------------------------------------------------------\n"); $this->stow("\n"); } } // end backup_table() function return_bytes($val) { $val = trim($val); $last = strtolower($val{strlen($val)-1}); switch($last) { // The 'G' modifier is available since PHP 5.1.0 case 'g': $val *= 1024; case 'm': $val *= 1024; case 'k': $val *= 1024; } return $val; } //////////////////////////// function db_backup($core_tables, $other_tables) { global $table_prefix, $wpdb; $datum = date("Ymd_B"); $wp_backup_filename = DB_NAME . "_$table_prefix$datum.sql"; if ($this->gzip()) { $wp_backup_filename .= '.gz'; } if (is_writable(ABSPATH . $this->backup_dir)) { $this->fp = $this->open(ABSPATH . $this->backup_dir . $wp_backup_filename); if(!$this->fp) { $this->backup_error(__('Could not open the backup file for writing!')); return false; } } else { $this->backup_error(__('The backup directory is not writeable!')); return false; } //Begin new backup of MySql $this->stow("# WordPress MySQL database backup\n"); $this->stow("#\n"); $this->stow("# Generated: " . date("l j. F Y H:i T") . "\n"); $this->stow("# Hostname: " . DB_HOST . "\n"); $this->stow("# Database: " . $this->backquote(DB_NAME) . "\n"); $this->stow("# --------------------------------------------------------\n"); if ( (is_array($other_tables)) && (count($other_tables) > 0) ) $tables = array_merge($core_tables, $other_tables); else $tables = $core_tables; foreach ($tables as $table) { // Increase script execution time-limit to 15 min for every table. if ( !ini_get('safe_mode')) @set_time_limit(15*60); // Create the SQL statements $this->stow("# --------------------------------------------------------\n"); $this->stow("# Table: " . $this->backquote($table) . "\n"); $this->stow("# --------------------------------------------------------\n"); $this->backup_table($table); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -