.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
Export.php
276 lines
| 1 | <?php |
| 2 | |
| 3 | namespace JetBackup\Cron\Task; |
| 4 | |
| 5 | use JetBackup\BackupJob\BackupJob; |
| 6 | use JetBackup\Data\Engine; |
| 7 | use JetBackup\Destination\Destination; |
| 8 | use JetBackup\Exception\ArchiveException; |
| 9 | use JetBackup\Exception\DBException; |
| 10 | use JetBackup\Exception\DownloaderException; |
| 11 | use JetBackup\Exception\ExportException; |
| 12 | use JetBackup\Exception\GzipException; |
| 13 | use JetBackup\Exception\IOException; |
| 14 | use JetBackup\Exception\SGBExtractorException; |
| 15 | use JetBackup\Exception\TaskException; |
| 16 | use JetBackup\JetBackup; |
| 17 | use JetBackup\License\License; |
| 18 | use JetBackup\Queue\Queue; |
| 19 | use JetBackup\Queue\QueueItem; |
| 20 | use JetBackup\Queue\QueueItemExport; |
| 21 | use JetBackup\SGB\Extractor; |
| 22 | use JetBackup\Snapshot\Snapshot; |
| 23 | use JetBackup\Snapshot\SnapshotDownload; |
| 24 | use JetBackup\Snapshot\SnapshotItem; |
| 25 | use JetBackup\Download\Download; |
| 26 | use SleekDB\Exceptions\InvalidArgumentException; |
| 27 | |
| 28 | if (!defined( '__JETBACKUP__')) die('Direct access is not allowed'); |
| 29 | |
| 30 | class Export extends Task { |
| 31 | |
| 32 | const LOG_FILENAME = 'export'; |
| 33 | |
| 34 | private Snapshot $_snapshot; |
| 35 | |
| 36 | private QueueItemExport $_queue_item_export; |
| 37 | |
| 38 | public function __construct() { |
| 39 | parent::__construct(self::LOG_FILENAME); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @return void |
| 44 | * @throws InvalidArgumentException |
| 45 | * @throws DBException |
| 46 | * @throws TaskException |
| 47 | * @throws \SleekDB\Exceptions\IOException |
| 48 | */ |
| 49 | public function execute():void { |
| 50 | parent::execute(); |
| 51 | |
| 52 | $this->_queue_item_export = $this->getQueueItem()->getItemData(); |
| 53 | $this->_snapshot = new Snapshot($this->_queue_item_export->getSnapshotId()); |
| 54 | |
| 55 | if($this->_snapshot->getEngine() == Engine::ENGINE_SGB) { |
| 56 | $this->getLogController()->logError("You can't export legacy backups"); |
| 57 | $this->getQueueItem()->updateStatus(Queue::STATUS_ABORTED); |
| 58 | $this->getQueueItem()->updateProgress('Export Aborted!', QueueItem::PROGRESS_LAST_STEP); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $destination = new Destination($this->_snapshot->getDestinationId()); |
| 63 | |
| 64 | if(!License::isValid() && !in_array($destination->getType(), Destination::LICENSE_EXCLUDED)) { |
| 65 | $this->getLogController()->logError("You can't export backups from {$destination->getType()} destination without a license"); |
| 66 | $this->getQueueItem()->updateStatus(Queue::STATUS_ABORTED); |
| 67 | $this->getQueueItem()->updateProgress('Export Aborted!', QueueItem::PROGRESS_LAST_STEP); |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if($this->getQueueItem()->getStatus() == Queue::STATUS_PENDING) { |
| 72 | $this->getLogController()->logMessage('Starting Export Task'); |
| 73 | |
| 74 | $this->getQueueItem()->getProgress()->setTotalItems(count(Queue::STATUS_EXPORT_NAMES)+3); |
| 75 | $this->getQueueItem()->save(); |
| 76 | |
| 77 | $this->getQueueItem()->updateProgress('Starting Export Task'); |
| 78 | } elseif($this->getQueueItem()->getStatus() > Queue::STATUS_PENDING) { |
| 79 | $this->getLogController()->logMessage('Resumed Export Task'); |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | $this->func([$this, '_download']); |
| 84 | $this->func([$this, '_extract']); |
| 85 | $this->func([$this, '_build']); |
| 86 | if($this->getQueueItem()->getStatus() < Queue::STATUS_DONE && !$this->getQueueItem()->getErrors()) $this->getQueueItem()->updateStatus(Queue::STATUS_DONE); |
| 87 | else $this->getQueueItem()->updateStatus(Queue::STATUS_PARTIALLY); |
| 88 | $this->getLogController()->logMessage('Completed!'); |
| 89 | } catch(ExportException $e) { |
| 90 | $this->getQueueItem()->updateStatus(Queue::STATUS_FAILED); |
| 91 | $this->getLogController()->logError($e->getMessage()); |
| 92 | $this->getLogController()->logMessage('Failed!'); |
| 93 | } |
| 94 | |
| 95 | $this->getQueueItem()->updateProgress( |
| 96 | $this->getQueueItem()->getStatus() == Queue::STATUS_DONE |
| 97 | ? 'Export Completed!' |
| 98 | : ($this->getQueueItem()->getStatus() == Queue::STATUS_PARTIALLY |
| 99 | ? 'Completed with errors (see logs)' |
| 100 | : 'Export Failed!'), |
| 101 | QueueItem::PROGRESS_LAST_STEP |
| 102 | ); |
| 103 | |
| 104 | $this->getLogController()->logMessage('Total time: ' . $this->getExecutionTimeElapsed()); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @return void |
| 109 | * @throws ExportException |
| 110 | * @throws InvalidArgumentException |
| 111 | * @throws \SleekDB\Exceptions\IOException |
| 112 | */ |
| 113 | public function _download() { |
| 114 | |
| 115 | $queue_item = $this->getQueueItem(); |
| 116 | |
| 117 | $this->getLogController()->logMessage('Execution time: ' . $this->getExecutionTimeElapsed()); |
| 118 | $this->getLogController()->logMessage('TTL time: ' . $this->getExecutionTimeLimit()); |
| 119 | |
| 120 | $queue_item->updateStatus(Queue::STATUS_EXPORT_DOWNLOAD); |
| 121 | $queue_item->updateProgress('Getting files for extraction'); |
| 122 | $this->getLogController()->logMessage('Downloading backup files'); |
| 123 | |
| 124 | try { |
| 125 | $download = new SnapshotDownload($this->_snapshot, $this->getQueueItem()->getWorkspace()); |
| 126 | $download->setLogController($this->getLogController()); |
| 127 | $download->setQueueItem($this->getQueueItem()); |
| 128 | $download->setTask($this); |
| 129 | $download->downloadAll(); |
| 130 | } catch (\Exception $e) { |
| 131 | throw new ExportException($e->getMessage()); |
| 132 | } |
| 133 | |
| 134 | // done downloading, reset sub process bar |
| 135 | $queue_item->getProgress()->resetSub(); |
| 136 | $queue_item->save(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * @return void |
| 141 | * @throws ExportException |
| 142 | */ |
| 143 | public function _extract():void { |
| 144 | |
| 145 | $queue_item = $this->getQueueItem(); |
| 146 | |
| 147 | $this->getLogController()->logMessage('Execution time: ' . $this->getExecutionTimeElapsed()); |
| 148 | $this->getLogController()->logMessage('TTL time: ' . $this->getExecutionTimeLimit()); |
| 149 | |
| 150 | $queue_item->updateStatus(Queue::STATUS_EXPORT_EXTRACT); |
| 151 | $queue_item->updateProgress('Extracting data'); |
| 152 | $this->getLogController()->logMessage('Extracting backup data to ' . $this->getQueueItem()->getWorkspace()); |
| 153 | |
| 154 | $this->foreachCallable([$this, '_getItems'], [], function($i, $item_id) { |
| 155 | $item = new SnapshotItem($item_id); |
| 156 | try { |
| 157 | $item->extract($this->getQueueItem()->getWorkspace(), $this->getLogController(), function(string $type, string $action, int $total, int $read) { |
| 158 | |
| 159 | $progress = $this->getQueueItem()->getProgress(); |
| 160 | $progress->setMessage($type); // gzip / archive |
| 161 | $progress->setSubMessage($action); // decompress / extract |
| 162 | $progress->setTotalSubItems($total); |
| 163 | $progress->setCurrentSubItem($read); |
| 164 | $this->getQueueItem()->save(); |
| 165 | |
| 166 | // Call checkExecutionTime, passing the desired variables |
| 167 | $this->checkExecutionTime(function () use ($type, $action, $total, $read) { |
| 168 | $progress = $this->getQueueItem()->getProgress(); |
| 169 | $progress->setMessage($type); |
| 170 | $progress->setSubMessage('Waiting for next cron'); |
| 171 | $progress->setTotalSubItems($total); |
| 172 | $progress->setCurrentSubItem($read); |
| 173 | $this->getQueueItem()->save(); |
| 174 | }); |
| 175 | }); |
| 176 | } catch(ArchiveException|GzipException $e) { |
| 177 | throw new ExportException($e->getMessage()); |
| 178 | } |
| 179 | }, 'extract_items'); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * @return void |
| 184 | * @throws ExportException |
| 185 | */ |
| 186 | public function _build() { |
| 187 | |
| 188 | $queue_item = $this->getQueueItem(); |
| 189 | |
| 190 | $this->getLogController()->logMessage('Execution time: ' . $this->getExecutionTimeElapsed()); |
| 191 | $this->getLogController()->logMessage('TTL time: ' . $this->getExecutionTimeLimit()); |
| 192 | |
| 193 | $queue_item->updateStatus(Queue::STATUS_EXPORT_BUILD); |
| 194 | $queue_item->updateProgress('Extracting data'); |
| 195 | $this->getLogController()->logMessage('Extracting backup data to ' . $this->getQueueItem()->getWorkspace()); |
| 196 | |
| 197 | $type = $this->_queue_item_export->getType(); |
| 198 | $workspace = $this->getQueueItem()->getWorkspace(); |
| 199 | |
| 200 | $homedir = $workspace . JetBackup::SEP . Snapshot::SKELETON_FILES_DIRNAME; |
| 201 | $destination = $workspace . JetBackup::SEP . 'skeleton'; |
| 202 | $database_tables = []; |
| 203 | |
| 204 | try { |
| 205 | |
| 206 | $list = SnapshotItem::query() |
| 207 | ->where([SnapshotItem::PARENT_ID, '=', $this->_snapshot->getId()]) |
| 208 | ->where([SnapshotItem::BACKUP_CONTAINS, '=', BackupJob::BACKUP_ACCOUNT_CONTAINS_DATABASE]) |
| 209 | ->getQuery() |
| 210 | ->fetch(); |
| 211 | } catch(\Exception $e) { |
| 212 | throw new ExportException($e->getMessage()); |
| 213 | } |
| 214 | |
| 215 | foreach($list as $item_details) { |
| 216 | $database_tables[] = $workspace . JetBackup::SEP . Snapshot::SKELETON_DATABASE_DIRNAME . JetBackup::SEP . $item_details[SnapshotItem::NAME] . '.sql'; |
| 217 | } |
| 218 | |
| 219 | if(!is_dir($destination)) mkdir($destination, 0700); |
| 220 | |
| 221 | $export = new \JetBackup\Export\Export($this); |
| 222 | |
| 223 | try { |
| 224 | $file = $export->build($type, $homedir, $database_tables, $destination); |
| 225 | } catch(IOException $e) { |
| 226 | throw new ExportException($e->getMessage()); |
| 227 | } |
| 228 | |
| 229 | $this->func([$this, '_moveDownload'], [$file]); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * @param $file |
| 234 | * |
| 235 | * @return void |
| 236 | * @throws ExportException |
| 237 | * @throws \SleekDB\Exceptions\IOException |
| 238 | * @throws InvalidArgumentException |
| 239 | */ |
| 240 | public function _moveDownload($file) { |
| 241 | |
| 242 | try { |
| 243 | $this->getLogController()->logDebug("[_moveDownload]] File: $file " ); |
| 244 | $download = Download::create($file); |
| 245 | } catch(\Exception $e) { |
| 246 | throw new ExportException($e->getMessage()); |
| 247 | } |
| 248 | |
| 249 | $this->_queue_item_export->setDownloadId($download->getId()); |
| 250 | $this->getQueueItem()->save(); |
| 251 | |
| 252 | $this->getLogController()->logMessage("Download Id: " . $download->getId()); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * @return array |
| 257 | * @throws ExportException |
| 258 | */ |
| 259 | public function _getItems():array { |
| 260 | |
| 261 | $items = []; |
| 262 | |
| 263 | try { |
| 264 | $list = SnapshotItem::query() |
| 265 | ->where([SnapshotItem::PARENT_ID, '=', $this->_snapshot->getId()]) |
| 266 | ->getQuery() |
| 267 | ->fetch(); |
| 268 | } catch(\Exception $e) { |
| 269 | throw new ExportException($e->getMessage()); |
| 270 | } |
| 271 | |
| 272 | foreach($list as $item_details) $items[] = $item_details[JetBackup::ID_FIELD]; |
| 273 | |
| 274 | return $items; |
| 275 | } |
| 276 | } |