Changeset 3517 for trunk/wp-content/plugins/wp-db-backup.php
- Timestamp:
- 02/12/2006 07:53:23 AM (19 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-content/plugins/wp-db-backup.php
r3447 r3517 37 37 38 38 function wpdbBackup() { 39 39 40 40 add_action('wp_cron_daily', array(&$this, 'wp_cron_daily')); 41 41 42 42 $this->backup_dir = trailingslashit($this->backup_dir); 43 43 $this->basename = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', __FILE__); 44 44 45 45 if (isset($_POST['do_backup'])) { 46 46 switch($_POST['do_backup']) { … … 50 50 case 'fragments': 51 51 add_action('admin_menu', array(&$this, 'fragment_menu')); 52 break; 52 break; 53 53 } 54 54 } elseif (isset($_GET['fragment'] )) { … … 60 60 } 61 61 } 62 62 63 63 function init() { 64 64 global $user_level; … … 69 69 if (isset($_GET['backup'])) { 70 70 $via = isset($_GET['via']) ? $_GET['via'] : 'http'; 71 71 72 72 $this->backup_file = $_GET['backup']; 73 73 74 74 switch($via) { 75 75 case 'smtp': … … 102 102 die(); 103 103 } 104 104 105 105 function build_backup_script() { 106 106 global $table_prefix, $wpdb; 107 107 108 108 $datum = date("Ymd_B"); 109 109 $backup_filename = DB_NAME . "_$table_prefix$datum.sql"; 110 110 if ($this->gzip()) $backup_filename .= '.gz'; 111 111 112 112 echo "<div class='wrap'>"; 113 113 //echo "<pre>" . print_r($_POST, 1) . "</pre>"; … … 144 144 145 145 function backup(table, segment) { 146 var fram = document.getElementById("backuploader"); 146 var fram = document.getElementById("backuploader"); 147 147 fram.src = "' . $_SERVER['REQUEST_URI'] . '&fragment=" + table + ":" + segment + ":' . $backup_filename . '"; 148 148 } 149 149 150 150 var curStep = 0; 151 151 152 152 function nextStep() { 153 153 backupStep(curStep); 154 154 curStep++; 155 155 } 156 156 157 157 function finishBackup() { 158 var fram = document.getElementById("backuploader"); 158 var fram = document.getElementById("backuploader"); 159 159 setMeter(100); 160 160 '; … … 180 180 '; 181 181 } 182 182 183 183 echo ' 184 184 } 185 185 186 186 function backupStep(step) { 187 187 switch(step) { 188 188 case 0: backup("", 0); break; 189 189 '; 190 190 191 191 $also_backup = array(); 192 192 if (isset($_POST['other_tables'])) { … … 211 211 } 212 212 echo "case {$step_count}: finishBackup(); break;"; 213 213 214 214 echo ' 215 215 } … … 225 225 function backup_fragment($table, $segment, $filename) { 226 226 global $table_prefix, $wpdb; 227 227 228 228 echo "$table:$segment:$filename"; 229 229 230 230 if($table == '') { 231 231 $msg = __('Creating backup file...'); … … 237 237 } 238 238 } 239 239 240 240 echo '<script type="text/javascript"><!--// 241 241 var msg = "' . $msg . '"; 242 242 window.parent.setProgress(msg); 243 243 '; 244 244 245 245 if (is_writable(ABSPATH . $this->backup_dir)) { 246 246 $this->fp = $this->open(ABSPATH . $this->backup_dir . $filename, 'a'); … … 250 250 } 251 251 else { 252 if($table == '') { 252 if($table == '') { 253 253 //Begin new backup of MySql 254 254 $this->stow("# WordPress MySQL database backup\n"); … … 267 267 $this->stow("# Table: " . $this->backquote($table) . "\n"); 268 268 $this->stow("# --------------------------------------------------------\n"); 269 } 269 } 270 270 $this->backup_table($table, $segment); 271 271 } … … 277 277 278 278 if($this->fp) $this->close($this->fp); 279 279 280 280 if($this->backup_errors) { 281 281 foreach($this->backup_errors as $error) { … … 295 295 '; 296 296 } 297 297 298 298 die(); 299 299 } … … 305 305 $also_backup = $_POST['other_tables']; 306 306 } 307 307 308 308 $core_tables = $_POST['core_tables']; 309 309 $this->backup_file = $this->db_backup($core_tables, $also_backup); … … 319 319 } 320 320 } 321 321 322 322 /////////////////////////////// 323 323 function admin_menu() { … … 388 388 } 389 389 } 390 390 391 391 ////////////// 392 392 function stow($query_line) { … … 403 403 } 404 404 } 405 405 406 406 function backup_error($err) { 407 407 if(count($this->backup_errors) < 20) { … … 411 411 } 412 412 } 413 413 414 414 ///////////////////////////// 415 415 function backup_table($table, $segment = 'none') { 416 416 global $wpdb; 417 417 418 418 /* 419 419 Taken partially from phpMyAdmin and partially from 420 420 Alain Wolf, Zurich - Switzerland 421 421 Website: http://restkultur.ch/personal/wolf/scripts/db_backup/ 422 422 423 423 Modified by Scott Merril (http://www.skippy.net/) 424 424 to use the WordPress $wpdb object … … 430 430 return FALSE; 431 431 } 432 432 433 433 if(($segment == 'none') || ($segment == 0)) { 434 434 // … … 440 440 $this->stow("\n"); 441 441 $this->stow("DROP TABLE IF EXISTS " . $this->backquote($table) . ";\n"); 442 442 443 443 // 444 444 //Table structure … … 449 449 $this->stow("#\n"); 450 450 $this->stow("\n"); 451 451 452 452 $create_table = $wpdb->get_results("SHOW CREATE TABLE $table", ARRAY_N); 453 453 if (FALSE === $create_table) { … … 456 456 } 457 457 $this->stow($create_table[0][1] . ' ;'); 458 458 459 459 if (FALSE === $table_structure) { 460 460 $this->backup_error(sprintf(__("Error getting table structure of %s"), $table)); 461 461 $this->stow("#\n# Error getting table structure of $table!\n#\n"); 462 462 } 463 463 464 464 // 465 465 // Comment in SQL-file … … 469 469 $this->stow("#\n"); 470 470 } 471 471 472 472 if(($segment == 'none') || ($segment >= 0)) { 473 473 $ints = array(); … … 482 482 } 483 483 } 484 485 484 485 486 486 // Batch by $row_inc 487 487 488 488 if($segment == 'none') { 489 489 $row_start = 0; … … 493 493 $row_inc = ROWS_PER_SEGMENT; 494 494 } 495 496 do { 495 496 do { 497 497 if ( !ini_get('safe_mode')) @set_time_limit(15*60); 498 498 $table_data = $wpdb->get_results("SELECT * FROM $table LIMIT {$row_start}, {$row_inc}", ARRAY_A); … … 504 504 } 505 505 */ 506 507 $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES ('; 506 507 $entries = 'INSERT INTO ' . $this->backquote($table) . ' VALUES ('; 508 508 // \x08\\x09, not required 509 509 $search = array("\x00", "\x0a", "\x0d", "\x1a"); … … 525 525 } while((count($table_data) > 0) and ($segment=='none')); 526 526 } 527 528 527 528 529 529 if(($segment == 'none') || ($segment < 0)) { 530 530 // Create footer/closing comment in SQL-file … … 535 535 $this->stow("\n"); 536 536 } 537 537 538 538 } // end backup_table() 539 539 540 540 function return_bytes($val) { 541 541 $val = trim($val); … … 550 550 $val *= 1024; 551 551 } 552 552 553 553 return $val; 554 554 } 555 555 556 556 //////////////////////////// 557 557 function db_backup($core_tables, $other_tables) { 558 558 global $table_prefix, $wpdb; 559 559 560 560 $datum = date("Ymd_B"); 561 561 $wp_backup_filename = DB_NAME . "_$table_prefix$datum.sql"; … … 563 563 $wp_backup_filename .= '.gz'; 564 564 } 565 565 566 566 if (is_writable(ABSPATH . $this->backup_dir)) { 567 567 $this->fp = $this->open(ABSPATH . $this->backup_dir . $wp_backup_filename); … … 574 574 return false; 575 575 } 576 576 577 577 //Begin new backup of MySql 578 578 $this->stow("# WordPress MySQL database backup\n"); … … 582 582 $this->stow("# Database: " . $this->backquote(DB_NAME) . "\n"); 583 583 $this->stow("# --------------------------------------------------------\n"); 584 584 585 585 if ( (is_array($other_tables)) && (count($other_tables) > 0) ) 586 586 $tables = array_merge($core_tables, $other_tables); 587 587 else 588 588 $tables = $core_tables; 589 589 590 590 foreach ($tables as $table) { 591 591 // Increase script execution time-limit to 15 min for every table. … … 597 597 $this->backup_table($table); 598 598 } 599 599 600 600 $this->close($this->fp); 601 601 602 602 if (count($this->backup_errors)) { 603 603 return false; … … 605 605 return $wp_backup_filename; 606 606 } 607 607 608 608 } //wp_db_backup 609 609 610 610 /////////////////////////// 611 611 function deliver_backup ($filename = '', $delivery = 'http', $recipient = '') { 612 612 if ('' == $filename) { return FALSE; } 613 613 614 614 $diskfile = ABSPATH . $this->backup_dir . $filename; 615 615 if ('http' == $delivery) { … … 641 641 $headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n"; 642 642 $headers .= 'From: ' . get_settings('admin_email') . "\n"; 643 643 644 644 $message = sprintf(__("Attached to this email is\n %1s\n Size:%2s kilobytes\n"), $filename, round(filesize($diskfile)/1024)); 645 645 // Add a multipart boundary above the plain message … … 649 649 "Content-Transfer-Encoding: 7bit\n\n" . 650 650 $message . "\n\n"; 651 651 652 652 // Add file attachment to the message 653 653 $message .= "--{$boundary}\n" . … … 659 659 $data . "\n\n" . 660 660 "--{$boundary}--\n"; 661 661 662 662 if (function_exists('wp_mail')) { 663 663 wp_mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers); … … 665 665 mail ($recipient, get_bloginfo('name') . ' ' . __('Database Backup'), $message, $headers); 666 666 } 667 667 668 668 unlink($diskfile); 669 669 } 670 670 return; 671 671 } 672 672 673 673 //////////////////////////// 674 674 function backup_menu() { … … 676 676 $feedback = ''; 677 677 $WHOOPS = FALSE; 678 678 679 679 // did we just do a backup? If so, let's report the status 680 680 if ( $this->backup_complete ) { … … 699 699 $feedback .= '</p></div>'; 700 700 } 701 701 702 702 if (count($this->backup_errors)) { 703 703 $feedback .= '<div class="updated error">' . __('The following errors were reported:') . "<pre>"; … … 707 707 $feedback .= "</pre></div>"; 708 708 } 709 709 710 710 // did we just save options for wp-cron? 711 711 if ( (function_exists('wp_cron_init')) && isset($_POST['wp_cron_backup_options']) ) { … … 717 717 $feedback .= '<div class="updated"><p>' . __('Scheduled Backup Options Saved!') . '</p></div>'; 718 718 } 719 719 720 720 // Simple table name storage 721 721 $wp_table_names = explode(',','categories,comments,linkcategories,links,options,post2cat,postmeta,posts,users,usermeta'); 722 722 // Apply WP DB prefix to table names 723 723 $wp_table_names = array_map(create_function('$a', 'global $table_prefix;return "{$table_prefix}{$a}";'), $wp_table_names); 724 724 725 725 $other_tables = array(); 726 726 $also_backup = array(); 727 728 // Get complete db table list 727 728 // Get complete db table list 729 729 $all_tables = $wpdb->get_results("SHOW TABLES", ARRAY_N); 730 730 $all_tables = array_map(create_function('$a', 'return $a[0];'), $all_tables); … … 733 733 // Get list of non-WP tables 734 734 $other_tables = array_diff($all_tables, $wp_backup_default_tables); 735 735 736 736 if ('' != $feedback) { 737 737 echo $feedback; … … 750 750 } 751 751 } 752 752 753 753 if ( !is_writable( ABSPATH . $this->backup_dir) ) { 754 754 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>"; … … 782 782 echo '<div><input type="radio" name="deliver" id="do_email" value="smtp" /> '; 783 783 echo '<label for="do_email">'.__('Email backup to:').'</label><input type="text" name="backup_recipient" size="20" value="' . get_settings('admin_email') . '" />'; 784 784 785 785 // Check DB dize. 786 786 $table_status = $wpdb->get_results("SHOW TABLE STATUS FROM " . $this->backquote(DB_NAME)); … … 789 789 $table_size = $table->Data_length - $table->Data_free; 790 790 if(in_array($table->Name, $wp_backup_default_tables)) { 791 $core_size += $table_size; 791 $core_size += $table_size; 792 792 } 793 793 $db_size += $table_size; … … 796 796 $mem_limit = $this->return_bytes($mem_limit); 797 797 $mem_limit = ($mem_limit == 0) ? 8*1024*1024 : $mem_limit - 2000000; 798 798 799 799 if (! $WHOOPS) { 800 800 echo '<input type="hidden" name="do_backup" id="do_backup" value="backup" /></div>'; … … 805 805 echo '</fieldset>'; 806 806 echo '</form>'; 807 807 808 808 // this stuff only displays if wp_cron is installed 809 809 if (function_exists('wp_cron_init')) { … … 851 851 } 852 852 // end of wp_cron section 853 853 854 854 echo '</div>'; 855 855 856 856 }// end wp_backup_menu() 857 857 858 858 ///////////////////////////// 859 859 function wp_cron_daily() { 860 860 861 861 $schedule = intval(get_option('wp_cron_backup_schedule')); 862 862 if (0 == $schedule) { … … 864 864 return; 865 865 } 866 866 867 867 global $table_prefix, $wpdb; 868 868 … … 873 873 $core_tables = array_intersect($all_tables, $wp_table_names); 874 874 $other_tables = get_option('wp_cron_backup_tables'); 875 875 876 876 $recipient = get_option('wp_cron_backup_recipient'); 877 877 878 878 $backup_file = $this->db_backup($core_tables, $other_tables); 879 879 if (FALSE !== $backup_file) { 880 880 $this->deliver_backup ($backup_file, 'smtp', $recipient); 881 881 } 882 882 883 883 return; 884 884 } // wp_cron_db_backup
Note: See TracChangeset
for help on using the changeset viewer.