📄 wp-db-backup.php
字号:
$this->close($this->fp); if (count($this->backup_errors)) { return false; } else { return $wp_backup_filename; } } //wp_db_backup /////////////////////////// function deliver_backup ($filename = '', $delivery = 'http', $recipient = '') { if ('' == $filename) { return FALSE; } $diskfile = ABSPATH . $this->backup_dir . $filename; if ('http' == $delivery) { if (! file_exists($diskfile)) { $msg = sprintf(__('File not found:%s'), "<br /><strong>$filename</strong><br />"); $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); $msg .= '<br /><a href="' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}" . '">' . __('Return to Backup'); die($msg); } header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Length: ' . filesize($diskfile)); header("Content-Disposition: attachment; filename=$filename"); readfile($diskfile); unlink($diskfile); } elseif ('smtp' == $delivery) { if (! file_exists($diskfile)) return false; if (! is_email ($recipient)) { $recipient = get_settings('admin_email'); } $randomish = md5(time()); $boundary = "==WPBACKUP-BY-SKIPPY-$randomish"; $fp = fopen($diskfile,"rb"); $file = fread($fp,filesize($diskfile)); $this->close($fp); $data = chunk_split(base64_encode($file)); $headers = "MIME-Version: 1.0\n"; $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; $headers .= 'From: ' . get_settings('admin_email') . "\n"; $message = sprintf(__("Attached to this email is\n %1s\n Size:%2s kilobytes\n"), $filename, round(filesize($diskfile)/1024)); // Add a multipart boundary above the plain message $message = "This is a multi-part message in MIME format.\n\n" . "--{$boundary}\n" . "Content-Type: text/plain; charset=\"utf-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Add file attachment to the message $message .= "--{$boundary}\n" . "Content-Type: application/octet-stream;\n" . " name=\"{$filename}\"\n" . "Content-Disposition: attachment;\n" . " filename=\"{$filename}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$boundary}--\n"; if (function_exists('wp_mail')) { wp_mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers); } else { mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers); } unlink($diskfile); } return; } //////////////////////////// function backup_menu() { global $table_prefix, $wpdb; $feedback = ''; $WHOOPS = FALSE; // did we just do a backup? If so, let's report the status if ( $this->backup_complete ) { $feedback = '<div class="updated"><p>' . __('Backup Successful') . '!'; $file = $this->backup_file; switch($_POST['deliver']) { case 'http': $feedback .= '<br />' . sprintf(__('Your backup file: <a href="%1s">%2s</a> should begin downloading shortly.'), get_settings('siteurl') . "/{$this->backup_dir}{$this->backup_file}", $this->backup_file); break; case 'smtp': if (! is_email($_POST['backup_recipient'])) { $feedback .= get_settings('admin_email'); } else { $feedback .= $_POST['backup_recipient']; } $feedback = '<br />' . sprintf(__('Your backup has been emailed to %s'), $feedback); break; case 'none': $feedback .= '<br />' . __('Your backup file has been saved on the server. If you would like to download it now, right click and select "Save As"'); $feedback .= ':<br /> <a href="' . get_settings('siteurl') . "/{$this->backup_dir}$file\">$file</a> : " . sprintf(__('%s bytes'), filesize(ABSPATH . $this->backup_dir . $file)); } $feedback .= '</p></div>'; } if (count($this->backup_errors)) { $feedback .= '<div class="updated error">' . __('The following errors were reported:') . "<pre>"; foreach($this->backup_errors as $error) { $feedback .= "{$error}\n"; //Errors are already localized } $feedback .= "</pre></div>"; } // did we just save options for wp-cron? if ( (function_exists('wp_cron_init')) && isset($_POST['wp_cron_backup_options']) ) { update_option('wp_cron_backup_schedule', intval($_POST['cron_schedule']), FALSE); update_option('wp_cron_backup_tables', $_POST['wp_cron_backup_tables']); if (is_email($_POST['cron_backup_recipient'])) { update_option('wp_cron_backup_recipient', $_POST['cron_backup_recipient'], FALSE); } $feedback .= '<div class="updated"><p>' . __('Scheduled Backup Options Saved!') . '</p></div>'; } // Simple table name storage $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta'); // Apply WP DB prefix to table names $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names); $other_tables = array(); $also_backup = array(); // Get complete db table list $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N); $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables); // Get list of WP tables that actually exist in this DB (for 1.6 compat!) $wp_backup_default_tables = array_intersect($all_tables, $wp_table_names); // Get list of non-WP tables $other_tables = array_diff($all_tables, $wp_backup_default_tables); if ('' != $feedback) { echo $feedback; } // Give the new dirs the same perms as wp-content. $stat = stat( ABSPATH . 'wp-content' ); $dir_perms = $stat['mode'] & 0000777; // Get the permission bits. if ( !file_exists( ABSPATH . $this->backup_dir) ) { if ( @ mkdir( ABSPATH . $this->backup_dir) ) { @ chmod( ABSPATH . $this->backup_dir, $dir_perms); } else { echo '<div class="updated error"><p align="center">' . __('WARNING: Your wp-content directory is <strong>NOT</strong> writable! We can not create the backup directory.') . '<br />' . ABSPATH . $this->backup_dir . "</p></div>"; $WHOOPS = TRUE; } } if ( !is_writable( ABSPATH . $this->backup_dir) ) { echo '<div class="updated error"><p align="center">' . __('WARNING: Your backup directory is <strong>NOT</strong> writable! We can not create the backup directory.') . '<br />' . ABSPATH . "</p></div>"; } if ( !file_exists( ABSPATH . $this->backup_dir . 'index.php') ) { @ touch( ABSPATH . $this->backup_dir . "index.php"); } echo "<div class='wrap'>"; echo '<h2>' . __('Backup') . '</h2>'; echo '<fieldset class="options"><legend>' . __('Tables') . '</legend>'; echo '<form method="post">'; echo '<table align="center" cellspacing="5" cellpadding="5"><tr><td width="50%" align="left" class="alternate" valign="top">'; echo __('These core WordPress tables will always be backed up:') . '<br /><ul>'; foreach ($wp_backup_default_tables as $table) { echo "<li><input type='hidden' name='core_tables[]' value='$table' />$table</li>"; } echo '</ul></td><td width="50%" align="left" valign="top">'; if (count($other_tables) > 0) { echo __('You may choose to include any of the following tables:') . ' <br />'; foreach ($other_tables as $table) { echo "<label style=\"display:block;\"><input type='checkbox' name='other_tables[]' value='{$table}' /> {$table}</label>"; } } echo '</tr></table></fieldset>'; echo '<fieldset class="options"><legend>' . __('Backup Options') . '</legend>'; echo __('What to do with the backup file:') . "<br />"; echo '<label style="display:block;"><input type="radio" name="deliver" value="none" /> ' . __('Save to server') . " ({$this->backup_dir})</label>"; echo '<label style="display:block;"><input type="radio" checked="checked" name="deliver" value="http" /> ' . __('Download to your computer') . '</label>'; echo '<div><input type="radio" name="deliver" id="do_email" value="smtp" /> '; echo '<label for="do_email">'.__('Email backup to:').'</label><input type="text" name="backup_recipient" size="20" value="' . get_settings('admin_email') . '" />'; // Check DB dize. $table_status = $wpdb->get_results("SHOW TABLE STATUS FROM " . $this->backquote(DB_NAME)); $core_size = $db_size = 0; foreach($table_status as $table) { $table_size = $table->Data_length - $table->Data_free; if(in_array($table->Name, $wp_backup_default_tables)) { $core_size += $table_size; } $db_size += $table_size; } $mem_limit = ini_get('memory_limit'); $mem_limit = $this->return_bytes($mem_limit); $mem_limit = ($mem_limit == 0) ? 8*1024*1024 : $mem_limit - 2000000; if (! $WHOOPS) { echo '<input type="hidden" name="do_backup" id="do_backup" value="backup" /></div>'; echo '<p class="submit"><input type="submit" name="submit" onclick="document.getElementById(\'do_backup\').value=\'fragments\';" value="' . __('Backup') . '!" / ></p>'; } else { echo '<p class="alternate">' . __('WARNING: Your backup directory is <strong>NOT</strong> writable!') . '</p>'; } echo '</fieldset>'; echo '</form>'; // this stuff only displays if wp_cron is installed if (function_exists('wp_cron_init')) { echo '<fieldset class="options"><legend>' . __('Scheduled Backup') . '</legend>'; $datetime = get_settings('date_format') . ' @ ' . get_settings('time_format'); echo '<p>' . __('Last WP-Cron Daily Execution') . ': ' . date($datetime, get_option('wp_cron_daily_lastrun')) . '<br />'; echo __('Next WP-Cron Daily Execution') . ': ' . date($datetime, (get_option('wp_cron_daily_lastrun') + 86400)) . '</p>'; echo '<form method="post">'; echo '<table width="100%" callpadding="5" cellspacing="5">'; echo '<tr><td align="center">'; echo __('Schedule: '); $wp_cron_backup_schedule = get_option('wp_cron_backup_schedule'); $schedule = array(0 => __('None'), 1 => __('Daily')); foreach ($schedule as $value => $name) { echo ' <input type="radio" name="cron_schedule"'; if ($wp_cron_backup_schedule == $value) { echo ' checked="checked" '; } echo 'value="' . $value . '" /> ' . __($name); } echo '</td><td align="center">'; $cron_recipient = get_option('wp_cron_backup_recipient'); if (! is_email($cron_recipient)) { $cron_recipient = get_settings('admin_email'); } echo __('Email backup to:') . ' <input type="text" name="cron_backup_recipient" size="20" value="' . $cron_recipient . '" />'; echo '</td></tr>'; $cron_tables = get_option('wp_cron_backup_tables'); if (! is_array($cron_tables)) { $cron_tables = array(); } if (count($other_tables) > 0) { echo '<tr><td colspan="2" align="left">' . __('Tables to include:') . '<br />'; foreach ($other_tables as $table) { echo '<input type="checkbox" '; if (in_array($table, $cron_tables)) { echo 'checked=checked '; } echo "name='wp_cron_backup_tables[]' value='{$table}' /> {$table}<br />"; } echo '</td></tr>'; } echo '<tr><td colspan="2" align="center"><input type="hidden" name="wp_cron_backup_options" value="SET" /><input type="submit" name="submit" value="' . __('Submit') . '" /></td></tr></table></form>'; echo '</fieldset>'; } // end of wp_cron section echo '</div>'; }// end wp_backup_menu() ///////////////////////////// function wp_cron_daily() { $schedule = intval(get_option('wp_cron_backup_schedule')); if (0 == $schedule) { // Scheduled backup is disabled return; } global $table_prefix, $wpdb; $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta'); $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names); $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N); $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables); $core_tables = array_intersect($all_tables, $wp_table_names); $other_tables = get_option('wp_cron_backup_tables'); $recipient = get_option('wp_cron_backup_recipient'); $backup_file = $this->db_backup($core_tables, $other_tables); if (FALSE !== $backup_file) { $this->deliver_backup ($backup_file, 'smtp', $recipient); } return; } // wp_cron_db_backup}function wpdbBackup_init() { global $mywpdbbackup; if ( !current_user_can('import') ) return; $mywpdbbackup = new wpdbBackup(); }add_action('plugins_loaded', 'wpdbBackup_init');?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -