.htaccess
1 year ago
Backup.php
1 year ago
Download.php
1 year ago
DownloadBackupLog.php
1 year ago
Export.php
1 year ago
Extract.php
1 year ago
PreRestore.php
5 months ago
Reindex.php
2 months ago
Restore.php
5 months ago
RetentionCleanup.php
4 months ago
System.php
5 months ago
Task.php
4 months ago
index.html
1 year ago
web.config
1 year ago
Extract.php
224 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Cron\Task; |
| 4 | |
| 5 | use JetBackup\Data\Engine; |
| 6 | use JetBackup\Destination\Destination; |
| 7 | use JetBackup\Exception\DBException; |
| 8 | use JetBackup\Exception\DownloaderException; |
| 9 | use JetBackup\Exception\ExtractException; |
| 10 | use JetBackup\Exception\SGBExtractorException; |
| 11 | use JetBackup\Exception\TaskException; |
| 12 | use JetBackup\JetBackup; |
| 13 | use JetBackup\License\License; |
| 14 | use JetBackup\Queue\Queue; |
| 15 | use JetBackup\Queue\QueueItem; |
| 16 | use JetBackup\Queue\QueueItemExtract; |
| 17 | use JetBackup\SGB\Extractor; |
| 18 | use JetBackup\Snapshot\Snapshot; |
| 19 | use JetBackup\Snapshot\SnapshotDownload; |
| 20 | use JetBackup\Snapshot\SnapshotItem; |
| 21 | use SleekDB\Exceptions\InvalidArgumentException; |
| 22 | use SleekDB\Exceptions\IOException; |
| 23 | |
| 24 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 25 | |
| 26 | class Extract extends Task { |
| 27 | |
| 28 | const LOG_FILENAME = 'extract'; |
| 29 | |
| 30 | private Snapshot $_snapshot; |
| 31 | private string $_target; |
| 32 | |
| 33 | public function __construct() { |
| 34 | parent::__construct(self::LOG_FILENAME); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @return void |
| 39 | * @throws DBException |
| 40 | * @throws TaskException |
| 41 | * @throws IOException |
| 42 | * @throws InvalidArgumentException |
| 43 | */ |
| 44 | public function execute():void { |
| 45 | parent::execute(); |
| 46 | |
| 47 | /** @var QueueItemExtract $queue_item */ |
| 48 | $queue_item = $this->getQueueItem()->getItemData(); |
| 49 | |
| 50 | $this->_snapshot = new Snapshot($queue_item->getSnapshotId()); |
| 51 | |
| 52 | $destination = new Destination($this->_snapshot->getDestinationId()); |
| 53 | |
| 54 | if(!License::isValid() && !in_array($destination->getType(), Destination::LICENSE_EXCLUDED)) { |
| 55 | $this->getLogController()->logError("You can't extract backups from {$destination->getType()} destination without a license"); |
| 56 | $this->getQueueItem()->updateStatus(Queue::STATUS_ABORTED); |
| 57 | $this->getQueueItem()->updateProgress('Extract Aborted!', QueueItem::PROGRESS_LAST_STEP); |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $this->_target = $queue_item->getExtractPath() ?: $this->getQueueItem()->getWorkspace(); |
| 62 | |
| 63 | $this->getLogController()->logDebug('getExtractPath: ' . $queue_item->getExtractPath()); |
| 64 | $this->getLogController()->logDebug('getWorkspace: ' . $this->getQueueItem()->getWorkspace()); |
| 65 | |
| 66 | if($this->getQueueItem()->getStatus() == Queue::STATUS_PENDING) { |
| 67 | $this->getLogController()->logMessage('Starting Extract Task'); |
| 68 | |
| 69 | $this->getQueueItem()->getProgress()->setTotalItems(count(Queue::STATUS_EXTRACT_NAMES)+3); |
| 70 | $this->getQueueItem()->save(); |
| 71 | |
| 72 | $this->getQueueItem()->updateProgress('Starting Extract Task'); |
| 73 | } elseif($this->getQueueItem()->getStatus() > Queue::STATUS_PENDING) { |
| 74 | $this->getLogController()->logMessage('Resumed Extract Task'); |
| 75 | } |
| 76 | |
| 77 | try { |
| 78 | $this->func([$this, '_download']); |
| 79 | $this->func([$this, '_extract']); |
| 80 | |
| 81 | $queue_item->setExtractPath($this->_target); |
| 82 | $this->getQueueItem()->save(); |
| 83 | |
| 84 | if($this->getQueueItem()->getStatus() < Queue::STATUS_DONE && !$this->getQueueItem()->getErrors()) $this->getQueueItem()->updateStatus(Queue::STATUS_DONE); |
| 85 | else $this->getQueueItem()->updateStatus(Queue::STATUS_PARTIALLY); |
| 86 | $this->getLogController()->logMessage('Completed!'); |
| 87 | } catch(ExtractException $e) { |
| 88 | $this->getQueueItem()->updateStatus(Queue::STATUS_FAILED); |
| 89 | $this->getLogController()->logError($e->getMessage()); |
| 90 | $this->getLogController()->logMessage('Failed!'); |
| 91 | } |
| 92 | |
| 93 | $this->getQueueItem()->updateProgress( |
| 94 | $this->getQueueItem()->getStatus() == Queue::STATUS_DONE |
| 95 | ? 'Extract Completed!' |
| 96 | : ($this->getQueueItem()->getStatus() == Queue::STATUS_PARTIALLY |
| 97 | ? 'Completed with errors (see logs)' |
| 98 | : 'Extract Failed!'), |
| 99 | QueueItem::PROGRESS_LAST_STEP |
| 100 | ); |
| 101 | |
| 102 | $this->getLogController()->logMessage('Total time: ' . $this->getExecutionTimeElapsed()); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return array |
| 107 | * @throws ExtractException |
| 108 | */ |
| 109 | public function _getItems():array { |
| 110 | |
| 111 | $items = []; |
| 112 | |
| 113 | try { |
| 114 | $list = SnapshotItem::query() |
| 115 | ->where([SnapshotItem::PARENT_ID, '=', $this->_snapshot->getId()]) |
| 116 | ->getQuery() |
| 117 | ->fetch(); |
| 118 | } catch(\Exception $e) { |
| 119 | throw new ExtractException($e->getMessage()); |
| 120 | } |
| 121 | |
| 122 | foreach($list as $item_details) $items[] = $item_details[JetBackup::ID_FIELD]; |
| 123 | |
| 124 | return $items; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return void |
| 129 | * @throws ExtractException |
| 130 | */ |
| 131 | public function _download() { |
| 132 | |
| 133 | $queue_item = $this->getQueueItem(); |
| 134 | |
| 135 | $this->getLogController()->logMessage('Execution time: ' . $this->getExecutionTimeElapsed()); |
| 136 | $this->getLogController()->logMessage('TTL time: ' . $this->getExecutionTimeLimit()); |
| 137 | |
| 138 | $queue_item->updateStatus(Queue::STATUS_EXTRACT_DOWNLOAD); |
| 139 | $queue_item->updateProgress('Downloading backup files'); |
| 140 | $this->getLogController()->logMessage('Downloading backup files'); |
| 141 | |
| 142 | try { |
| 143 | $download = new SnapshotDownload($this->_snapshot, $this->_target); |
| 144 | $download->setLogController($this->getLogController()); |
| 145 | $download->setQueueItem($this->getQueueItem()); |
| 146 | $download->setTask($this); |
| 147 | $download->downloadAll(); |
| 148 | } catch(\Exception $e) { |
| 149 | throw new ExtractException($e->getMessage()); |
| 150 | } |
| 151 | |
| 152 | // done downloading, reset sub process bar |
| 153 | $queue_item->getProgress()->resetSub(); |
| 154 | $queue_item->save(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * @return void |
| 159 | * @throws ExtractException |
| 160 | */ |
| 161 | public function _extract():void { |
| 162 | |
| 163 | $queue_item = $this->getQueueItem(); |
| 164 | |
| 165 | $this->getLogController()->logMessage('Execution time: ' . $this->getExecutionTimeElapsed()); |
| 166 | $this->getLogController()->logMessage('TTL time: ' . $this->getExecutionTimeLimit()); |
| 167 | |
| 168 | $queue_item->updateStatus(Queue::STATUS_EXTRACT_EXTRACT); |
| 169 | $queue_item->updateProgress('Extracting data'); |
| 170 | $this->getLogController()->logMessage('Extracting backup data to ' . $this->_target); |
| 171 | |
| 172 | if($this->_snapshot->getEngine() == Engine::ENGINE_SGB) { |
| 173 | |
| 174 | // SGB snapshot has only 1 item |
| 175 | $item = $this->_snapshot->getItems()[0]; |
| 176 | |
| 177 | $path = $this->getQueueItem()->getWorkspace() . JetBackup::SEP . $item->getPath(); |
| 178 | |
| 179 | try { |
| 180 | $extractor = new Extractor($path, $this->getQueueItem()->getWorkspace()); |
| 181 | $extractor->setLogController($this->getLogController()); |
| 182 | $extractor->extract(function() { |
| 183 | $this->checkExecutionTime(); |
| 184 | }); |
| 185 | } catch(SGBExtractorException $e) { |
| 186 | throw new DownloaderException($e->getMessage()); |
| 187 | } |
| 188 | |
| 189 | unlink($path); |
| 190 | |
| 191 | |
| 192 | |
| 193 | } else { |
| 194 | |
| 195 | $this->foreachCallable([$this, '_getItems'], [], function ($i, $item_id) { |
| 196 | $item = new SnapshotItem($item_id); |
| 197 | |
| 198 | $callback = function (string $type, string $action, int $total, int $read) { |
| 199 | //sleep(1); |
| 200 | $progress = $this->getQueueItem()->getProgress(); |
| 201 | $progress->setMessage($type); // gzip / archive |
| 202 | $progress->setSubMessage($action); // decompress / extract |
| 203 | $progress->setTotalSubItems($total); |
| 204 | $progress->setCurrentSubItem($read); |
| 205 | $this->getQueueItem()->save(); |
| 206 | |
| 207 | // Call checkExecutionTime, passing the desired variables |
| 208 | $this->checkExecutionTime(function () use ($type, $action, $total, $read) { |
| 209 | $progress = $this->getQueueItem()->getProgress(); |
| 210 | $progress->setMessage($type); |
| 211 | $progress->setSubMessage('Waiting for next cron'); |
| 212 | $progress->setTotalSubItems($total); |
| 213 | $progress->setCurrentSubItem($read); |
| 214 | $this->getQueueItem()->save(); |
| 215 | }); |
| 216 | |
| 217 | }; |
| 218 | |
| 219 | $item->extract($this->_target, $this->getLogController(), $callback); |
| 220 | }); |
| 221 | |
| 222 | } |
| 223 | } |
| 224 | } |