42 private $objectManager;
56 private $directoryList;
90 $this->directoryList = $directoryList;
92 $this->fsHelper = $fsHelper;
108 $fsBackup = $this->objectManager->create(\
Magento\Framework\Backup\Filesystem::class);
109 $fsBackup->setRootDir($this->directoryList->getRoot());
111 $fsBackup->addIgnorePaths($this->getCodeBackupIgnorePaths());
112 $granularType =
'Code';
113 $fsBackup->setName(
'code');
115 $fsBackup->addIgnorePaths($this->getMediaBackupIgnorePaths());
116 $granularType =
'Media';
117 $fsBackup->setName(
'media');
119 throw new LocalizedException(
new Phrase(
"This backup type \'$type\' is not supported."));
121 if (!$this->file->isExists($this->backupsDir)) {
122 $this->file->createDirectory($this->backupsDir);
124 $fsBackup->setBackupsDir($this->backupsDir);
125 $fsBackup->setBackupExtension(
'tgz');
126 $fsBackup->setTime($time);
127 $this->log->log($granularType .
' backup is starting...');
130 $granularType .
' backup filename: ' . $fsBackup->getBackupFilename()
131 .
' (The archive can be uncompressed with 7-Zip on Windows systems)' 133 $this->log->log($granularType .
' backup path: ' . $fsBackup->getBackupPath());
134 $this->log->logSuccess($granularType .
' backup completed successfully.');
135 return $fsBackup->getBackupPath();
149 if (preg_match(
'/[0-9]_(filesystem)_(code|media)\.(tgz)$/', $rollbackFile) !== 1) {
150 throw new LocalizedException(
new Phrase(
'The rollback file is invalid. Verify the file and try again.'));
152 if (!$this->file->isExists($this->backupsDir .
'/' . $rollbackFile)) {
153 throw new LocalizedException(
new Phrase(
"The rollback file doesn't exist. Verify the file and try again."));
156 $fsRollback = $this->objectManager->create(\
Magento\Framework\Backup\Filesystem::class);
158 $ignorePaths = $this->getCodeBackupIgnorePaths();
159 $granularType =
'Code';
160 $fsRollback->setName(
'code');
162 $ignorePaths = $this->getMediaBackupIgnorePaths();
163 $granularType =
'Media';
164 $fsRollback->setName(
'media');
166 throw new LocalizedException(
new Phrase(
"This backup type '$type' is not supported."));
168 $filesInfo = $this->fsHelper->getInfo(
169 $this->directoryList->getRoot(),
173 if (!$filesInfo[
'writable']) {
174 throw new NotEnoughPermissions(
175 new Phrase(
"The rollback can't be executed because not all files are writable.")
178 $fsRollback->setRootDir($this->directoryList->getRoot());
179 $fsRollback->addIgnorePaths($ignorePaths);
180 $fsRollback->setBackupsDir($this->backupsDir);
181 $fsRollback->setBackupExtension(
'tgz');
182 $time = explode(
'_', $rollbackFile);
183 $fsRollback->setKeepSourceFile($keepSourceFile);
184 $fsRollback->setTime($time[0]);
185 $this->log->log($granularType .
' rollback is starting ...');
186 $fsRollback->rollback();
187 $this->log->log($granularType .
' rollback filename: ' . $fsRollback->getBackupFilename());
188 $this->log->log($granularType .
' rollback file path: ' . $fsRollback->getBackupPath());
189 $this->log->logSuccess($granularType .
' rollback completed successfully.');
198 public function dbBackup($time)
201 $dbBackup = $this->objectManager->create(\
Magento\Framework\Backup\Db::class);
202 $dbBackup->setRootDir($this->directoryList->getRoot());
203 if (!$this->file->isExists($this->backupsDir)) {
204 $this->file->createDirectory($this->backupsDir);
206 $dbBackup->setBackupsDir($this->backupsDir);
207 $dbBackup->setBackupExtension(
'sql');
208 $dbBackup->setTime($time);
209 $this->log->log(
'DB backup is starting...');
211 $this->log->log(
'DB backup filename: ' . $dbBackup->getBackupFilename());
212 $this->log->log(
'DB backup path: ' . $dbBackup->getBackupPath());
213 $this->log->logSuccess(
'DB backup completed successfully.');
214 return $dbBackup->getBackupPath();
225 public function dbRollback($rollbackFile, $keepSourceFile =
false)
227 if (preg_match(
'/[0-9]_(db)(.*?).(sql)$/', $rollbackFile) !== 1) {
228 throw new LocalizedException(
new Phrase(
'The rollback file is invalid. Verify the file and try again.'));
230 if (!$this->file->isExists($this->backupsDir .
'/' . $rollbackFile)) {
231 throw new LocalizedException(
new Phrase(
"The rollback file doesn't exist. Verify the file and try again."));
234 $dbRollback = $this->objectManager->create(\
Magento\Framework\Backup\Db::class);
235 $dbRollback->setRootDir($this->directoryList->getRoot());
236 $dbRollback->setBackupsDir($this->backupsDir);
237 $dbRollback->setBackupExtension(
'sql');
238 $time = explode(
'_', $rollbackFile);
239 if (count($time) === 3) {
240 $thirdPart = explode(
'.', $time[2]);
241 $dbRollback->setName($thirdPart[0]);
243 $dbRollback->setTime($time[0]);
244 $this->log->log(
'DB rollback is starting...');
245 $dbRollback->setKeepSourceFile($keepSourceFile);
246 $dbRollback->setResourceModel($this->objectManager->create(\
Magento\Backup\Model\
ResourceModel\Db::class));
247 if ($dbRollback->getBackupFilename() !== $rollbackFile) {
248 $correctName = $this->getCorrectFileNameWithoutPrefix($dbRollback, $rollbackFile);
249 $dbRollback->setName($correctName);
251 $dbRollback->rollback();
252 $this->log->log(
'DB rollback filename: ' . $dbRollback->getBackupFilename());
253 $this->log->log(
'DB rollback path: ' . $dbRollback->getBackupPath());
254 $this->log->logSuccess(
'DB rollback completed successfully.');
262 private function getCodeBackupIgnorePaths()
268 $this->directoryList->getRoot() .
'/update',
269 $this->directoryList->getRoot() .
'/node_modules',
270 $this->directoryList->getRoot() .
'/.grunt',
271 $this->directoryList->getRoot() .
'/.idea',
272 $this->directoryList->getRoot() .
'/.svn',
273 $this->directoryList->getRoot() .
'/.git' 282 private function getMediaBackupIgnorePaths()
285 foreach (
new \DirectoryIterator($this->directoryList->getRoot()) as
$item) {
287 $ignorePaths[] = str_replace(
'\\',
'/',
$item->getPathname());
292 $ignorePaths[] = str_replace(
'\\',
'/',
$item->getPathname());
309 $ignorePaths = $this->getCodeBackupIgnorePaths();
311 $ignorePaths = $this->getMediaBackupIgnorePaths();
315 $filesInfo = $this->fsHelper->getInfo(
316 $this->directoryList->getRoot(),
320 if ($filesInfo[
'size']) {
321 $filesystemSize = $filesInfo[
'size'];
323 return $filesystemSize;
332 public function getDBDiskSpace()
335 $dbBackup = $this->objectManager->create(\
Magento\Framework\Backup\Db::class);
336 return $dbBackup->getDBSize();
347 private function getCorrectFileNameWithoutPrefix(\
Magento\Framework\Backup\Db $dbRollback, $rollbackFile)
349 $namePrefix = $dbRollback->getTime() .
'_' . $dbRollback->getType();
351 $fileNameWithoutPrefix = str_replace($namePrefix,
'', $rollbackFile);
353 $fileNameWithoutPrefix = str_replace(
'_',
' ', $fileNameWithoutPrefix);
355 $fileNameWithoutPrefix = pathinfo($fileNameWithoutPrefix, PATHINFO_FILENAME);
357 return $fileNameWithoutPrefix;
elseif(isset( $params[ 'redirect_parent']))
getFSDiskSpace($type=Factory::TYPE_FILESYSTEM)
const DEFAULT_BACKUP_DIRECTORY
__construct(ObjectManagerInterface $objectManager, LoggerInterface $log, DirectoryList $directoryList, File $file, Helper $fsHelper)