public static function _send_dbqueue() { require_once pb_backupbuddy::plugin_path() . '/destinations/stash2/init.php'; $response = pb_backupbuddy_destination_stash2::stashAPI(pb_backupbuddy::$options['remote_destinations'][backupbuddy_live::getLiveID()], 'live-put', array('files' => self::$_dbqueue_rendered)); //error_log( 'Live db send response:' ); //error_log( print_r( $response, true ) ); $errors = array(); if (!is_array($response)) { // Error message. $errors[] = 'Error #3279237: Unexpected server response. Check your BackupBuddy Stash Live login and try again. Detailed response: `' . print_r($response, true) . '`.'; } else { // Errors. if (isset($response['error'])) { $errors[] = $response['error']['message']; } else { // No error? if (!isset($response['success']) || '1' != $response['success']) { $errors[] = 'Error #9327324: Something went wrong. Success was not reported. Detailed response: `' . print_r($response, true) . '`.'; } } } self::$_queue_sent = true; // Prevent potentially sending again if shutdown hook double-fires. if (count($errors) > 0) { pb_backupbuddy::status('error', 'Error sending live continuous data. Error(s): `' . implode(', ', $errors) . '`.'); //backupbuddy_core::addNotification( 'live_continuous_error', 'BackupBuddy Stash Live Errors', implode( ', ', $errors ), $errors ); } else { // Update last activity time. backupbuddy_live::update_db_live_activity_time(); if (pb_backupbuddy::$options['log_level'] == '3') { // Full logging enabled. pb_backupbuddy::status('details', 'Success sending live continuous data to server.'); } } return true; }
private static function _step_database_snapshot($tables = array(), $chunkTables = array(), $rows_start = 0) { if (false === self::_load_state()) { return false; } if (false === self::_load_tables()) { return false; } backupbuddy_live::update_db_live_activity_time(); // Databse snapshot storage directory. Includes trailing slash. $directory = backupbuddy_live::getLiveDatabaseSnapshotDir(); pb_backupbuddy::status('message', __('Starting database snapshot procedure.', 'it-l10n-backupbuddy')); if (0 == count($chunkTables)) { // First pass. // Delete any existing db snapshots stored locally. $snapshots = glob($directory . '*.sql'); pb_backupbuddy::status('details', 'Found `' . count($snapshots) . '` total existing local SQL files to delete from temporary dump directory `' . $directory . '`.'); foreach ($snapshots as $snapshot) { @unlink($snapshot); } $tables = backupbuddy_live::calculateTables(); $chunkTables = $tables; } else { // Resuming chunking. pb_backupbuddy::status('details', '`' . count($chunkTables) . '` tables left to dump.'); } pb_backupbuddy::status('details', 'Tables: `' . print_r($tables, true) . '`, chunkTables: `' . print_r($chunkTables, true) . '`, Rows_Start: `' . print_r($rows_start, true) . '`.'); if ('php' == pb_backupbuddy::$options['database_method_strategy']) { $force_methods = array('php'); } elseif ('commandline' == pb_backupbuddy::$options['database_method_strategy']) { $force_methods = array('commandline'); } elseif ('all' == pb_backupbuddy::$options['database_method_strategy']) { $force_methods = array('php', 'commandline'); } else { pb_backupbuddy::status('error', 'Error #95432: Invalid forced database dump method setting: `' . pb_backupbuddy::$options['database_method_strategy'] . '`.'); return false; } $destination_settings = self::get_destination_settings(); $maxExecution = $destination_settings['max_time']; // Load mysqlbuddy and perform dump. pb_backupbuddy::status('details', 'Loading mysqlbuddy.'); require_once pb_backupbuddy::plugin_path() . '/lib/mysqlbuddy/mysqlbuddy.php'; global $wpdb; pb_backupbuddy::$classes['mysqlbuddy'] = new pb_backupbuddy_mysqlbuddy(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD, $wpdb->prefix, $force_methods, $maxExecution); // $database_host, $database_name, $database_user, $database_pass, $old_prefix, $force_method = array() // Prepare destination snapshot sql files directory. pb_backupbuddy::status('details', 'Creating dump directory.'); if (pb_backupbuddy::$filesystem->mkdir($directory, $mode = 0755, $recurse = true) === false) { $error = 'Error #387974: BackupBuddy unable to create directory `' . $directory . '`. Please verify write permissions for the parent directory `' . dirname($directory) . '` or manually create the specified directory & set permissions.'; } // Do the database dump. $result = pb_backupbuddy::$classes['mysqlbuddy']->dump($directory, $chunkTables, $rows_start); // if array, returns tables,rowstart // Process dump result. if (is_array($result)) { // Chunking. return array('Creating database snapshot', array($tables, $result[0], $result[1])); // Full table list, remaining tables, row to resume at. } else { // Should be either true (success) or false (fail). if (true === $result) { // Success. pb_backupbuddy::status('details', 'Database dump fully completed. Calculating database stats.'); // Set last snapshot time. self::$_state['stats']['last_db_snapshot'] = microtime(true); // Timestamp snapshot completed. Used to delete live sql updates prior to this timestamp. // Get info on tables. $table_details = $wpdb->get_results("SELECT TABLE_NAME,DATA_LENGTH,INDEX_LENGTH FROM information_schema.tables WHERE table_schema = DATABASE()", ARRAY_A); $table_sizes = array(); foreach ($table_details as $table_detail) { $table_sizes[$table_detail['TABLE_NAME']] = $table_detail['DATA_LENGTH'] + $table_detail['INDEX_LENGTH']; } unset($table_details); // Add any new tables to catalog listing. $database_size = 0; foreach ($tables as $table) { // Table is not yet in the catalog. if (!isset(self::$_tables[$table])) { self::$_tables[$table] = self::$_tableDefaults; // Apply defaults. self::$_tables[$table]['a'] = self::$_state['stats']['last_db_snapshot']; self::$_tables[$table]['m'] = self::$_state['stats']['last_db_snapshot']; } else { // Table already in catalog. Update it. self::$_tables[$table] = array_merge(self::$_tableDefaults, self::$_tables[$table]); // Apply defaults to existing data. self::$_tables[$table]['m'] = self::$_state['stats']['last_db_snapshot']; } // Set size if we have calculated it (if it was available to us). if (isset($table_sizes[$table])) { self::$_tables[$table]['s'] = $table_sizes[$table]; $database_size += $table_sizes[$table]; } // Reset try attempts. self::$_tables[$table]['t'] = 0; } // end foreach. // Mark any removed tables as needing deletion in catalog listing. Handles tables that no longer exist or are excluded. if (count($table_sizes) > 0) { // If we were able to get table listings. foreach (self::$_tables as $catalogTableName => $catalogTable) { // Iterate through stored tables in catalog. if (!isset($table_sizes[$catalogTableName]) || !in_array($catalogTableName, $tables)) { // Backed up table is no longer in mysql db OR was not in list of tables to backup (eg is now excluded). // If table was already sent, mark for deletion. Else just remove entirely here. if (0 != self::$_tables[$catalogTableName]['b']) { // Already backed up to server. self::$_tables[$catalogTableName]['d'] = true; // Mark for deletion. self::$_state['stats']['tables_pending_delete']++; } else { // Remove outright here. unset(self::$_tables[$catalogTableName]); } } } } self::$_state['stats']['tables_total_size'] = $database_size; self::$_state['stats']['tables_total_count'] = count($tables); self::$_state['stats']['tables_pending_send'] = count($tables); // Save catalog. self::$_stateObj->save(); self::$_tablesObj->save(); return true; } elseif (false === $result) { $error = 'Error #8349434: Live unable to dump database. See log for details.'; pb_backupbuddy::status('error', $error); backupbuddy_core::addNotification('live_error', 'BackupBuddy Stash Live Error', $error); self::$_state['step']['last_status'] = $error; return false; } else { $error = 'Error #398349734: Live unexpected database dump response. See log for details.'; pb_backupbuddy::status('error', $error); backupbuddy_core::addNotification('live_error', 'BackupBuddy Stash Live Error', $error); self::$_state['step']['last_status'] = $error; return false; } } // end if non-chunking. }