📄 wp-db-backup.php
字号:
<?php/*Plugin Name: WordPress Database BackupPlugin URI: http://www.skippy.net/blog/plugins/Description: On-demand backup of your WordPress database.Author: Scott MerrillVersion: 1.7Author URI: http://www.skippy.net/Much of this was modified from Mark Ghosh's One Click Backup, whichin turn was derived from phpMyAdmin.Many thanks to Owen (http://asymptomatic.net/wp/) for his patch http://dev.wp-plugins.org/ticket/219*/// CHANGE THIS IF YOU WANT TO USE A // DIFFERENT BACKUP LOCATION$rand = substr( md5( md5( DB_PASSWORD ) ), -5 );define('WP_BACKUP_DIR', 'wp-content/backup-' . $rand);define('ROWS_PER_SEGMENT', 100);class wpdbBackup { var $backup_complete = false; var $backup_file = ''; var $backup_dir = WP_BACKUP_DIR; var $backup_errors = array(); var $basename; function gzip() { return function_exists('gzopen'); } function wpdbBackup() { add_action('wp_cron_daily', array(&$this, 'wp_cron_daily')); $this->backup_dir = trailingslashit($this->backup_dir); $this->basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); if (isset($_POST['do_backup'])) { if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.')); switch($_POST['do_backup']) { case 'backup': $this->perform_backup(); break; case 'fragments': add_action('admin_menu', array(&$this, 'fragment_menu')); break; } } elseif (isset($_GET['fragment'] )) { if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.')); add_action('init', array(&$this, 'init')); } elseif (isset($_GET['backup'] )) { if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.')); add_action('init', array(&$this, 'init')); } else { add_action('admin_menu', array(&$this, 'admin_menu')); } } function init() { if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.')); if (isset($_GET['backup'])) { $via = isset($_GET['via']) ? $_GET['via'] : 'http'; $this->backup_file = $_GET['backup']; switch($via) { case 'smtp': case 'email': $this->deliver_backup ($this->backup_file, 'smtp', $_GET['recipient']); echo ' <!-- ' . $via . ' --> <script type="text/javascript"><!--\\ '; if($this->backup_errors) { foreach($this->backup_errors as $error) { echo "window.parent.addError('$error');\n"; } } echo ' alert("' . __('Backup Complete!') . '"); </script> '; break; default: $this->deliver_backup ($this->backup_file, $via); } die(); } if (isset($_GET['fragment'] )) { list($table, $segment, $filename) = explode(':', $_GET['fragment']); $this->backup_fragment($table, $segment, $filename); } die(); } function build_backup_script() { global $table_prefix, $wpdb; $datum = date("Ymd_B"); $backup_filename = DB_NAME . "_$table_prefix$datum.sql"; if ($this->gzip()) $backup_filename .= '.gz'; echo "<div class='wrap'>"; //echo "<pre>" . print_r($_POST, 1) . "</pre>"; echo '<h2>' . __('Backup') . '</h2> <fieldset class="options"><legend>' . __('Progress') . '</legend> <p><strong>' . __('DO NOT DO THE FOLLOWING AS IT WILL CAUSE YOUR BACKUP TO FAIL:'). '</strong></p> <ol> <li>'.__('Close this browser').'</li> <li>'.__('Reload this page').'</li> <li>'.__('Click the Stop or Back buttons in your browser').'</li> </ol> <p><strong>' . __('Progress:') . '</strong></p> <div id="meterbox" style="height:11px;width:80%;padding:3px;border:1px solid #659fff;"><div id="meter" style="height:11px;background-color:#659fff;width:0%;text-align:center;font-size:6pt;"> </div></div> <div id="progress_message"></div> <div id="errors"></div> </fieldset> <iframe id="backuploader" src="about:blank" style="border:0px solid white;height:1em;width:1em;"></iframe> <script type="text/javascript"><!--// function setMeter(pct) { var meter = document.getElementById("meter"); meter.style.width = pct + "%"; meter.innerHTML = Math.floor(pct) + "%"; } function setProgress(str) { var progress = document.getElementById("progress_message"); progress.innerHTML = str; } function addError(str) { var errors = document.getElementById("errors"); errors.innerHTML = errors.innerHTML + str + "<br />"; } function backup(table, segment) { var fram = document.getElementById("backuploader"); fram.src = "' . $_SERVER['REQUEST_URI'] . '&fragment=" + table + ":" + segment + ":' . $backup_filename . '"; } var curStep = 0; function nextStep() { backupStep(curStep); curStep++; } function finishBackup() { var fram = document.getElementById("backuploader"); setMeter(100); '; $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); $download_uri = get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup={$backup_filename}"; switch($_POST['deliver']) { case 'http': echo ' setProgress("' . sprintf(__("Backup complete, preparing <a href=\\\"%s\\\">backup</a> for download..."), $download_uri) . '"); fram.src = "' . $download_uri . '"; '; break; case 'smtp': echo ' setProgress("' . sprintf(__("Backup complete, sending <a href=\\\"%s\\\">backup</a> via email..."), $download_uri) . '"); fram.src = "' . $download_uri . '&via=email&recipient=' . $_POST['backup_recipient'] . '"; '; break; default: echo ' setProgress("' . sprintf(__("Backup complete, download <a href=\\\"%s\\\">here</a>."), $download_uri) . '"); '; } echo ' } function backupStep(step) { switch(step) { case 0: backup("", 0); break; '; $also_backup = array(); if (isset($_POST['other_tables'])) { $also_backup = $_POST['other_tables']; } else { $also_backup = array(); } $core_tables = $_POST['core_tables']; $tables = array_merge($core_tables, $also_backup); $step_count = 1; foreach ($tables as $table) { $rec_count = $wpdb->get_var("SELECT count(*) FROM {$table}"); $rec_segments = ceil($rec_count / ROWS_PER_SEGMENT); $table_count = 0; do { echo "case {$step_count}: backup(\"{$table}\", {$table_count}); break;\n"; $step_count++; $table_count++; } while($table_count < $rec_segments); echo "case {$step_count}: backup(\"{$table}\", -1); break;\n"; $step_count++; } echo "case {$step_count}: finishBackup(); break;"; echo ' } if(step != 0) setMeter(100 * step / ' . $step_count . '); } nextStep(); //--></script> </div> '; } function backup_fragment($table, $segment, $filename) { global $table_prefix, $wpdb; echo "$table:$segment:$filename"; if($table == '') { $msg = __('Creating backup file...'); } else { if($segment == -1) { $msg = sprintf(__('Finished backing up table \\"%s\\".'), $table); } else { $msg = sprintf(__('Backing up table \\"%s\\"...'), $table); } } echo '<script type="text/javascript"><!--// var msg = "' . $msg . '"; window.parent.setProgress(msg); '; if (is_writable(ABSPATH . $this->backup_dir)) { $this->fp = $this->open(ABSPATH . $this->backup_dir . $filename, 'a'); if(!$this->fp) { $this->backup_error(__('Could not open the backup file for writing!')); $this->fatal_error = __('The backup file could not be saved. Please check the permissions for writing to your backup directory and try again.'); } else { if($table == '') { //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"); } else { if($segment == 0) { // Increase script execution time-limit to 15 min for every table. if ( !ini_get('safe_mode')) @set_time_limit(15*60); //ini_set('memory_limit', '16M'); // Create the SQL statements $this->stow("# --------------------------------------------------------\n"); $this->stow("# Table: " . $this->backquote($table) . "\n"); $this->stow("# --------------------------------------------------------\n"); } $this->backup_table($table, $segment); } } } else { $this->backup_error(__('The backup directory is not writeable!')); $this->fatal_error = __('The backup directory is not writeable! Please check the permissions for writing to your backup directory and try again.'); } if($this->fp) $this->close($this->fp); if($this->backup_errors) { foreach($this->backup_errors as $error) { echo "window.parent.addError('$error');\n"; } } if($this->fatal_error) { echo ' alert("' . addslashes($this->fatal_error) . '"); //--></script> '; } else { echo ' window.parent.nextStep(); //--></script> '; } die(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -