Пример #1
0
// MISC SETUP:
// Set up default error email notification email address if none is set.
if (pb_backupbuddy::$options['email_notify_error'] == '') {
    pb_backupbuddy::$options['email_notify_error'] = get_option('admin_email');
    pb_backupbuddy::save();
}
// Migrate a previous zip Force Compatibility option setting to the new Zip Method Strategy option setting
// Leave Force Compatibility option alone for now in case site is downgraded
// If the zip method strategy is not already set non-zero then set to Force Compatibility if that option
// is already set otherwise set to Best Only
if ('0' === pb_backupbuddy::$options['zip_method_strategy']) {
    if (isset(pb_backupbuddy::$options['force_compatibility']) && '1' === pb_backupbuddy::$options['force_compatibility']) {
        pb_backupbuddy::$options['zip_method_strategy'] = '3';
    } else {
        pb_backupbuddy::$options['zip_method_strategy'] = '1';
    }
    pb_backupbuddy::save();
}
backupbuddy_core::verifyHousekeeping();
// Verify existance of default S3 config (currently blank to fix shell_exec() warning issue. Added 3.1.8.3 Jan 29, 2013 - Dustin.
$s3_config = pb_backupbuddy::plugin_path() . '/destinations/_s3lib/aws-sdk/config.inc.php';
if (!@file_exists($s3_config)) {
    if (true === @touch($s3_config)) {
        // Be silent as to not risk breaking activation as this is minor. Just in case of logging issues.
        //pb_backupbuddy::status( 'details', 'Created default blank destination config `' . $s3_config . '`.' );
    } else {
        // Be silent as to not risk breaking activation as this is minor. Just in case of logging issues.
        //pb_backupbuddy::status( 'error', 'Unable to create default blank destination config `' . $s3_config . '`. Check permissions.' );
    }
}
unset($s3_config);
Пример #2
0
 function pre_backup($serial, $archiveFile, $profile, $trigger, $pre_backup = array(), $post_backup = array(), $schedule_title = '', $export_plugins = array(), $deployDirection, $deployDestinationSettings)
 {
     pb_backupbuddy::status('startFunction', json_encode(array('function' => 'pre_backup', 'title' => 'Getting ready to backup')));
     $type = $profile['type'];
     // Log some status information.
     pb_backupbuddy::status('details', __('Performing pre-backup procedures.', 'it-l10n-backupbuddy'));
     if ($type == 'full') {
         pb_backupbuddy::status('message', __('Full backup mode.', 'it-l10n-backupbuddy'));
     } elseif ($type == 'db') {
         pb_backupbuddy::status('message', __('Database only backup mode.', 'it-l10n-backupbuddy'));
     } elseif ($type == 'files') {
         pb_backupbuddy::status('message', __('Files only backup mode.', 'it-l10n-backupbuddy'));
         //$profile['skip_database_dump'] = '1';
     } elseif ($type == 'export') {
         pb_backupbuddy::status('message', __('Multisite subsite export mode.', 'it-l10n-backupbuddy'));
     } else {
         pb_backupbuddy::status('error', 'Error #8587383: Unknown backup mode `' . htmlentities($type) . '`.');
     }
     if ('' != $deployDirection) {
         pb_backupbuddy::status('details', 'Deployment direction: `' . $deployDirection . '`.');
     }
     if ('1' == pb_backupbuddy::$options['prevent_flush']) {
         pb_backupbuddy::status('details', 'Flushing will be skipped based on advanced settings.');
     } else {
         pb_backupbuddy::status('details', 'Flushing will not be skipped (default).');
     }
     // Schedule daily housekeeping.
     backupbuddy_core::verifyHousekeeping();
     // Verify directories.
     pb_backupbuddy::status('details', 'Verifying directories ...');
     if (false === backupbuddy_core::verify_directories()) {
         pb_backupbuddy::status('error', 'Error #18573. Error verifying directories. See details above. Backup halted.');
         pb_backupbuddy::status('haltScript', '');
         // Halt JS on page.
         die;
     } else {
         pb_backupbuddy::status('details', 'Directories verified.');
     }
     // Delete all backup archives if this troubleshooting option is enabled.
     if (pb_backupbuddy::$options['delete_archives_pre_backup'] == '1') {
         pb_backupbuddy::status('message', 'Deleting all existing backups prior to backup as configured on the settings page.');
         $file_list = glob(backupbuddy_core::getBackupDirectory() . 'backup*.zip');
         if (is_array($file_list) && !empty($file_list)) {
             foreach ($file_list as $file) {
                 if (@unlink($file) === true) {
                     pb_backupbuddy::status('details', 'Deleted backup archive `' . basename($file) . '` based on settings to delete all backups.');
                 } else {
                     pb_backupbuddy::status('details', 'Unable to delete backup archive `' . basename($file) . '` based on settings to delete all backups. Verify permissions.');
                 }
             }
         }
     }
     // Generate unique serial ID.
     pb_backupbuddy::status('details', 'Backup serial generated: `' . $serial . '`.');
     pb_backupbuddy::status('details', 'About to load fileoptions data in create mode.');
     require_once pb_backupbuddy::plugin_path() . '/classes/fileoptions.php';
     pb_backupbuddy::status('details', 'Fileoptions instance #40.');
     $this->_backup_options = new pb_backupbuddy_fileoptions(backupbuddy_core::getLogDirectory() . 'fileoptions/' . $serial . '.txt', $read_only = false, $ignore_lock = false, $create_file = true);
     if (true !== ($result = $this->_backup_options->is_ok())) {
         pb_backupbuddy::status('error', __('Fatal Error #9034 A. Unable to access fileoptions data.', 'it-l10n-backupbuddy') . ' Error: ' . $result);
         pb_backupbuddy::status('haltScript', '');
         // Halt JS on page.
         return false;
     }
     pb_backupbuddy::status('details', 'Fileoptions data loaded.');
     $this->_backup =& $this->_backup_options->options;
     // Set reference.
     // Cleanup internal stats. Deployments should not impact stats.
     if ('' == $deployDirection) {
         pb_backupbuddy::status('details', 'Updating statistics for last backup start.');
         pb_backupbuddy::$options['last_backup_start'] = microtime(true);
         // Reset time since last backup.
         pb_backupbuddy::$options['last_backup_serial'] = $serial;
         pb_backupbuddy::save();
     }
     // Output active plugins list for debugging...
     $activePlugins = get_option('active_plugins');
     pb_backupbuddy::status('details', 'Active WordPress plugins (' . count($activePlugins) . '): `' . implode('; ', $activePlugins) . '`.');
     pb_backupbuddy::status('startSubFunction', json_encode(array('function' => 'wp_plugins_found', 'title' => 'Found ' . count($activePlugins) . ' active WordPress plugins.')));
     unset($activePlugins);
     // Compression to bool.
     /*
     if ( $profile['compression'] == '1' ) {
     	$profile['compression'] = true;
     } else {
     	$profile['compression'] = false;
     }
     */
     if (pb_backupbuddy::$options['compression'] == '1') {
         $compression = true;
     } else {
         $compression = false;
     }
     $archiveURL = '';
     $abspath = str_replace('\\', '/', ABSPATH);
     // Change slashes to handle Windows as we store backup_directory with Linux-style slashes even on Windows.
     $backup_dir = str_replace('\\', '/', backupbuddy_core::getBackupDirectory());
     if (FALSE !== stristr($backup_dir, $abspath)) {
         // Make sure file to download is in a publicly accessible location (beneath WP web root technically).
         $sitepath = str_replace($abspath, '', $backup_dir);
         $archiveURL = rtrim(site_url(), '/\\') . '/' . trim($sitepath, '/\\') . '/' . basename($archiveFile);
     }
     $forceSingleDatabaseFile = false;
     if ('1' == pb_backupbuddy::$options['force_single_db_file']) {
         $forceSingleDatabaseFile = true;
     }
     // Set up the backup data.
     $this->_backup = array('data_version' => 1, 'backupbuddy_version' => pb_backupbuddy::settings('version'), 'serial' => $serial, 'init_complete' => false, 'backup_mode' => $profile['backup_mode'], 'type' => $type, 'profile' => $profile, 'default_profile' => pb_backupbuddy::$options['profiles'][0], 'start_time' => time(), 'finish_time' => 0, 'updated_time' => time(), 'status' => array(), 'max_execution_time' => backupbuddy_core::adjustedMaxExecutionTime(), 'archive_size' => 0, 'schedule_title' => $schedule_title, 'backup_directory' => backupbuddy_core::getBackupDirectory(), 'archive_file' => $archiveFile, 'archive_url' => $archiveURL, 'trigger' => $trigger, 'zip_method_strategy' => pb_backupbuddy::$options['zip_method_strategy'], 'compression' => $compression, 'ignore_zip_warnings' => pb_backupbuddy::$options['ignore_zip_warnings'], 'ignore_zip_symlinks' => pb_backupbuddy::$options['ignore_zip_symlinks'], 'steps' => array(), 'integrity' => array(), 'temp_directory' => '', 'backup_root' => '', 'export_plugins' => array(), 'additional_table_includes' => array(), 'additional_table_excludes' => array(), 'directory_exclusions' => backupbuddy_core::get_directory_exclusions($profile, false, $serial), 'table_sizes' => array(), 'breakout_tables' => array(), 'force_single_db_file' => $forceSingleDatabaseFile, 'deployment_log' => '', 'deployment_direction' => $deployDirection, 'deployment_destination' => $deployDestinationSettings, 'runnerUID' => get_current_user_id());
     pb_backupbuddy::status('startSubFunction', json_encode(array('function' => 'file_excludes', 'title' => 'Found ' . count($this->_backup['directory_exclusions']) . ' file or directory exclusions.')));
     // Warn if excluding key paths.
     $alertFileExcludes = backupbuddy_core::alert_core_file_excludes($this->_backup['directory_exclusions']);
     foreach ($alertFileExcludes as $alertFileExcludeId => $alertFileExclude) {
         pb_backupbuddy::status('warning', $alertFileExclude);
     }
     pb_backupbuddy::anti_directory_browsing(backupbuddy_core::getTempDirectory(), $die = false);
     // Figure out paths.
     if ($this->_backup['type'] == 'full' || $this->_backup['type'] == 'files') {
         $this->_backup['temp_directory'] = backupbuddy_core::getTempDirectory() . $serial . '/';
         $this->_backup['backup_root'] = ABSPATH;
         // ABSPATH contains trailing slash.
     } elseif ($this->_backup['type'] == 'db') {
         $this->_backup['temp_directory'] = backupbuddy_core::getTempDirectory() . $serial . '/';
         $this->_backup['backup_root'] = $this->_backup['temp_directory'];
     } elseif ($this->_backup['type'] == 'export') {
         // WordPress unzips into wordpress subdirectory by default so must include that in path.
         $this->_backup['temp_directory'] = backupbuddy_core::getTempDirectory() . $serial . '/wordpress/wp-content/uploads/backupbuddy_temp/' . $serial . '/';
         // We store temp data for export within the temporary WordPress installation within the temp directory. A bit confusing; sorry about that.
         $this->_backup['backup_root'] = backupbuddy_core::getTempDirectory() . $serial . '/wordpress/';
     } else {
         pb_backupbuddy::status('error', __('Backup FAILED. Unknown backup type.', 'it-l10n-backupbuddy'));
         pb_backupbuddy::status('haltScript', '');
         // Halt JS on page.
     }
     pb_backupbuddy::status('details', 'Temp directory: `' . $this->_backup['temp_directory'] . '`.');
     pb_backupbuddy::status('details', 'Backup root: `' . $this->_backup['backup_root'] . '`.');
     // Plugins to export (only for MS exports).
     if (count($export_plugins) > 0) {
         $this->_backup['export_plugins'] = $export_plugins;
     }
     // Calculate additional database table inclusion/exclusion.
     $this->_backup['additional_table_includes'] = backupbuddy_core::get_mysqldump_additional('includes', $profile);
     $this->_backup['additional_table_excludes'] = backupbuddy_core::get_mysqldump_additional('excludes', $profile);
     /********* Begin setting up steps array. *********/
     if ($type == 'export') {
         pb_backupbuddy::status('details', 'Setting up export-specific steps.');
         $this->_backup['steps'][] = array('function' => 'ms_download_extract_wordpress', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         $this->_backup['steps'][] = array('function' => 'ms_create_wp_config', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         $this->_backup['steps'][] = array('function' => 'ms_copy_plugins', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         $this->_backup['steps'][] = array('function' => 'ms_copy_themes', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         $this->_backup['steps'][] = array('function' => 'ms_copy_media', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         $this->_backup['steps'][] = array('function' => 'ms_copy_users_table', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
     }
     if ('pull' != $deployDirection && '1' != $profile['skip_database_dump'] && $profile['type'] != 'files') {
         // Backup database if not skipping AND not a files only backup.
         global $wpdb;
         // Default tables to backup.
         if ($type == 'export') {
             // Multisite Subsite export only dumps tables specific to this subsite prefix.
             $base_dump_mode = 'prefix';
         } else {
             // Non-multisite export so use profile to determine tables to backup.
             if ($profile['backup_nonwp_tables'] == '1') {
                 // Backup all tables.
                 $base_dump_mode = 'all';
             } elseif ($profile['backup_nonwp_tables'] == '2') {
                 // Backup no tables by default. Relies on listed additional tables.
                 $base_dump_mode = 'none';
             } else {
                 // Only backup matching prefix.
                 $base_dump_mode = 'prefix';
             }
         }
         $additional_tables = $this->_backup['additional_table_includes'];
         if ($type == 'export') {
             global $wpdb;
             array_push($additional_tables, $wpdb->prefix . "users");
             array_push($additional_tables, $wpdb->prefix . "usermeta");
         }
         // Warn if excluding key WP tables.
         $tableExcludes = backupbuddy_core::alert_core_table_excludes($this->_backup['additional_table_excludes']);
         foreach ($tableExcludes as $tableExcludeId => $tableExclude) {
             pb_backupbuddy::status('warning', $tableExclude);
         }
         // Calculate tables to dump based on the provided information. $tables will be an array of tables.
         $tables = $this->_calculate_tables($base_dump_mode, $additional_tables, $this->_backup['additional_table_excludes']);
         pb_backupbuddy::status('startSubFunction', json_encode(array('function' => 'calculate_tables', 'title' => 'Found ' . count($tables) . ' tables to backup based on settings.', 'more' => 'Tables: ' . implode(', ', $tables))));
         // If calculations show NO database tables should be backed up then change mode to skip database dump.
         if (0 == count($tables)) {
             pb_backupbuddy::status('warning', 'WARNING #857272: No database tables will be backed up based on current settings. This will not be a complete backup. Adjust settings if this is not intended and use with caution. Skipping database dump step.');
             $profile['skip_database_dump'] = '1';
             $this->_backup['profile']['skip_database_dump'] = '1';
         } else {
             // One or more tables set to backup.
             // Obtain tables sizes. Surround each table name by a single quote and implode with commas for SQL query to get sizes.
             $tables_formatted = $tables;
             foreach ($tables_formatted as &$table_formatted) {
                 $table_formatted = "'{$table_formatted}'";
             }
             $tables_formatted = implode(',', $tables_formatted);
             $sql = "SHOW TABLE STATUS WHERE Name IN({$tables_formatted});";
             $rows = $wpdb->get_results($sql, ARRAY_A);
             if (false === $rows) {
                 pb_backupbuddy::alert('Error #85473474: Unable to retrieve table status. Query: `' . $sql . '`.', true);
                 return false;
             }
             $totalDatabaseSize = 0;
             foreach ($rows as $row) {
                 $this->_backup['table_sizes'][$row['Name']] = $row['Data_length'] + $row['Index_length'];
                 $totalDatabaseSize += $this->_backup['table_sizes'][$row['Name']];
             }
             unset($rows);
             unset($tables_formatted);
             $databaseSize = pb_backupbuddy::$format->file_size($totalDatabaseSize);
             pb_backupbuddy::status('details', 'Total calculated database size: `' . $databaseSize . '`.');
             // Step through tables we want to break out and figure out which ones were indeed set to be backed up and break them out.
             if (pb_backupbuddy::$options['breakout_tables'] == '0') {
                 // Breaking out DISABLED.
                 pb_backupbuddy::status('details', 'Breaking out tables DISABLED based on settings.');
             } else {
                 // Breaking out ENABLED.
                 // Tables we will try to break out into standalone steps if possible.
                 $breakout_tables_defaults = array($wpdb->prefix . 'posts', $wpdb->prefix . 'postmeta');
                 pb_backupbuddy::status('details', 'Breaking out tables ENABLED based on settings. Tables to be broken out into individual steps: `' . implode(', ', $breakout_tables_defaults) . '`.');
                 foreach ((array) $breakout_tables_defaults as $breakout_tables_default) {
                     if (in_array($breakout_tables_default, $tables)) {
                         $this->_backup['breakout_tables'][] = $breakout_tables_default;
                         $tables = array_diff($tables, array($breakout_tables_default));
                         // Remove from main table backup list.
                     }
                 }
                 unset($breakout_tables_defaults);
                 // No longer needed.
             }
             $this->_backup['steps'][] = array('function' => 'backup_create_database_dump', 'args' => array($tables), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
             // Set up backup steps for additional broken out tables.
             foreach ((array) $this->_backup['breakout_tables'] as $breakout_table) {
                 $this->_backup['steps'][] = array('function' => 'backup_create_database_dump', 'args' => array(array($breakout_table)), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
             }
         }
         // end there being tables to backup.
     } else {
         pb_backupbuddy::status('message', __('Skipping database dump based on settings / profile type.', 'it-l10n-backupbuddy') . ' Backup type: `' . $type . '`.');
     }
     if ('pull' != $deployDirection) {
         $this->_backup['steps'][] = array('function' => 'backup_zip_files', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         if ($type == 'export') {
             $this->_backup['steps'][] = array('function' => 'ms_cleanup', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         }
         if ($profile['integrity_check'] == '1') {
             pb_backupbuddy::status('details', __('Integrity check will be performed based on settings for this profile.', 'it-l10n-backupbuddy'));
             $this->_backup['steps'][] = array('function' => 'integrity_check', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
         } else {
             pb_backupbuddy::status('details', __('Skipping integrity check step based on settings for this profile.', 'it-l10n-backupbuddy'));
         }
     }
     $this->_backup['steps'][] = array('function' => 'post_backup', 'args' => array(), 'start_time' => 0, 'finish_time' => 0, 'attempts' => 0);
     // Prepend and append pre backup and post backup steps.
     $this->_backup['steps'] = array_merge($pre_backup, $this->_backup['steps'], $post_backup);
     /********* End setting up steps array. *********/
     // Save what we have so far so that any errors below will end up displayed to user.
     $this->_backup_options->save();
     /********* Begin directory creation and security. *********/
     pb_backupbuddy::anti_directory_browsing(backupbuddy_core::getBackupDirectory());
     // Prepare temporary directory for holding SQL and data file.
     if (backupbuddy_core::getTempDirectory() == '') {
         pb_backupbuddy::status('error', 'Error #54534344. Temp directory blank. Please deactivate then reactivate plugin to reset.');
         return false;
     }
     if (!file_exists($this->_backup['temp_directory'])) {
         if (pb_backupbuddy::$filesystem->mkdir($this->_backup['temp_directory']) === false) {
             pb_backupbuddy::status('error', 'Error #9002b. Unable to create temporary storage directory (' . $this->_backup['temp_directory'] . ')');
             return false;
         }
     }
     if (!is_writable($this->_backup['temp_directory'])) {
         pb_backupbuddy::status('error', 'Error #9015. Temp data directory is not writable. Check your permissions. (' . $this->_backup['temp_directory'] . ')');
         return false;
     }
     pb_backupbuddy::anti_directory_browsing(ABSPATH . 'wp-content/uploads/backupbuddy_temp/');
     // Prepare temporary directory for holding ZIP file while it is being generated.
     $this->_backup['temporary_zip_directory'] = backupbuddy_core::getBackupDirectory() . 'temp_zip_' . $this->_backup['serial'] . '/';
     if (!file_exists($this->_backup['temporary_zip_directory'])) {
         if (pb_backupbuddy::$filesystem->mkdir($this->_backup['temporary_zip_directory']) === false) {
             pb_backupbuddy::status('details', 'Error #9002c. Unable to create temporary ZIP storage directory (' . $this->_backup['temporary_zip_directory'] . ')');
             return false;
         }
     }
     if (!is_writable($this->_backup['temporary_zip_directory'])) {
         pb_backupbuddy::status('error', 'Error #9015. Temp data directory is not writable. Check your permissions. (' . $this->_backup['temporary_zip_directory'] . ')');
         return false;
     }
     /********* End directory creation and security *********/
     // Generate backup DAT (data) file containing details about the backup.
     if ($this->backup_create_dat_file($trigger) !== true) {
         pb_backupbuddy::status('details', __('Problem creating DAT file.', 'it-l10n-backupbuddy'));
         return false;
     }
     // Generating ImportBuddy file to include in the backup for FULL BACKUPS ONLY currently. Cannot put in DB because it would be in root and be excluded or conflict on extraction.
     if ($type == 'full') {
         if (pb_backupbuddy::$options['include_importbuddy'] == '1') {
             pb_backupbuddy::status('details', 'Generating ImportBuddy tool to include in backup archive: `' . $this->_backup['temp_directory'] . 'importbuddy.php`.');
             pb_backupbuddy::status('startAction', 'importbuddyCreation');
             backupbuddy_core::importbuddy($this->_backup['temp_directory'] . 'importbuddy.php');
             pb_backupbuddy::status('finishAction', 'importbuddyCreation');
             pb_backupbuddy::status('details', 'ImportBuddy generation complete.');
         } else {
             // dont include importbuddy.
             pb_backupbuddy::status('details', 'ImportBuddy tool inclusion in ZIP backup archive skipped based on settings or backup type.');
         }
     }
     // Save all of this.
     $this->_backup['init_complete'] = true;
     // pre_backup() completed.
     $this->_backup_options->save();
     pb_backupbuddy::status('details', __('Finished pre-backup procedures.', 'it-l10n-backupbuddy'));
     pb_backupbuddy::status('milestone', 'finish_settings');
     pb_backupbuddy::status('finishFunction', json_encode(array('function' => 'pre_backup')));
     return true;
 }