Manage → Backup to get started.
Author: Austin Matzko
Author URI: http://www.ilfilosofo.com/blog/
Version: 2.1.3
Development continued from that done by Skippy (http://www.skippy.net/)
Originally modified from Mark Ghosh's One Click Backup, which
in turn was derived from phpMyAdmin.
Many thanks to Owen (http://asymptomatic.net/wp/) for his patch
http://dev.wp-plugins.org/ticket/219
Copyright 2007 Austin Matzko (email : if.website at gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
*/
/**
* Change WP_BACKUP_DIR 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);
/**
* Set MOD_EVASIVE_OVERRIDE to true
* and increase MOD_EVASIVE_DELAY
* if the backup stops prematurely.
*/
// define('MOD_EVASIVE_OVERRIDE', false);
define('MOD_EVASIVE_DELAY', '500');
class wpdbBackup {
var $backup_complete = false;
var $backup_file = '';
var $backup_filename;
var $backup_dir = WP_BACKUP_DIR;
var $core_table_names;
var $errors = array();
var $basename;
var $page_url;
var $referer_check_key;
var $useMailer = false;
function gzip() {
return function_exists('gzopen');
}
function module_check() {
$mod_evasive = false;
if ( true === MOD_EVASIVE_OVERRIDE ) return true;
if ( false === MOD_EVASIVE_OVERRIDE ) return false;
if ( function_exists('apache_get_modules') )
foreach( (array) apache_get_modules() as $mod )
if ( false !== strpos($mod,'mod_evasive') || false !== strpos($mod,'mod_dosevasive') )
return true;
return false;
}
function wpdbBackup() {
global $table_prefix, $wpdb;
add_action('init', array(&$this, 'init_textdomain'));
add_action('phpmailer_init', array(&$this, 'setup_phpmailer'));
add_action('wp_db_backup_cron', array(&$this, 'cron_backup'));
add_action('wp_cron_daily', array(&$this, 'wp_cron_daily'));
add_filter('cron_schedules', array(&$this, 'add_sched_options'));
add_filter('wp_db_b_schedule_choices', array(&$this, 'schedule_choices'));
$table_prefix = ( isset( $table_prefix ) ) ? $table_prefix : $wpdb->prefix;
$datum = date("Ymd_B");
$this->backup_filename = DB_NAME . "_$table_prefix$datum.sql";
if ($this->gzip()) $this->backup_filename .= '.gz';
$this->core_table_names = array(
$wpdb->categories,
$wpdb->comments,
$wpdb->link2cat,
$wpdb->linkcategories,
$wpdb->links,
$wpdb->options,
$wpdb->post2cat,
$wpdb->postmeta,
$wpdb->posts,
$wpdb->terms,
$wpdb->term_taxonomy,
$wpdb->term_relationships,
$wpdb->users,
$wpdb->usermeta,
);
$this->backup_dir = trailingslashit(apply_filters('wp_db_b_backup_dir',$this->backup_dir));
$this->basename = 'wp-db-backup';
$this->referer_check_key = $this->basename . '-download_' . DB_NAME;
$query_args = array( 'page' => $this->basename );
if ( function_exists('wp_create_nonce') )
$query_args = array_merge( $query_args, array('_wpnonce' => wp_create_nonce($this->referer_check_key)) );
$this->page_url = add_query_arg( $query_args, get_option('siteurl') . '/wp-admin/edit.php');
if (isset($_POST['do_backup'])) {
$this->wp_secure('fatal');
check_admin_referer($this->referer_check_key);
$this->can_user_backup('main');
switch($_POST['do_backup']) {
case 'backup':
add_action('init', array(&$this, 'perform_backup'));
break;
case 'fragments':
add_action('admin_menu', array(&$this, 'fragment_menu'));
break;
}
} elseif (isset($_GET['fragment'] )) {
$this->can_user_backup('frame');
add_action('init', array(&$this, 'init'));
} elseif (isset($_GET['backup'] )) {
$this->can_user_backup();
add_action('init', array(&$this, 'init'));
} else {
add_action('admin_menu', array(&$this, 'admin_menu'));
}
}
function init() {
$this->can_user_backup();
if (isset($_GET['backup'])) {
$via = isset($_GET['via']) ? $_GET['via'] : 'http';
$this->backup_file = $_GET['backup'];
$this->validate_file($this->backup_file);
switch($via) {
case 'smtp':
case 'email':
$success = $this->deliver_backup($this->backup_file, 'smtp', $_GET['recipient']);
$this->error_display( 'frame' );
if ( $success ) {
echo '
';
}
break;
default:
$this->deliver_backup($this->backup_file, $via);
$this->error_display( 'frame' );
}
die();
}
if (isset($_GET['fragment'] )) {
list($table, $segment, $filename) = explode(':', $_GET['fragment']);
$this->validate_file($filename);
$this->backup_fragment($table, $segment, $filename);
}
die();
}
function init_textdomain() {
load_plugin_textdomain('wp-db-backup');
}
function build_backup_script() {
global $table_prefix, $wpdb;
echo "
";
//echo "
" . print_r($_POST, 1) . "
";
echo '
' . __('Backup','wp-db-backup') . '
';
}
function backup_fragment($table, $segment, $filename) {
global $table_prefix, $wpdb;
echo "$table:$segment:$filename";
if($table == '') {
$msg = __('Creating backup file...','wp-db-backup');
} else {
if($segment == -1) {
$msg = sprintf(__('Finished backing up table \\"%s\\".','wp-db-backup'), $table);
} else {
$msg = sprintf(__('Backing up table \\"%s\\"...','wp-db-backup'), $table);
}
}
if (is_writable(ABSPATH . $this->backup_dir)) {
$this->fp = $this->open(ABSPATH . $this->backup_dir . $filename, 'a');
if(!$this->fp) {
$this->error(__('Could not open the backup file for writing!','wp-db-backup'));
$this->error(array('loc' => 'frame', 'kind' => 'fatal', 'msg' => __('The backup file could not be saved. Please check the permissions for writing to your backup directory and try again.','wp-db-backup')));
}
else {
if($table == '') {
//Begin new backup of MySql
$this->stow("# " . __('WordPress MySQL database backup','wp-db-backup') . "\n");
$this->stow("#\n");
$this->stow("# " . sprintf(__('Generated: %s','wp-db-backup'),date("l j. F Y H:i T")) . "\n");
$this->stow("# " . sprintf(__('Hostname: %s','wp-db-backup'),DB_HOST) . "\n");
$this->stow("# " . sprintf(__('Database: %s','wp-db-backup'),$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);
// Create the SQL statements
$this->stow("# --------------------------------------------------------\n");
$this->stow("# " . sprintf(__('Table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
$this->stow("# --------------------------------------------------------\n");
}
$this->backup_table($table, $segment);
}
}
} else {
$this->error(array('kind' => 'fatal', 'loc' => 'frame', 'msg' => __('The backup directory is not writeable! Please check the permissions for writing to your backup directory and try again.','wp-db-backup')));
}
if($this->fp) $this->close($this->fp);
$this->error_display('frame');
echo '
';
die();
}
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']);
wp_redirect($this->page_url);
} elseif ('http' == $_POST['deliver']) {
$download_uri = add_query_arg('backup',$this->backup_file,$this->page_url);
wp_redirect($download_uri);
}
// we do this to say we're done.
$this->backup_complete = true;
}
}
function admin_menu() {
add_management_page(__('Backup','wp-db-backup'), __('Backup','wp-db-backup'), 'import', $this->basename, array(&$this, 'backup_menu'));
}
function fragment_menu() {
add_management_page(__('Backup','wp-db-backup'), __('Backup','wp-db-backup'), 'import', $this->basename, array(&$this, 'build_backup_script'));
}
/**
* Better addslashes for SQL queries.
* Taken from phpMyAdmin.
*/
function sql_addslashes($a_string = '', $is_like = FALSE) {
if ($is_like) $a_string = str_replace('\\', '\\\\\\\\', $a_string);
else $a_string = str_replace('\\', '\\\\', $a_string);
return str_replace('\'', '\\\'', $a_string);
}
/**
* Add backquotes to tables and db-names in
* SQL queries. Taken from phpMyAdmin.
*/
function backquote($a_name) {
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 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);
}
/**
* Write to the backup file
* @param string $query_line the line to write
* @return null
*/
function stow($query_line) {
if ($this->gzip()) {
if(FALSE === @gzwrite($this->fp, $query_line))
$this->error(__('There was an error writing a line to the backup script:','wp-db-backup') . ' ' . $query_line);
} else {
if(FALSE === @fwrite($this->fp, $query_line))
$this->error(__('There was an error writing a line to the backup script:','wp-db-backup') . ' ' . $query_line);
}
}
/**
* Logs any error messages
* @param array $args
* @return bool
*/
function error($args = array()) {
if ( is_string( $args ) )
$args = array('msg' => $args);
$args = array_merge( array('loc' => 'main', 'kind' => 'warn', 'msg' => ''), $args);
$this->errors[$args['kind']][] = $args['msg'];
if ( 'fatal' == $args['kind'] || 'frame' == $args['loc'])
$this->error_display($args['loc']);
return true;
}
/**
* Displays error messages
* @param array $errs
* @param string $loc
* @return string
*/
function error_display($loc = 'main', $echo = true) {
$errs = $this->errors;
unset( $this->errors );
if ( ! count($errs) ) return;
$msg = '';
$err_list = array_slice(array_merge( (array) $errs['fatal'], (array) $errs['warn']), 0, 10);
if ( 10 == count( $err_list ) )
$err_list[9] = __('Subsequent errors have been omitted from this log.','wp-db-backup');
$wrap = ( 'frame' == $loc ) ? "" : '%1$s';
$line = ( 'frame' == $loc ) ?
"try{ window.parent.addError('%1\$s'); msgList += ' %1\$s'; } catch(e) { msgList += ' %1\$s';}\n" :
"%1\$s \n";
foreach( (array) $err_list as $err )
$msg .= sprintf($line,$err);
$msg = sprintf($wrap,$msg);
if ( count($errs['fatal'] ) ) {
if ( function_exists('wp_die') && 'frame' != $loc ) wp_die($msg);
else die($msg);
}
else {
if ( $echo ) echo $msg;
else return $msg;
}
}
/**
* Taken partially from phpMyAdmin and partially from
* Alain Wolf, Zurich - Switzerland
* Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
* Modified by Scott Merrill (http://www.skippy.net/)
* to use the WordPress $wpdb object
* @param string $table
* @param string $segment
* @return void
*/
function backup_table($table, $segment = 'none') {
global $wpdb;
$table_structure = $wpdb->get_results("DESCRIBE $table");
if (! $table_structure) {
$this->error(__('Error getting table details','wp-db-backup') . ": $table");
return FALSE;
}
if(($segment == 'none') || ($segment == 0)) {
// Add SQL statement to drop existing table
$this->stow("\n\n");
$this->stow("#\n");
$this->stow("# " . sprintf(__('Delete any existing table %s','wp-db-backup'),$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("# " . sprintf(__('Table structure of table %s','wp-db-backup'),$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) {
$err_msg = sprintf(__('Error with SHOW CREATE TABLE for %s.','wp-db-backup'), $table);
$this->error($err_msg);
$this->stow("#\n# $err_msg\n#\n");
}
$this->stow($create_table[0][1] . ' ;');
if (FALSE === $table_structure) {
$err_msg = sprintf(__('Error getting table structure of %s','wp-db-backup'), $table);
$this->error($err_msg);
$this->stow("#\n# $err_msg\n#\n");
}
// Comment in SQL-file
$this->stow("\n\n");
$this->stow("#\n");
$this->stow('# ' . sprintf(__('Data contents of table %s','wp-db-backup'),$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) {
$err_msg = sprintf(__('Error getting table contents from %s','wp-db-backup'),$table);
$this->error($err_msg);
fwrite($fp, "#\n# $err_msg\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("# " . sprintf(__('End of data contents of table %s','wp-db-backup'),$this->backquote($table)) . "\n");
$this->stow("# --------------------------------------------------------\n");
$this->stow("\n");
}
} // end backup_table()
function db_backup($core_tables, $other_tables) {
global $table_prefix, $wpdb;
if (is_writable(ABSPATH . $this->backup_dir)) {
$this->fp = $this->open(ABSPATH . $this->backup_dir . $this->backup_filename);
if(!$this->fp) {
$this->error(__('Could not open the backup file for writing!','wp-db-backup'));
return false;
}
} else {
$this->error(__('The backup directory is not writeable!','wp-db-backup'));
return false;
}
//Begin new backup of MySql
$this->stow("# " . __('WordPress MySQL database backup','wp-db-backup') . "\n");
$this->stow("#\n");
$this->stow("# " . sprintf(__('Generated: %s','wp-db-backup'),date("l j. F Y H:i T")) . "\n");
$this->stow("# " . sprintf(__('Hostname: %s','wp-db-backup'),DB_HOST) . "\n");
$this->stow("# " . sprintf(__('Database: %s','wp-db-backup'),$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("# " . sprintf(__('Table: %s','wp-db-backup'),$this->backquote($table)) . "\n");
$this->stow("# --------------------------------------------------------\n");
$this->backup_table($table);
}
$this->close($this->fp);
if (count($this->errors)) {
return false;
} else {
return $this->backup_filename;
}
} //wp_db_backup
/**
* Sets up the attachment to work with phpmailer, which appears in WP 2.2+
* @return bool
*/
function setup_phpmailer(&$phpmailer) {
if ( $this->useMailer ) :
$phpmailer->AddAttachment($this->diskfile, $this->filename);
$phpmailer->Body = $this->message;
endif;
return true;
}
function deliver_backup($filename = '', $delivery = 'http', $recipient = '') {
if ('' == $filename) { return false; }
$this->diskfile = ABSPATH . $this->backup_dir . $filename;
$this->filename = $filename;
if ('http' == $delivery) {
if (! file_exists($this->diskfile))
$this->error(array('kind' => 'fatal', 'msg' => sprintf(__('File not found:%s','wp-db-backup'), " $filename ") . ' ' . __('Return to Backup','wp-db-backup') . ''));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($this->diskfile));
header("Content-Disposition: attachment; filename=$filename");
$success = readfile($this->diskfile);
unlink($this->diskfile);
} elseif ('smtp' == $delivery) {
if (! file_exists($this->diskfile)) return false;
if (! is_email ($recipient)) {
$recipient = get_option('admin_email');
}
$randomish = md5(time());
$boundary = "==WPBACKUP-BY-SKIPPY-$randomish";
$fp = fopen($this->diskfile,"rb");
$file = fread($fp,filesize($this->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_option('admin_email') . "\n";
$this->message = $message = sprintf(__("Attached to this email is\n %1s\n Size:%2s kilobytes\n",'wp-db-backup'), $filename, round(filesize($this->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')) {
$this->useMailer = true;
$success = @wp_mail($recipient, get_bloginfo('name') . ' ' . __('Database Backup','wp-db-backup'), $message, $headers);
$this->useMailer = false;
} else {
$success = @mail($recipient, get_bloginfo('name') . ' ' . __('Database Backup','wp-db-backup'), $message, $headers);
}
if ( false == $success ) {
$msg = __('The following errors were reported:','wp-db-backup') . "\n ";
$msg = ( function_exists('error_get_last') ) ? error_get_last('message') : __('ERROR: The mail application has failed to deliver the backup.','wp-db-backup');
$this->error($msg);
}
unlink($this->diskfile);
}
return $success;
}
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 = '
' . __('Backup Successful','wp-db-backup') . '!';
$file = $this->backup_file;
switch($_POST['deliver']) {
case 'http':
$feedback .= ' ' . sprintf(__('Your backup file: %2s should begin downloading shortly.','wp-db-backup'), get_option('siteurl') . "/{$this->backup_dir}{$this->backup_file}", $this->backup_file);
break;
case 'smtp':
if (! is_email($_POST['backup_recipient'])) {
$feedback .= get_option('admin_email');
} else {
$feedback .= $_POST['backup_recipient'];
}
$feedback = ' ' . sprintf(__('Your backup has been emailed to %s','wp-db-backup'), $feedback);
break;
case 'none':
$feedback .= ' ' . __('Your backup file has been saved on the server. If you would like to download it now, right click and select "Save As"','wp-db-backup');
$feedback .= ': backup_dir}$file\">$file : " . sprintf(__('%s bytes','wp-db-backup'), filesize(ABSPATH . $this->backup_dir . $file));
}
$feedback .= '
';
endif;
$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, $this->core_table_names);
// Get list of non-WP tables
$other_tables = array_diff($all_tables, $wp_backup_default_tables);
if ('' != $feedback)
echo $feedback;
if ( ! $this->wp_secure() )
return;
// 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 '
' . __('WARNING: Your wp-content directory is NOT writable! We can not create the backup directory.','wp-db-backup') . ' ' . ABSPATH . $this->backup_dir . "