/** * {@inheritdoc} */ protected function executeAction(MWP_IncrementalBackup_Database_DumperInterface $dumper, array $tables = array(), array $params = array()) { $path = isset($params['path']) ? $params['path'] : $this->baseDir; $this->createDirectory($path); $result = array(); foreach ($tables as $entry) { $table = isset($entry['name']) ? $entry['name'] : null; $filename = isset($entry['filename']) ? $entry['filename'] : $this->generateFilename($table); $pathname = $path . '/' . $filename; $realpath = ABSPATH . $pathname; try { if ($table === null) { throw new RuntimeException('Table name not passed.'); } $this->assertTablesExist(array($table)); $dumper->dump($table, $realpath); $result[] = array('table' => $table, 'pathname' => $pathname, 'realpath' => $realpath, 'url' => site_url() . '/' . $pathname); } catch (Exception $e) { mwp_logger()->error('Failed dumping table', array('table' => $table, 'filename' => $filename, 'pathname' => $pathname, 'error' => $e->getMessage())); $this->cleanDumpedTables($result); if ($e instanceof MWP_Worker_Exception) { throw $e; } throw new MWP_Worker_Exception(MWP_Worker_Exception::BACKUP_DATABASE_FAILED, $e->getMessage(), array('message' => $e->getMessage(), 'line' => $e->getLine(), 'file' => $e->getFile())); } } return $this->createResult(array('tables' => $result)); }
/** * {@inheritdoc} */ public function createStream(array $tables = array()) { if (!mwp_is_shell_available()) { throw new MWP_Worker_Exception(MWP_Worker_Exception::SHELL_NOT_AVAILABLE, 'Shell not available.'); } $mysqldump = mwp_container()->getExecutableFinder()->find('mysqldump', 'mysqldump'); $processBuilder = $this->createProcessBuilder($tables, $mysqldump); $process = $processBuilder->getProcess(); mwp_logger()->info('Database dumping process starting', array('executable_location' => $mysqldump, 'command_line' => $process->getCommandLine())); return new MWP_Stream_ProcessOutput($process); }
/** * Run md5sum process and return file hash, or null in case of error * * @param $realPath * * @return string|null */ public function computeUnixMd5Sum($realPath) { try { $processBuilder = Symfony_Process_ProcessBuilder::create()->setPrefix('md5sum')->add($realPath); if (!mwp_is_shell_available()) { throw new MMB_Exception("Shell is not available"); } $process = $processBuilder->getProcess(); $process->run(); if (!$process->isSuccessful()) { throw new Symfony_Process_Exception_ProcessFailedException($process); } // Output is in the format of "md5hash filename" $output = trim($process->getOutput()); $parts = explode(' ', $output); // Return only the first part of the output return trim($parts[0]); } catch (Symfony_Process_Exception_ProcessFailedException $e) { mwp_logger()->error('MD5 command line sum failed', array('process' => $e->getProcess())); } catch (Exception $e) { mwp_logger()->error('MD5 command line sum failed', array('exception' => $e)); } return null; }
function mwp_uninstall() { delete_option('mwp_recovering'); $loaderName = '0-worker.php'; try { $mustUsePluginDir = rtrim(WPMU_PLUGIN_DIR, '/'); $loaderPath = $mustUsePluginDir . '/' . $loaderName; if (!file_exists($loaderPath)) { return; } $removed = @unlink($loaderPath); if (!$removed) { $error = error_get_last(); throw new Exception(sprintf('Unable to remove loader: %s', $error['message'])); } } catch (Exception $e) { mwp_logger()->error('Unable to remove loader', array('exception' => $e)); } }
public static function selfUpdate() { if (get_option('mwp_recovering')) { return false; } try { $response = self::requestJson('http://s3-us-west-2.amazonaws.com/mwp-orion-public/worker/latest.json'); $response += array('version' => '0.0.0', 'schedule' => 86400, 'autoUpdate' => false, 'checksum' => array()); wp_clear_scheduled_hook('mwp_auto_update'); wp_schedule_single_event(current_time('timestamp') + $response['schedule'], 'mwp_auto_update'); if (!$response['autoUpdate']) { return false; } if (version_compare($response['version'], $GLOBALS['MMB_WORKER_VERSION'], '<')) { return false; } self::recoverFiles(dirname(__FILE__), $response['checksum'], $response['version']); } catch (Exception $e) { mwp_logger()->error("Self-update failed.", array('exception' => $e)); return false; } return true; }
/** * Downloads backup file from Google Drive to root folder on local server. * * @param array $args arguments passed to the function * [google_drive_token] -> user's Google drive token in json form * [google_drive_directory] -> folder on user's Google Drive account which backup file should be downloaded from * [google_drive_site_folder] -> subfolder with site name in google_drive_directory which backup file should be downloaded from * [backup_file] -> absolute path of backup file on local server * [file_id] -> google file id * * @return bool|array absolute path to downloaded file is successful, array with error message if not */ public function get_google_drive_backup($args) { mwp_register_autoload_google(); $googleClient = new Google_ApiClient(); $googleClient->setAccessToken($args['google_drive_token']); $driveService = new Google_Service_Drive($googleClient); mwp_logger()->info('Connecting to Google Drive'); try { $about = $driveService->about->get(); $rootFolderId = $about->getRootFolderId(); } catch (Exception $e) { mwp_logger()->error('Error while connecting to Google Drive', array('exception' => $e)); return array('error' => 'Error while connecting to Google Drive: ' . $e->getMessage()); } if (empty($args['file_id'])) { mwp_logger()->info('Looking for backup directory'); try { $backupFolderFiles = $driveService->files->listFiles(array('q' => sprintf("title='%s' and '%s' in parents and trashed = false", addslashes($args['google_drive_directory']), $rootFolderId))); } catch (Exception $e) { mwp_logger()->error('Error while looking for backup directory', array('exception' => $e)); return array('error' => 'Error while looking for backup directory: ' . $e->getMessage()); } if (!$backupFolderFiles->offsetExists(0)) { mwp_logger()->error('Backup directory ("{directory}") does not exist', array('directory' => $args['google_drive_directory'])); return array('error' => sprintf("The backup directory (%s) does not exist.", $args['google_drive_directory'])); } /** @var Google_Service_Drive_DriveFile $backupFolder */ $backupFolder = $backupFolderFiles->offsetGet(0); if ($args['google_drive_site_folder']) { mwp_logger()->info('Looking into the site folder'); try { $siteFolderFiles = $driveService->files->listFiles(array('q' => sprintf("title='%s' and '%s' in parents and trashed = false", addslashes($this->site_name), $backupFolder->getId()))); } catch (Exception $e) { mwp_logger()->error('Error while looking for the site folder', array('exception' => $e)); return array('error' => 'Error while looking for the site folder: ' . $e->getMessage()); } if ($siteFolderFiles->offsetExists(0)) { $backupFolder = $siteFolderFiles->offsetGet(0); } } try { $backupFiles = $driveService->files->listFiles(array('q' => sprintf("title='%s' and '%s' in parents and trashed = false", addslashes($args['backup_file']), $backupFolder->getId()))); } catch (Exception $e) { mwp_logger()->error('Error while fetching Google Drive backup file', array('file_name' => $args['backup_file'], 'exception' => $e)); return array('error' => 'Error while fetching Google Drive backup file: ' . $e->getMessage()); } if (!$backupFiles->offsetExists(0)) { return array('error' => sprintf('Backup file "%s" was not found on your Google Drive account.', $args['backup_file'])); } /** @var Google_Service_Drive_DriveFile $backupFile */ $backupFile = $backupFiles->offsetGet(0); } else { try { /** @var Google_Service_Drive_DriveFile $backupFile */ $backupFile = $driveService->files->get($args['file_id']); } catch (Exception $e) { mwp_logger()->error('Error while fetching Google Drive backup file by id', array('file_id' => $args['file_id'], 'exception' => $e)); return array('error' => 'Error while fetching Google Drive backup file: ' . $e->getMessage()); } } $downloadUrl = $backupFile->getDownloadUrl(); $downloadLocation = ABSPATH . 'mwp_temp_backup.zip'; $fileSize = $backupFile->getFileSize(); $downloaded = 0; $chunkSize = 1024 * 1024 * 4; $fh = fopen($downloadLocation, 'w+'); if (!is_resource($fh)) { return array('error' => 'Temporary backup download location is not writable (location: "%s").', $downloadLocation); } while ($downloaded < $fileSize) { $request = new Google_Http_Request($downloadUrl); $googleClient->getAuth()->sign($request); $toDownload = min($chunkSize, $fileSize - $downloaded); mwp_logger()->info('Downloading: {downloaded}/{size}', array('downloaded' => mwp_format_bytes($downloaded), 'size' => mwp_format_bytes($fileSize))); $request->setRequestHeaders($request->getRequestHeaders() + array('Range' => 'bytes=' . $downloaded . '-' . ($downloaded + $toDownload - 1))); $googleClient->getIo()->makeRequest($request); if ($request->getResponseHttpCode() !== 206) { mwp_logger()->error('Google Drive has returned an invalid response', array('response_headers' => $request->getResponseHeaders(), 'response_body' => $request->getResponseBody())); return array('error' => sprintf('Google Drive service has returned an invalid response code (%s)', $request->getResponseHttpCode())); } fwrite($fh, $request->getResponseBody()); $downloaded += $toDownload; } fclose($fh); $fileMd5 = md5_file($downloadLocation); if ($backupFile->getMd5Checksum() !== $fileMd5) { mwp_logger()->error('File checksum does not match, downloaded file is corrupted.', array('original' => $backupFile->getMd5Checksum(), 'downloaded' => $fileMd5)); return array('error' => 'File downloaded was corrupted.'); } return $downloadLocation; }
/** * Plugin install callback function * Check PHP version */ public function install() { delete_option('mwp_recovering'); mwp_container()->getMigration()->migrate(); try { $this->registerMustUse('0-worker.php', $this->buildLoaderContent('worker/init.php')); } catch (Exception $e) { mwp_logger()->error('Unable to write ManageWP loader', array('exception' => $e)); } /** @var wpdb $wpdb */ global $wpdb, $_wp_using_ext_object_cache; $_wp_using_ext_object_cache = false; //delete plugin options, just in case if ($this->mmb_multisite != false) { $network_blogs = $wpdb->get_results("select `blog_id`, `site_id` from `{$wpdb->blogs}`"); if (!empty($network_blogs)) { if (is_network_admin()) { update_option('mmb_network_admin_install', 1); foreach ($network_blogs as $details) { if ($details->site_id == $details->blog_id) { update_blog_option($details->blog_id, 'mmb_network_admin_install', 1); } else { update_blog_option($details->blog_id, 'mmb_network_admin_install', -1); } delete_blog_option($details->blog_id, '_worker_nossl_key'); delete_blog_option($details->blog_id, '_worker_public_key'); delete_blog_option($details->blog_id, '_action_message_id'); } } else { update_option('mmb_network_admin_install', -1); delete_option('_worker_nossl_key'); delete_option('_worker_public_key'); delete_option('_action_message_id'); } } } else { delete_option('_worker_nossl_key'); delete_option('_worker_public_key'); delete_option('_action_message_id'); } delete_option('mwp_notifications'); delete_option('mwp_worker_brand'); delete_option('mwp_pageview_alerts'); delete_option('mwp_worker_configuration'); $path = realpath(dirname(__FILE__) . "/../../worker.json"); if (file_exists($path)) { $configuration = file_get_contents($path); $jsonConfiguration = json_decode($configuration, true); if ($jsonConfiguration !== null) { update_option("mwp_worker_configuration", $jsonConfiguration); } } update_option('mmb_worker_activation_time', time()); }