3 Plugin Name: WordPress Database Backup
4 Plugin URI: http://www.skippy.net/blog/plugins/
5 Description: On-demand backup of your WordPress database.
8 Author URI: http://www.skippy.net/
10 Much of this was modified from Mark Ghosh's One Click Backup, which
11 in turn was derived from phpMyAdmin.
13 Many thanks to Owen (http://asymptomatic.net/wp/) for his patch
14 http://dev.wp-plugins.org/ticket/219
17 // CHANGE THIS IF YOU WANT TO USE A
18 // DIFFERENT BACKUP LOCATION
20 $rand = substr( md5( md5( DB_PASSWORD ) ), -5 );
22 define('WP_BACKUP_DIR', 'wp-content/backup-' . $rand);
24 define('ROWS_PER_SEGMENT', 100);
28 var $backup_complete = false;
29 var $backup_file = '';
30 var $backup_dir = WP_BACKUP_DIR;
31 var $backup_errors = array();
35 return function_exists('gzopen');
38 function wpdbBackup() {
39 add_action('wp_cron_daily', array(&$this, 'wp_cron_daily'));
41 $this->backup_dir = trailingslashit($this->backup_dir);
42 $this->basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__);
44 if (isset($_POST['do_backup'])) {
45 if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.'));
46 switch($_POST['do_backup']) {
48 $this->perform_backup();
51 add_action('admin_menu', array(&$this, 'fragment_menu'));
54 } elseif (isset($_GET['fragment'] )) {
55 if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.'));
56 add_action('init', array(&$this, 'init'));
57 } elseif (isset($_GET['backup'] )) {
58 if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.'));
59 add_action('init', array(&$this, 'init'));
61 add_action('admin_menu', array(&$this, 'admin_menu'));
66 if ( !current_user_can('import') ) die(__('You are not allowed to perform backups.'));
68 if (isset($_GET['backup'])) {
69 $via = isset($_GET['via']) ? $_GET['via'] : 'http';
71 $this->backup_file = $_GET['backup'];
72 $this->validate_file($this->backup_file);
77 $this->deliver_backup ($this->backup_file, 'smtp', $_GET['recipient']);
80 <script type="text/javascript"><!--\\
82 if($this->backup_errors) {
83 foreach($this->backup_errors as $error) {
84 echo "window.parent.addError('$error');\n";
88 alert("' . __('Backup Complete!') . '");
93 $this->deliver_backup ($this->backup_file, $via);
97 if (isset($_GET['fragment'] )) {
98 list($table, $segment, $filename) = explode(':', $_GET['fragment']);
99 $this->validate_file($filename);
100 $this->backup_fragment($table, $segment, $filename);
106 function build_backup_script() {
107 global $table_prefix, $wpdb;
109 $datum = date("Ymd_B");
110 $backup_filename = DB_NAME . "_$table_prefix$datum.sql";
111 if ($this->gzip()) $backup_filename .= '.gz';
113 echo "<div class='wrap'>";
114 //echo "<pre>" . print_r($_POST, 1) . "</pre>";
115 echo '<h2>' . __('Backup') . '</h2>
116 <fieldset class="options"><legend>' . __('Progress') . '</legend>
118 __('DO NOT DO THE FOLLOWING AS IT WILL CAUSE YOUR BACKUP TO FAIL:').
121 <li>'.__('Close this browser').'</li>
122 <li>'.__('Reload this page').'</li>
123 <li>'.__('Click the Stop or Back buttons in your browser').'</li>
125 <p><strong>' . __('Progress:') . '</strong></p>
126 <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>
127 <div id="progress_message"></div>
128 <div id="errors"></div>
130 <iframe id="backuploader" src="about:blank" style="border:0px solid white;height:1em;width:1em;"></iframe>
131 <script type="text/javascript"><!--//
132 function setMeter(pct) {
133 var meter = document.getElementById("meter");
134 meter.style.width = pct + "%";
135 meter.innerHTML = Math.floor(pct) + "%";
137 function setProgress(str) {
138 var progress = document.getElementById("progress_message");
139 progress.innerHTML = str;
141 function addError(str) {
142 var errors = document.getElementById("errors");
143 errors.innerHTML = errors.innerHTML + str + "<br />";
146 function backup(table, segment) {
147 var fram = document.getElementById("backuploader");
148 fram.src = "' . $_SERVER['REQUEST_URI'] . '&fragment=" + table + ":" + segment + ":' . $backup_filename . '";
153 function nextStep() {
158 function finishBackup() {
159 var fram = document.getElementById("backuploader");
163 $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__);
164 $download_uri = get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup={$backup_filename}";
165 switch($_POST['deliver']) {
168 setProgress("' . sprintf(__("Backup complete, preparing <a href=\\\"%s\\\">backup</a> for download..."), $download_uri) . '");
169 fram.src = "' . $download_uri . '";
174 setProgress("' . sprintf(__("Backup complete, sending <a href=\\\"%s\\\">backup</a> via email..."), $download_uri) . '");
175 fram.src = "' . $download_uri . '&via=email&recipient=' . $_POST['backup_recipient'] . '";
180 setProgress("' . sprintf(__("Backup complete, download <a href=\\\"%s\\\">here</a>."), $download_uri) . '");
187 function backupStep(step) {
189 case 0: backup("", 0); break;
192 $also_backup = array();
193 if (isset($_POST['other_tables'])) {
194 $also_backup = $_POST['other_tables'];
196 $also_backup = array();
198 $core_tables = $_POST['core_tables'];
199 $tables = array_merge($core_tables, $also_backup);
201 foreach ($tables as $table) {
202 $rec_count = $wpdb->get_var("SELECT count(*) FROM {$table}");
203 $rec_segments = ceil($rec_count / ROWS_PER_SEGMENT);
206 echo "case {$step_count}: backup(\"{$table}\", {$table_count}); break;\n";
209 } while($table_count < $rec_segments);
210 echo "case {$step_count}: backup(\"{$table}\", -1); break;\n";
213 echo "case {$step_count}: finishBackup(); break;";
217 if(step != 0) setMeter(100 * step / ' . $step_count . ');
226 function backup_fragment($table, $segment, $filename) {
227 global $table_prefix, $wpdb;
229 echo "$table:$segment:$filename";
232 $msg = __('Creating backup file...');
235 $msg = sprintf(__('Finished backing up table \\"%s\\".'), $table);
237 $msg = sprintf(__('Backing up table \\"%s\\"...'), $table);
241 echo '<script type="text/javascript"><!--//
242 var msg = "' . $msg . '";
243 window.parent.setProgress(msg);
246 if (is_writable(ABSPATH . $this->backup_dir)) {
247 $this->fp = $this->open(ABSPATH . $this->backup_dir . $filename, 'a');
249 $this->backup_error(__('Could not open the backup file for writing!'));
250 $this->fatal_error = __('The backup file could not be saved. Please check the permissions for writing to your backup directory and try again.');
254 //Begin new backup of MySql
255 $this->stow("# WordPress MySQL database backup\n");
257 $this->stow("# Generated: " . date("l j. F Y H:i T") . "\n");
258 $this->stow("# Hostname: " . DB_HOST . "\n");
259 $this->stow("# Database: " . $this->backquote(DB_NAME) . "\n");
260 $this->stow("# --------------------------------------------------------\n");
263 // Increase script execution time-limit to 15 min for every table.
264 if ( !ini_get('safe_mode')) @set_time_limit(15*60);
265 //ini_set('memory_limit', '16M');
266 // Create the SQL statements
267 $this->stow("# --------------------------------------------------------\n");
268 $this->stow("# Table: " . $this->backquote($table) . "\n");
269 $this->stow("# --------------------------------------------------------\n");
271 $this->backup_table($table, $segment);
275 $this->backup_error(__('The backup directory is not writeable!'));
276 $this->fatal_error = __('The backup directory is not writeable! Please check the permissions for writing to your backup directory and try again.');
279 if($this->fp) $this->close($this->fp);
281 if($this->backup_errors) {
282 foreach($this->backup_errors as $error) {
283 echo "window.parent.addError('$error');\n";
286 if($this->fatal_error) {
288 alert("' . addslashes($this->fatal_error) . '");
294 window.parent.nextStep();
302 function perform_backup() {
303 // are we backing up any other tables?
304 $also_backup = array();
305 if (isset($_POST['other_tables'])) {
306 $also_backup = $_POST['other_tables'];
309 $core_tables = $_POST['core_tables'];
310 $this->backup_file = $this->db_backup($core_tables, $also_backup);
311 if (FALSE !== $this->backup_file) {
312 if ('smtp' == $_POST['deliver']) {
313 $this->deliver_backup ($this->backup_file, $_POST['deliver'], $_POST['backup_recipient']);
314 } elseif ('http' == $_POST['deliver']) {
315 $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__);
316 header('Refresh: 3; ' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}&backup={$this->backup_file}");
318 // we do this to say we're done.
319 $this->backup_complete = true;
323 ///////////////////////////////
324 function admin_menu() {
325 add_management_page(__('Backup'), __('Backup'), 'import', basename(__FILE__), array(&$this, 'backup_menu'));
328 function fragment_menu() {
329 add_management_page(__('Backup'), __('Backup'), 'import', basename(__FILE__), array(&$this, 'build_backup_script'));
332 /////////////////////////////////////////////////////////
333 function sql_addslashes($a_string = '', $is_like = FALSE)
336 Better addslashes for SQL queries.
337 Taken from phpMyAdmin.
340 $a_string = str_replace('\\', '\\\\\\\\', $a_string);
342 $a_string = str_replace('\\', '\\\\', $a_string);
344 $a_string = str_replace('\'', '\\\'', $a_string);
347 } // function sql_addslashes($a_string = '', $is_like = FALSE)
349 ///////////////////////////////////////////////////////////
350 function backquote($a_name)
353 Add backqouotes to tables and db-names in
354 SQL queries. Taken from phpMyAdmin.
356 if (!empty($a_name) && $a_name != '*') {
357 if (is_array($a_name)) {
360 while(list($key, $val) = each($a_name)) {
361 $result[$key] = '`' . $val . '`';
365 return '`' . $a_name . '`';
370 } // function backquote($a_name, $do_it = TRUE)
373 function open($filename = '', $mode = 'w') {
374 if ('' == $filename) return false;
376 $fp = @gzopen($filename, $mode);
378 $fp = @fopen($filename, $mode);
384 function close($fp) {
393 function stow($query_line) {
395 if(@gzwrite($this->fp, $query_line) === FALSE) {
396 backup_error(__('There was an error writing a line to the backup script:'));
397 backup_error(' ' . $query_line);
400 if(@fwrite($this->fp, $query_line) === FALSE) {
401 backup_error(__('There was an error writing a line to the backup script:'));
402 backup_error(' ' . $query_line);
407 function backup_error($err) {
408 if(count($this->backup_errors) < 20) {
409 $this->backup_errors[] = $err;
410 } elseif(count($this->backup_errors) == 20) {
411 $this->backup_errors[] = __('Subsequent errors have been omitted from this log.');
415 /////////////////////////////
416 function backup_table($table, $segment = 'none') {
420 Taken partially from phpMyAdmin and partially from
421 Alain Wolf, Zurich - Switzerland
422 Website: http://restkultur.ch/personal/wolf/scripts/db_backup/
424 Modified by Scott Merril (http://www.skippy.net/)
425 to use the WordPress $wpdb object
428 $table_structure = $wpdb->get_results("DESCRIBE $table");
429 if (! $table_structure) {
430 backup_errors(__('Error getting table details') . ": $table");
434 if(($segment == 'none') || ($segment == 0)) {
436 // Add SQL statement to drop existing table
439 $this->stow("# Delete any existing table " . $this->backquote($table) . "\n");
442 $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n");
446 // Comment in SQL-file
449 $this->stow("# Table structure of table " . $this->backquote($table) . "\n");
453 $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N);
454 if (FALSE === $create_table) {
455 $this->backup_error(sprintf(__("Error with SHOW CREATE TABLE for %s."), $table));
456 $this->stow("#\n# Error with SHOW CREATE TABLE for $table!\n#\n");
458 $this->stow($create_table[0][1] . ' ;');
460 if (FALSE === $table_structure) {
461 $this->backup_error(sprintf(__("Error getting table structure of %s"), $table));
462 $this->stow("#\n# Error getting table structure of $table!\n#\n");
466 // Comment in SQL-file
469 $this->stow('# Data contents of table ' . $this->backquote($table) . "\n");
473 if(($segment == 'none') || ($segment >= 0)) {
475 foreach ($table_structure as $struct) {
476 if ( (0 === strpos($struct->Type, 'tinyint')) ||
477 (0 === strpos(strtolower($struct->Type), 'smallint')) ||
478 (0 === strpos(strtolower($struct->Type), 'mediumint')) ||
479 (0 === strpos(strtolower($struct->Type), 'int')) ||
480 (0 === strpos(strtolower($struct->Type), 'bigint')) ||
481 (0 === strpos(strtolower($struct->Type), 'timestamp')) ) {
482 $ints[strtolower($struct->Field)] = "1";
489 if($segment == 'none') {
491 $row_inc = ROWS_PER_SEGMENT;
493 $row_start = $segment * ROWS_PER_SEGMENT;
494 $row_inc = ROWS_PER_SEGMENT;
498 if ( !ini_get('safe_mode')) @set_time_limit(15*60);
499 $table_data = $wpdb->get_results("SELECT * FROM $table LIMIT {$row_start}, {$row_inc}", ARRAY_A);
502 if (FALSE === $table_data) {
503 $wp_backup_error .= "Error getting table contents from $table\r\n";
504 fwrite($fp, "#\n# Error getting table contents fom $table!\n#\n");
508 $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES (';
509 // \x08\\x09, not required
510 $search = array("\x00", "\x0a", "\x0d", "\x1a");
511 $replace = array('\0', '\n', '\r', '\Z');
513 foreach ($table_data as $row) {
515 foreach ($row as $key => $value) {
516 if ($ints[strtolower($key)]) {
519 $values[] = "'" . str_replace($search, $replace, $this->sql_addslashes($value)) . "'";
522 $this->stow(" \n" . $entries . implode(', ', $values) . ') ;');
524 $row_start += $row_inc;
526 } while((count($table_data) > 0) and ($segment=='none'));
530 if(($segment == 'none') || ($segment < 0)) {
531 // Create footer/closing comment in SQL-file
534 $this->stow("# End of data contents of table " . $this->backquote($table) . "\n");
535 $this->stow("# --------------------------------------------------------\n");
539 } // end backup_table()
541 function return_bytes($val) {
543 $last = strtolower($val{strlen($val)-1});
545 // The 'G' modifier is available since PHP 5.1.0
557 ////////////////////////////
558 function db_backup($core_tables, $other_tables) {
559 global $table_prefix, $wpdb;
561 $datum = date("Ymd_B");
562 $wp_backup_filename = DB_NAME . "_$table_prefix$datum.sql";
564 $wp_backup_filename .= '.gz';
567 if (is_writable(ABSPATH . $this->backup_dir)) {
568 $this->fp = $this->open(ABSPATH . $this->backup_dir . $wp_backup_filename);
570 $this->backup_error(__('Could not open the backup file for writing!'));
574 $this->backup_error(__('The backup directory is not writeable!'));
578 //Begin new backup of MySql
579 $this->stow("# WordPress MySQL database backup\n");
581 $this->stow("# Generated: " . date("l j. F Y H:i T") . "\n");
582 $this->stow("# Hostname: " . DB_HOST . "\n");
583 $this->stow("# Database: " . $this->backquote(DB_NAME) . "\n");
584 $this->stow("# --------------------------------------------------------\n");
586 if ( (is_array($other_tables)) && (count($other_tables) > 0) )
587 $tables = array_merge($core_tables, $other_tables);
589 $tables = $core_tables;
591 foreach ($tables as $table) {
592 // Increase script execution time-limit to 15 min for every table.
593 if ( !ini_get('safe_mode')) @set_time_limit(15*60);
594 // Create the SQL statements
595 $this->stow("# --------------------------------------------------------\n");
596 $this->stow("# Table: " . $this->backquote($table) . "\n");
597 $this->stow("# --------------------------------------------------------\n");
598 $this->backup_table($table);
601 $this->close($this->fp);
603 if (count($this->backup_errors)) {
606 return $wp_backup_filename;
611 ///////////////////////////
612 function deliver_backup ($filename = '', $delivery = 'http', $recipient = '') {
613 if ('' == $filename) { return FALSE; }
615 $diskfile = ABSPATH . $this->backup_dir . $filename;
616 if ('http' == $delivery) {
617 if (! file_exists($diskfile)) {
618 $msg = sprintf(__('File not found:%s'), "<br /><strong>$filename</strong><br />");
619 $this_basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__);
620 $msg .= '<br /><a href="' . get_settings('siteurl') . "/wp-admin/edit.php?page={$this_basename}" . '">' . __('Return to Backup');
623 header('Content-Description: File Transfer');
624 header('Content-Type: application/octet-stream');
625 header('Content-Length: ' . filesize($diskfile));
626 header("Content-Disposition: attachment; filename=$filename");
629 } elseif ('smtp' == $delivery) {
630 if (! file_exists($diskfile)) return false;
632 if (! is_email ($recipient)) {
633 $recipient = get_settings('admin_email');
635 $randomish = md5(time());
636 $boundary = "==WPBACKUP-BY-SKIPPY-$randomish";
637 $fp = fopen($diskfile,"rb");
638 $file = fread($fp,filesize($diskfile));
640 $data = chunk_split(base64_encode($file));
641 $headers = "MIME-Version: 1.0\n";
642 $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
643 $headers .= 'From: ' . get_settings('admin_email') . "\n";
645 $message = sprintf(__("Attached to this email is\n %1s\n Size:%2s kilobytes\n"), $filename, round(filesize($diskfile)/1024));
646 // Add a multipart boundary above the plain message
647 $message = "This is a multi-part message in MIME format.\n\n" .
649 "Content-Type: text/plain; charset=\"utf-8\"\n" .
650 "Content-Transfer-Encoding: 7bit\n\n" .
653 // Add file attachment to the message
654 $message .= "--{$boundary}\n" .
655 "Content-Type: application/octet-stream;\n" .
656 " name=\"{$filename}\"\n" .
657 "Content-Disposition: attachment;\n" .
658 " filename=\"{$filename}\"\n" .
659 "Content-Transfer-Encoding: base64\n\n" .
663 if (function_exists('wp_mail')) {
664 wp_mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers);
666 mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers);
674 ////////////////////////////
675 function backup_menu() {
676 global $table_prefix, $wpdb;
680 // did we just do a backup? If so, let's report the status
681 if ( $this->backup_complete ) {
682 $feedback = '<div class="updated"><p>' . __('Backup Successful') . '!';
683 $file = $this->backup_file;
684 switch($_POST['deliver']) {
686 $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);
689 if (! is_email($_POST['backup_recipient'])) {
690 $feedback .= get_settings('admin_email');
692 $feedback .= $_POST['backup_recipient'];
694 $feedback = '<br />' . sprintf(__('Your backup has been emailed to %s'), $feedback);
697 $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"');
698 $feedback .= ':<br /> <a href="' . get_settings('siteurl') . "/{$this->backup_dir}$file\">$file</a> : " . sprintf(__('%s bytes'), filesize(ABSPATH . $this->backup_dir . $file));
700 $feedback .= '</p></div>';
703 if (count($this->backup_errors)) {
704 $feedback .= '<div class="updated error">' . __('The following errors were reported:') . "<pre>";
705 foreach($this->backup_errors as $error) {
706 $feedback .= "{$error}\n"; //Errors are already localized
708 $feedback .= "</pre></div>";
711 // did we just save options for wp-cron?
712 if ( (function_exists('wp_cron_init')) && isset($_POST['wp_cron_backup_options']) ) {
713 update_option('wp_cron_backup_schedule', intval($_POST['cron_schedule']), FALSE);
714 update_option('wp_cron_backup_tables', $_POST['wp_cron_backup_tables']);
715 if (is_email($_POST['cron_backup_recipient'])) {
716 update_option('wp_cron_backup_recipient', $_POST['cron_backup_recipient'], FALSE);
718 $feedback .= '<div class="updated"><p>' . __('Scheduled Backup Options Saved!') . '</p></div>';
721 // Simple table name storage
722 $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta');
723 // Apply WP DB prefix to table names
724 $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names);
726 $other_tables = array();
727 $also_backup = array();
729 // Get complete db table list
730 $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
731 $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables);
732 // Get list of WP tables that actually exist in this DB (for 1.6 compat!)
733 $wp_backup_default_tables = array_intersect($all_tables, $wp_table_names);
734 // Get list of non-WP tables
735 $other_tables = array_diff($all_tables, $wp_backup_default_tables);
737 if ('' != $feedback) {
741 // Give the new dirs the same perms as wp-content.
742 $stat = stat( ABSPATH . 'wp-content' );
743 $dir_perms = $stat['mode'] & 0000777; // Get the permission bits.
745 if ( !file_exists( ABSPATH . $this->backup_dir) ) {
746 if ( @ mkdir( ABSPATH . $this->backup_dir) ) {
747 @ chmod( ABSPATH . $this->backup_dir, $dir_perms);
749 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>";
754 if ( !is_writable( ABSPATH . $this->backup_dir) ) {
755 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>";
758 if ( !file_exists( ABSPATH . $this->backup_dir . 'index.php') ) {
759 @ touch( ABSPATH . $this->backup_dir . "index.php");
762 echo "<div class='wrap'>";
763 echo '<h2>' . __('Backup') . '</h2>';
764 echo '<fieldset class="options"><legend>' . __('Tables') . '</legend>';
765 echo '<form method="post">';
766 echo '<table align="center" cellspacing="5" cellpadding="5"><tr><td width="50%" align="left" class="alternate" valign="top">';
767 echo __('These core WordPress tables will always be backed up:') . '<br /><ul>';
768 foreach ($wp_backup_default_tables as $table) {
769 echo "<li><input type='hidden' name='core_tables[]' value='$table' />$table</li>";
771 echo '</ul></td><td width="50%" align="left" valign="top">';
772 if (count($other_tables) > 0) {
773 echo __('You may choose to include any of the following tables:') . ' <br />';
774 foreach ($other_tables as $table) {
775 echo "<label style=\"display:block;\"><input type='checkbox' name='other_tables[]' value='{$table}' /> {$table}</label>";
778 echo '</tr></table></fieldset>';
779 echo '<fieldset class="options"><legend>' . __('Backup Options') . '</legend>';
780 echo __('What to do with the backup file:') . "<br />";
781 echo '<label style="display:block;"><input type="radio" name="deliver" value="none" /> ' . __('Save to server') . " ({$this->backup_dir})</label>";
782 echo '<label style="display:block;"><input type="radio" checked="checked" name="deliver" value="http" /> ' . __('Download to your computer') . '</label>';
783 echo '<div><input type="radio" name="deliver" id="do_email" value="smtp" /> ';
784 echo '<label for="do_email">'.__('Email backup to:').'</label><input type="text" name="backup_recipient" size="20" value="' . get_settings('admin_email') . '" />';
787 $table_status = $wpdb->get_results("SHOW TABLE STATUS FROM " . $this->backquote(DB_NAME));
788 $core_size = $db_size = 0;
789 foreach($table_status as $table) {
790 $table_size = $table->Data_length - $table->Data_free;
791 if(in_array($table->Name, $wp_backup_default_tables)) {
792 $core_size += $table_size;
794 $db_size += $table_size;
796 $mem_limit = ini_get('memory_limit');
797 $mem_limit = $this->return_bytes($mem_limit);
798 $mem_limit = ($mem_limit == 0) ? 8*1024*1024 : $mem_limit - 2000000;
801 echo '<input type="hidden" name="do_backup" id="do_backup" value="backup" /></div>';
802 echo '<p class="submit"><input type="submit" name="submit" onclick="document.getElementById(\'do_backup\').value=\'fragments\';" value="' . __('Backup') . '!" / ></p>';
804 echo '<p class="alternate">' . __('WARNING: Your backup directory is <strong>NOT</strong> writable!') . '</p>';
809 // this stuff only displays if wp_cron is installed
810 if (function_exists('wp_cron_init')) {
811 echo '<fieldset class="options"><legend>' . __('Scheduled Backup') . '</legend>';
812 $datetime = get_settings('date_format') . ' @ ' . get_settings('time_format');
813 echo '<p>' . __('Last WP-Cron Daily Execution') . ': ' . date($datetime, get_option('wp_cron_daily_lastrun')) . '<br />';
814 echo __('Next WP-Cron Daily Execution') . ': ' . date($datetime, (get_option('wp_cron_daily_lastrun') + 86400)) . '</p>';
815 echo '<form method="post">';
816 echo '<table width="100%" callpadding="5" cellspacing="5">';
817 echo '<tr><td align="center">';
818 echo __('Schedule: ');
819 $wp_cron_backup_schedule = get_option('wp_cron_backup_schedule');
820 $schedule = array(0 => __('None'), 1 => __('Daily'));
821 foreach ($schedule as $value => $name) {
822 echo ' <input type="radio" name="cron_schedule"';
823 if ($wp_cron_backup_schedule == $value) {
824 echo ' checked="checked" ';
826 echo 'value="' . $value . '" /> ' . __($name);
828 echo '</td><td align="center">';
829 $cron_recipient = get_option('wp_cron_backup_recipient');
830 if (! is_email($cron_recipient)) {
831 $cron_recipient = get_settings('admin_email');
833 echo __('Email backup to:') . ' <input type="text" name="cron_backup_recipient" size="20" value="' . $cron_recipient . '" />';
835 $cron_tables = get_option('wp_cron_backup_tables');
836 if (! is_array($cron_tables)) {
837 $cron_tables = array();
839 if (count($other_tables) > 0) {
840 echo '<tr><td colspan="2" align="left">' . __('Tables to include:') . '<br />';
841 foreach ($other_tables as $table) {
842 echo '<input type="checkbox" ';
843 if (in_array($table, $cron_tables)) {
844 echo 'checked=checked ';
846 echo "name='wp_cron_backup_tables[]' value='{$table}' /> {$table}<br />";
850 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>';
853 // end of wp_cron section
857 }// end wp_backup_menu()
859 /////////////////////////////
860 function wp_cron_daily() {
862 $schedule = intval(get_option('wp_cron_backup_schedule'));
863 if (0 == $schedule) {
864 // Scheduled backup is disabled
868 global $table_prefix, $wpdb;
870 $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta');
871 $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names);
872 $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N);
873 $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables);
874 $core_tables = array_intersect($all_tables, $wp_table_names);
875 $other_tables = get_option('wp_cron_backup_tables');
877 $recipient = get_option('wp_cron_backup_recipient');
879 $backup_file = $this->db_backup($core_tables, $other_tables);
880 if (FALSE !== $backup_file) {
881 $this->deliver_backup ($backup_file, 'smtp', $recipient);
885 } // wp_cron_db_backup
887 function validate_file($file) {
888 if (false !== strpos($file, '..'))
889 die(__("Cheatin' uh ?"));
891 if (false !== strpos($file, './'))
892 die(__("Cheatin' uh ?"));
894 if (':' == substr($file, 1, 1))
895 die(__("Cheatin' uh ?"));
900 function wpdbBackup_init() {
901 global $mywpdbbackup;
903 if ( !current_user_can('import') ) return;
905 $mywpdbbackup = new wpdbBackup();
908 add_action('plugins_loaded', 'wpdbBackup_init');