<?php $this->title(__('Backing Up', 'it-l10n-backupbuddy')); require_once $this->_pluginPath . '/classes/backup.php'; $backup = new pluginbuddy_backupbuddy_backup($this); if (defined('PB_DEMO_MODE')) { echo '<br>'; $this->alert('You are currently running in demo mode. A backup file will not be created.', true); } if ($backup->start_backup_process($_GET['run_backup'], 'manual') !== true) { echo __('Error #4344443: Backup failure', 'it-l10n-backupbuddy'); echo $backup->get_errors(); } ?> <script type="text/javascript"> jQuery(document).ready(function() { // Wait 2 seconds before first poll. setTimeout( 'backupbuddy_poll()' , 2000 ); jQuery("#pb_backupbuddy_advanced_details").click(function() { jQuery("#pb_backupbuddy_advanced_details_div").slideToggle(); }); setInterval( 'blink_ledz()' , 600 ); }); var stale_archive_time_trigger = 25; // If this time ellapses without archive size increasing warn user that something may have gone wrong. keep_polling = 1;
} } $files[(string) $modified_time . '+' . $filename] = array('filename' => $filename, 'backup_type' => $backup_type, 'integrity_status' => $integrity_status, 'integrity_scantime' => $integrity_scantime, 'integrity_description' => $integrity_description, 'size' => $file_stats['size'], 'modified' => $modified_time); $this->_parent->_zipbuddy->clear_status(); } if (!empty($files)) { krsort($files); } } // Save updated integrity data if a check was performed. if ($integrity_checked === true) { $this->_parent->save(); } $tip_backup_file = 'Files include random characters in their name for increased security. Verify that write permissions are available for this directory. Backup files are stored in ' . str_replace('\\', '/', $this->_options['backup_directory']); require_once $this->_pluginPath . '/classes/backup.php'; $pluginbuddy_backupbuddy_backup = new pluginbuddy_backupbuddy_backup($this); $pluginbuddy_backupbuddy_backup->trim_old_archives(); ?> <script type="text/javascript"> function pb_backupbuddy_selectdestination( destination_id, destination_title, callback_data ) { if ( callback_data != '' ) { jQuery.post( '<?php echo admin_url('admin-ajax.php'); ?> ?action=pb_backupbuddy_remotesend', { destination_id: destination_id, destination_title: destination_title, file: callback_data },
function periodic_cleanup() { $this->load(); // TODO: Check for orphaned .gz files in root from PCLZip. // TODO: Check for orphaned log files. // Check for orphaned backups in the data structure that havent been updates in 12+ hours & cleanup after them. foreach ((array) $this->_options['backups'] as $backup_serial => $backup) { if (time() - $backup['updated_time'] > 43200) { // If more than 12 hours has passed... $this->log('Cleaned up stale backup `' . $backup_serial . '`.'); $this->cron_final_cleanup($backup_serial); } } // Verify existance of anti-directory browsing files. require_once $this->_pluginPath . '/classes/backup.php'; $pluginbuddy_backupbuddy_backup = new pluginbuddy_backupbuddy_backup($this); $pluginbuddy_backupbuddy_backup->anti_directory_browsing($this->_options['backup_directory']); // Remove any copy of importbuddy.php in root. if (file_exists(ABSPATH . 'importbuddy.php')) { $this->log('Unlinked importbuddy.php in root of site.'); unlink(ABSPATH . 'importbuddy.php'); } }
function ajax_site_export() { $this->_parent->set_greedy_script_limits(); global $wpdb, $current_site, $current_blog; $blog_id = absint($current_blog->blog_id); check_ajax_referer('export-site'); $return_args = array('completion' => 0, 'message' => '', 'errors' => false); require_once $this->_parent->_pluginPath . '/lib/zipbuddy/zipbuddy.php'; $step = absint($_POST['step']); $zip_id = sanitize_text_field($_POST['zip_id']); $upload_dir = wp_upload_dir(); $original_upload_base_dir = $upload_dir['basedir']; $extract_files_to = $original_upload_base_dir . '/' . $zip_id; switch ($step) { case 1: //Step 1 - Download a copy of WordPress $wp_url = 'http://wordpress.org/latest.zip'; $wp_file = download_url($wp_url); if (is_wp_error($wp_file)) { $return_args['errors'] = true; $return_args['message'] = $wp_file->get_error_message(); $this->log('MS-MS Step 1 - ' . $wp_file->get_error_message(), 'error'); } else { $return_args['message'] = __('WordPress Successfully Downloaded - Extracting WordPress to a Temporary Directory', 'it-l10n-backupbuddy'); $return_args['completion'] = 5; $return_args['wp_file'] = $wp_file; } break; case 2: //Step 2 - Extract WP into a separate directory $wp_file = file_exists($_POST['wp_file']) ? $_POST['wp_file'] : false; if (!$wp_file) { $return_args['errors'] = true; $return_args['message'] = __('WordPress file could not be located', 'it-l10n-backupbuddy'); $this->log('MS-MS Step 2 - ' . __('WordPress file could not be located', 'it-l10n-backupbuddy'), 'error'); } else { $return_args['debug'] = $extract_files_to; $return_args['message'] = __('WordPress extracted - Creating installation file', 'it-l10n-backupbuddy'); $return_args['completion'] = 15; $zipbuddy = new pluginbuddy_zipbuddy($extract_files_to); ob_start(); //todo - upload to wp-contennt //when merging, update extract_files to use new importbuddy version of zip / unzip functionality $zipbuddy->unzip($wp_file, $extract_files_to); $return_args['debug'] = ob_get_clean(); unlink($wp_file); } break; case 3: //Step 3 - Create new WP-Config File $wp_config_path = $extract_files_to . '/wordpress/'; if (!file_exists($wp_config_path)) { $return_args['errors'] = true; $return_args['message'] = __('Temporary WordPress installation not found', 'it-l10n-backupbuddy'); $return_args['debug'] = $wp_config_path; $this->log('MS-MS Step 3 - ' . __('Temporary WordPress installation file could not be located', 'it-l10n-backupbuddy') . $wp_config_path, 'error'); } else { $to_file = "<?php\n"; $to_file .= sprintf("define( 'DB_NAME', '%s' );\n", ''); $to_file .= sprintf("define( 'DB_USER', '%s' );\n", ''); $to_file .= sprintf("define( 'DB_PASSWORD', '%s' );\n", ''); $to_file .= sprintf("define( 'DB_HOST', '%s' );\n", ''); $charset = defined('DB_CHARSET') ? DB_CHARSET : ''; $collate = defined('DB_COLLATE') ? DB_COLLATE : ''; $to_file .= sprintf("define( 'DB_CHARSET', '%s' );\n", $charset); $to_file .= sprintf("define( 'DB_COLLATE', '%s' );\n", $collate); //Attempt to remotely retrieve salts $salts = wp_remote_get('https://api.wordpress.org/secret-key/1.1/salt/'); if (!is_wp_error($salts)) { $to_file .= wp_remote_retrieve_body($salts) . "\n"; } $to_file .= sprintf("define( 'WPLANG', '%s' );\n", WPLANG); $to_file .= sprintf('$table_prefix = \'%s\';' . "\n", 'bbms' . $blog_id . '_'); $to_file .= "if ( !defined('ABSPATH') ) { \n\tdefine('ABSPATH', dirname(__FILE__) . '/'); }"; $to_file .= "/** Sets up WordPress vars and included files. */\n\n\t\t\t\t\t\trequire_once(ABSPATH . 'wp-settings.php');"; $to_file .= "\n?>"; $wp_config_path .= 'wp-config.php'; //Create the file, save, and close $file_handle = fopen($wp_config_path, 'w'); fwrite($file_handle, $to_file); fclose($file_handle); //Prepare the response $return_args['debug'] = $extract_files_to; $return_args['message'] = __('Installation file created - Copying Over Plugins.', 'it-l10n-backupbuddy'); $return_args['completion'] = 25; } break; case 4: //Step 4 - Copy over plugins //Move over plugins $plugin_items = get_transient($zip_id); //Populate $items_to_copy for all plugins to copy over if (is_array($plugin_items)) { $items_to_copy = array(); //Get content directories by using this plugin as a base $content_dir = $dropin_plugins_dir = dirname(dirname(dirname(rtrim(plugin_dir_path(__FILE__), '/')))); $mu_plugins_dir = $content_dir . '/mu-plugins'; $plugins_dir = $content_dir . '/plugins'; //Get the special plugins (mu, dropins, network activated) foreach ($plugin_items as $type => $plugins) { foreach ($plugins as $plugin) { if ($type == 'mu') { $items_to_copy[$plugin] = $mu_plugins_dir . '/' . $plugin; } elseif ($type == 'dropin') { $items_to_copy[$plugin] = $dropin_plugins_dir . '/' . $plugin; } elseif ($type == 'network' || $type == 'site') { //Determine if we're a folder-based plugin, or a file-based plugin (such as hello.php) $plugin_path = dirname($plugins_dir . '/' . $plugin); if (basename($plugin_path) == 'plugins') { $plugin_path = $plugins_dir . '/' . $plugin; } $items_to_copy[basename($plugin_path)] = $plugin_path; } } //end foreach $plugins } //end foreach special plugins //Copy the files over $wp_dir = ''; if (count($items_to_copy) > 0) { $wp_dir = $extract_files_to . '/wordpress/'; $wp_plugin_dir = $wp_dir . '/wp-content/plugins/'; foreach ($items_to_copy as $file => $original_destination) { if (file_exists($original_destination) && file_exists($wp_plugin_dir)) { $this->copy($original_destination, $wp_plugin_dir . $file); } } } //Prepare the response $return_args['debug'] = $wp_dir; $return_args['message'] = __('Plugins copied over. Now copying over the active theme.', 'it-l10n-backupbuddy'); $return_args['completion'] = 50; } else { //Nothing has technically failed at this point - There just aren't any plugins to copy over $return_args['message'] = __('Plugins copied over. Now copying over the active theme.', 'it-l10n-backupbuddy'); $return_args['completion'] = 50; $this->log('MS-MS Step 4 - ' . __('No plugins to copy over', 'it-l10n-backupbuddy'), 'error'); } break; case 5: //Step 5 - Copy over themes $current_theme = current_theme_info(); $template_dir = $current_theme->template_dir; $stylesheet_dir = $current_theme->stylesheet_dir; //If $template_dir and $stylesheet_dir don't match, that means we have a child theme and need to copy over the parent also $items_to_copy = array(); $items_to_copy[basename($template_dir)] = $template_dir; if ($template_dir != $stylesheet_dir) { $items_to_copy[basename($stylesheet_dir)] = $stylesheet_dir; } //Copy the files over if (count($items_to_copy) > 0) { $wp_dir = $extract_files_to . '/wordpress/'; $wp_theme_dir = $wp_dir . '/wp-content/themes/'; foreach ($items_to_copy as $file => $original_destination) { if (file_exists($original_destination) && file_exists($wp_theme_dir)) { $this->copy($original_destination, $wp_theme_dir . $file); } } } $return_args['debug'] = $wp_dir; $return_args['message'] = __('Theme has been copied over. Now copying over media files.', 'it-l10n-backupbuddy'); $return_args['completion'] = 60; break; case 6: //Step 6 - Copy over media/upload files $upload_dir = wp_upload_dir(); $original_upload_base_dir = $upload_dir['basedir']; $destination_upload_base_dir = $extract_files_to . '/wordpress/wp-content/uploads'; $this->copy($original_upload_base_dir, $destination_upload_base_dir, array('ignore_files' => array($zip_id))); $return_args['debug'] = $destination_upload_base_dir; $return_args['message'] = __('Media has been copied over. Now preparing the export.', 'it-l10n-backupbuddy'); $return_args['completion'] = 70; break; case 7: //Step 7 - Create Users Table //Get users of current site global $wpdb, $current_blog; $user_args = array('blog_id' => $current_blog->blog_id); $users = get_users($user_args); //Copy over the user and usermeta tables $found_tables = array($wpdb->users, $wpdb->usermeta); $user_tablename = $usermeta_tablename = ''; $sql_to_execute = array(); if ($found_tables) { foreach ($found_tables as $index => $tablename) { $new_table = ''; if (strstr($tablename, 'users')) { $new_table = $user_tablename = $wpdb->prefix . 'users'; } if (strstr($tablename, 'usermeta')) { $new_table = $usermeta_tablename = $wpdb->prefix . 'usermeta'; } $sql_to_execute[] = sprintf('CREATE TABLE %1$s LIKE %2$s', $new_table, $tablename); $sql_to_execute[] = sprintf('INSERT %1$s SELECT * FROM %2$s', $new_table, $tablename); //Couldn't use $wpdb->prepare here because sql doesn't like quotes around the tablenames } } //Tables have been created, now execute a query to remove the users and user data that doesn't matter $users_to_capture = array(); if ($users) { foreach ($users as $user) { array_push($users_to_capture, $user->ID); } } $users_to_capture = implode(',', $users_to_capture); $sql_to_execute[] = sprintf("DELETE from %s WHERE ID NOT IN( %s )", $user_tablename, $users_to_capture); $sql_to_execute[] = sprintf("DELETE from %s WHERE user_id NOT IN( %s )", $usermeta_tablename, $users_to_capture); //Execute queries foreach ($sql_to_execute as $sql) { $wpdb->query($sql); } //Return the response $return_args['message'] = __('Building the export file and cleaning up.', 'it-l10n-backupbuddy'); $return_args['completion'] = 80; break; case 8: //Step 8 - Backup global $current_site, $wpdb; require_once $this->_parent->_pluginPath . '/classes/backup.php'; $backup_directory = $extract_files_to . '/wordpress/'; $temp_directory = $backup_directory . '/wp-content/uploads/backupbuddy_temp/'; $prefix = $wpdb->prefix; $pluginbuddy_backup = new pluginbuddy_backupbuddy_backup($this->_parent); //Get a list of tables to backup $query = "SHOW TABLES LIKE '{$prefix}%'"; $results = $wpdb->get_results($query, ARRAY_A); $tables_to_ignore = array($prefix . 'blogs', $prefix . 'blog_versions', $prefix . 'site', $prefix . 'sitemeta', $prefix . 'registration_log', $prefix . 'signups', $prefix . 'sitecategories'); $list_of_tables = array(); foreach ($results as $results_key => $table_array) { foreach ($table_array as $key => $tablename) { if (preg_match("/^{$prefix}(?!\\d+)/", $tablename) && !in_array($tablename, $tables_to_ignore)) { array_push($list_of_tables, $tablename); } } } //Do the database dump $backup_serial = $pluginbuddy_backup->rand_string(10); $dat_directory = $temp_directory . '/' . $backup_serial . '/'; $backup = array('backup_time' => time(), 'serial' => $backup_serial, 'backup_type' => 'full', 'temp_directory' => $dat_directory, 'backup_mode' => '2'); $options = array('include_tables' => array(), 'temp_directory' => $dat_directory, 'high_security' => '1', 'backup_nonwp_tables' => '0'); //Create the temp directory $this->_parent->mkdir_recursive($dat_directory); $pluginbuddy_backup->anti_directory_browsing($temp_directory); //Create the dat file $pluginbuddy_backup->backup_create_dat_file($backup, $options, true); //Create the database dump $pluginbuddy_backup->backup_create_database_dump($backup, $options, $list_of_tables, true); //Archive the file require_once $this->_parent->_pluginPath . '/lib/zipbuddy/zipbuddy.php'; $zipbuddy = new pluginbuddy_zipbuddy($backup_directory); $archive_directory = $this->_parent->_options['backup_directory']; $this->_parent->mkdir_recursive($archive_directory); $pluginbuddy_backup->anti_directory_browsing($archive_directory); $archive_file = $archive_directory . 'backup-' . $this->_parent->backup_prefix() . '-' . str_replace('-', '_', date('Y-m-d')) . '-' . $backup_serial . '.zip'; $zipbuddy->add_directory_to_zip($archive_file, $backup_directory, true); //Return the response $archive_file_url = str_replace(ABSPATH, '', $archive_file); $archive_file_url = site_url($archive_file_url); $file_args = sprintf('<a href="%s">%s</a>', $archive_file_url, __('Download Now', 'it-l10n-backupbuddy')); $return_args['message'] = sprintf(__('You\'re done! The export file is ready for download. %s', 'it-l10n-backupbuddy'), $file_args); $return_args['completion'] = 100; //Cleanup $this->_parent->delete_directory_recursive($extract_files_to); break; } //end switch die(json_encode($return_args)); }