public function addFileFromPath($filename, $path, $stateData) { $fp = fopen($path, 'rb'); $chunk = isset($stateData['chunk']) ? (int) $stateData['chunk'] : 0; $deflate = sgfun('zlib.deflate'); if ($chunk > 0) { fseek($fp, $chunk * self::CHUNK_SIZE); $zlen = new Math_BigInteger($stateData['zlen']); } else { $zlen = new Math_BigInteger(0); } $notificationCenter = SGNotificationCenter::getInstance(); //read file in small chunks while (!feof($fp)) { if ($notificationCenter->postAskNotification(SG_NOTIFICATION_SHOULD_CONTINUE_ACTION) === false) { $data = $deflate(); $this->write($data); if ($chunk > 0) { $zlen = $zlen->add(new Math_BigInteger(strlen($data))); } $this->totalWrittenBytes = $this->totalWrittenBytes->add(new Math_BigInteger(strlen($data))); fclose($fp); fclose($this->fileHandle); $this->zoffsets[] = $this->totalWrittenBytes->toString(); $notificationCenter->postDoNotification(SG_NOTIFICATION_SAVE_FILES_ACTION_STATE, array('chunk' => $chunk, 'filename' => $filename, 'path' => $path, 'cdrSize' => $this->cdrFilesCount, 'zoffsets' => $this->zoffsets, 'zlen' => $zlen->toString())); throw new SGExceptionPause(); } $data = fread($fp, self::CHUNK_SIZE); $data = $deflate($data); $this->write($data); $zlen = $zlen->add(new Math_BigInteger(strlen($data))); $this->totalWrittenBytes = $this->totalWrittenBytes->add(new Math_BigInteger(strlen($data))); $chunk++; } $data = $deflate(); $this->write($data); $zlen = $zlen->add(new Math_BigInteger(strlen($data))); $this->totalWrittenBytes = $this->totalWrittenBytes->add(new Math_BigInteger(strlen($data))); fclose($fp); $len = new Math_BigInteger(0); $this->addFileToCdr($filename, $zlen, $len); }
public function saveActionState($data) { $data['totalRowCount'] = $this->totalRowCount; $data['currentRowCount'] = $this->currentRowCount; $data['nextProgressUpdate'] = $this->nextProgressUpdate; $notificationCenter = SGNotificationCenter::getInstance(); $notificationCenter->postDoNotification(SG_NOTIFICATION_SAVE_ACTION_STATE, array('databaseBackup' => $data)); }
public function backup() { $stateData = self::loadStateData(); if ($stateData) { $this->continueBackupWithStateData($stateData); } else { $this->fileName = self::getBackupFileName(); $this->prepareBackupFolder(SG_BACKUP_DIRECTORY . $this->fileName); } $shouldContinue = true; try { if (!$stateData) { $this->prepareForBackup(); } if ($this->databaseBackupAvailable) { $this->backupDatabase->setFilePath($this->databaseBackupPath); $this->backupFiles->addDontExclude(realpath($this->databaseBackupPath)); if (!$stateData || isset($stateData['databaseBackup'])) { if (!$this->filesBackupAvailable) { SGConfig::set('SG_BACKUP_FILE_PATHS', '', false); } $this->backupDatabase->backup($this->databaseBackupPath, $stateData); $stateData = array(); $rootDirectory = realpath(SGConfig::get('SG_APP_ROOT_DIRECTORY')) . '/'; $path = substr(realpath($this->databaseBackupPath), strlen($rootDirectory)); $this->backupFiles->addDontExclude(realpath($this->databaseBackupPath)); $backupItems = SGConfig::get('SG_BACKUP_FILE_PATHS'); $allItems = $backupItems ? explode(',', $backupItems) : array(); $allItems[] = $path; SGConfig::set('SG_BACKUP_FILE_PATHS', implode(',', $allItems), false); $currentStatus = $this->getCurrentActionStatus(); if ($currentStatus == SG_ACTION_STATUS_CANCELLING || $currentStatus == SG_ACTION_STATUS_CANCELLED) { $this->cancel(); } self::changeActionStatus($this->actionId, SG_ACTION_STATUS_IN_PROGRESS_FILES); } } if (!$stateData || isset($stateData['filesBackup'])) { $this->backupFiles->backup($this->filesBackupPath, $stateData); $this->didFinishBackup(); } } catch (SGException $exception) { if ($exception instanceof SGExceptionSkip) { $this->setCurrentActionStatusCancelled(); } else { if ($exception instanceof SGExceptionPause) { return; } else { SGBackupLog::writeExceptionObject($exception); if ($this->databaseBackupAvailable) { $this->backupDatabase->cancel(); } $this->backupFiles->cancel(); if (SGBoot::isFeatureAvailable('NOTIFICATIONS')) { SGBackupMailNotification::sendBackupNotification(false); } self::changeActionStatus($this->actionId, SG_ACTION_STATUS_ERROR); } } $shouldContinue = false; } //check if should continue uploading backup to storages if ($shouldContinue) { try { $this->backupUploadToStorages(); } catch (SGException $exception) { if ($exception instanceof SGExceptionPause) { return; } SGBackupLog::writeExceptionObject($exception); } } $notificationCenter = SGNotificationCenter::getInstance(); $notificationCenter->postNotification(SG_NOTIFICATION_DID_FINISH_BACKUP); }
public function saveActionState($data) { $data['nextProgressUpdate'] = $this->nextProgressUpdate; $data['totalBackupFilesCount'] = $this->totalBackupFilesCount; $data['currentBackupFileCount'] = $this->currentBackupFileCount; $data['warningsFound'] = $this->warningsFound; $data['stateOffset'] = ftell($this->stateFileHandle); $notificationCenter = SGNotificationCenter::getInstance(); $notificationCenter->postDoNotification(SG_NOTIFICATION_SAVE_ACTION_STATE, array('filesBackup' => $data)); }
/** * Table rows extractor * * @param string $tableName Name of table to export * * @return null */ private function listValues($tableName, $data) { if (empty($data)) { $this->prepareListValues($tableName); $onlyOnce = true; $lineSize = 0; $offset = 0; $chunk = 0; } else { $onlyOnce = (int) @$data['onlyOnce']; $lineSize = (int) @$data['lineSize']; $chunk = (int) @$data['chunk']; $offset = (int) @$data['offset']; $offset += $chunk; } $colStmt = $this->getColumnStmt($tableName); $stmt = "SELECT {$colStmt} FROM `{$tableName}`"; if ($this->dumpSettings['where']) { $stmt .= " WHERE {$this->dumpSettings['where']}"; } $limit = SG_BACKUP_DATABASE_INSERT_LIMIT; $notificationCenter = SGNotificationCenter::getInstance(); while (true) { $st = $this->dbHandler->exec($stmt . ' LIMIT ' . $offset . ',' . $limit); $row = $this->dbHandler->fetch($st); if (!$row) { break; } while ($row) { if ($notificationCenter->postAskNotification(SG_NOTIFICATION_SHOULD_CONTINUE_ACTION) === false) { $notificationCenter->postDoNotification(SG_NOTIFICATION_SAVE_DB_ACTION_STATE, array('onlyOnce' => $onlyOnce, 'lineSize' => $lineSize, 'offset' => $offset, 'chunk' => $chunk, 'excludeTables' => $this->excludeTables, 'tables' => $this->tables, 'views' => $this->views, 'triggers' => $this->triggers)); $this->compressManager->close(); throw new SGExceptionPause(); } $vals = $this->escape($tableName, $row); if ($onlyOnce) { $lineSize += $this->compressManager->write("INSERT INTO `{$tableName}` VALUES (" . implode(",", $vals) . ")"); $onlyOnce = false; } else { $lineSize += $this->compressManager->write(",(" . implode(",", $vals) . ")"); } if ($lineSize > self::MAXLINESIZE) { $onlyOnce = true; $this->compressManager->write(";/*SGEnd*/" . PHP_EOL); $lineSize = 0; } if ($this->delegate) { $this->delegate->didExportRow(); } $row = $this->dbHandler->fetch($st); $chunk++; } $offset += $limit; $chunk = 0; } if (!$onlyOnce) { $this->compressManager->write(";/*SGEnd*/" . PHP_EOL); } $this->endListValues($tableName); }
$options['directory'] = array('wp-content'); } if (@$options['backupType'] == SG_BACKUP_TYPE_FULL) { } setManualBackupOptions($options); } else { $options = json_decode(SGConfig::get('SG_ACTIVE_BACKUP_OPTIONS'), true); setManualBackupOptions($options); } $wpContent = basename(WP_CONTENT_DIR); $upload_dir = wp_upload_dir(); $wpUploads = basename($upload_dir['basedir']); $reloadHandler = new SGReloadHandler(); if ($reloadHandler->canReload()) { $pauseHandler = new SGPauseHandler(); $notificationCenter = SGNotificationCenter::getInstance(); $notificationCenter->addObserver(SG_NOTIFICATION_SHOULD_CONTINUE_ACTION, $pauseHandler, 'shouldPause', true); $notificationCenter->addObserver(SG_NOTIFICATION_DID_FINISH_BACKUP, $pauseHandler, 'didFinishBackup'); } $b = new SGBackup(); $b->backup(); if ($reloadHandler->canReload()) { $b->setReloading(true); $reloadHandler->reload(); } die(json_encode($success)); } catch (SGException $exception) { array_push($error, $exception->getMessage()); die(json_encode($error)); } function setManualBackupOptions($options)