Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
BackupRollback.php
Go to the documentation of this file.
1 <?php
8 
17 
24 {
28  const DEFAULT_BACKUP_DIRECTORY = 'backups';
29 
35  private $backupsDir;
36 
42  private $objectManager;
43 
49  private $log;
50 
56  private $directoryList;
57 
63  private $file;
64 
70  private $fsHelper;
71 
81  public function __construct(
82  ObjectManagerInterface $objectManager,
83  LoggerInterface $log,
84  DirectoryList $directoryList,
85  File $file,
86  Helper $fsHelper
87  ) {
88  $this->objectManager = $objectManager;
89  $this->log = $log;
90  $this->directoryList = $directoryList;
91  $this->file = $file;
92  $this->fsHelper = $fsHelper;
93  $this->backupsDir = $this->directoryList->getPath(DirectoryList::VAR_DIR)
95  }
96 
105  public function codeBackup($time, $type = Factory::TYPE_FILESYSTEM)
106  {
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');
118  } else {
119  throw new LocalizedException(new Phrase("This backup type \'$type\' is not supported."));
120  }
121  if (!$this->file->isExists($this->backupsDir)) {
122  $this->file->createDirectory($this->backupsDir);
123  }
124  $fsBackup->setBackupsDir($this->backupsDir);
125  $fsBackup->setBackupExtension('tgz');
126  $fsBackup->setTime($time);
127  $this->log->log($granularType . ' backup is starting...');
128  $fsBackup->create();
129  $this->log->log(
130  $granularType . ' backup filename: ' . $fsBackup->getBackupFilename()
131  . ' (The archive can be uncompressed with 7-Zip on Windows systems)'
132  );
133  $this->log->log($granularType . ' backup path: ' . $fsBackup->getBackupPath());
134  $this->log->logSuccess($granularType . ' backup completed successfully.');
135  return $fsBackup->getBackupPath();
136  }
137 
147  public function codeRollback($rollbackFile, $type = Factory::TYPE_FILESYSTEM, $keepSourceFile = false)
148  {
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.'));
151  }
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."));
154  }
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');
165  } else {
166  throw new LocalizedException(new Phrase("This backup type '$type' is not supported."));
167  }
168  $filesInfo = $this->fsHelper->getInfo(
169  $this->directoryList->getRoot(),
171  $ignorePaths
172  );
173  if (!$filesInfo['writable']) {
174  throw new NotEnoughPermissions(
175  new Phrase("The rollback can't be executed because not all files are writable.")
176  );
177  }
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.');
190  }
191 
198  public function dbBackup($time)
199  {
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);
205  }
206  $dbBackup->setBackupsDir($this->backupsDir);
207  $dbBackup->setBackupExtension('sql');
208  $dbBackup->setTime($time);
209  $this->log->log('DB backup is starting...');
210  $dbBackup->create();
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();
215  }
216 
225  public function dbRollback($rollbackFile, $keepSourceFile = false)
226  {
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.'));
229  }
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."));
232  }
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]);
242  }
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);
250  }
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.');
255  }
256 
262  private function getCodeBackupIgnorePaths()
263  {
264  return [
265  $this->directoryList->getPath(DirectoryList::MEDIA),
266  $this->directoryList->getPath(DirectoryList::STATIC_VIEW),
267  $this->directoryList->getPath(DirectoryList::VAR_DIR),
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'
274  ];
275  }
276 
282  private function getMediaBackupIgnorePaths()
283  {
284  $ignorePaths = [];
285  foreach (new \DirectoryIterator($this->directoryList->getRoot()) as $item) {
286  if (!$item->isDot() && ($this->directoryList->getPath(DirectoryList::PUB) !== $item->getPathname())) {
287  $ignorePaths[] = str_replace('\\', '/', $item->getPathname());
288  }
289  }
290  foreach (new \DirectoryIterator($this->directoryList->getPath(DirectoryList::PUB)) as $item) {
291  if (!$item->isDot() && ($this->directoryList->getPath(DirectoryList::MEDIA) !== $item->getPathname())) {
292  $ignorePaths[] = str_replace('\\', '/', $item->getPathname());
293  }
294  }
295  return $ignorePaths;
296  }
297 
306  {
307  $filesystemSize = 0;
309  $ignorePaths = $this->getCodeBackupIgnorePaths();
311  $ignorePaths = $this->getMediaBackupIgnorePaths();
312  } else {
313  throw new LocalizedException(new Phrase("This backup type '$type' is not supported."));
314  }
315  $filesInfo = $this->fsHelper->getInfo(
316  $this->directoryList->getRoot(),
318  $ignorePaths
319  );
320  if ($filesInfo['size']) {
321  $filesystemSize = $filesInfo['size'];
322  }
323  return $filesystemSize;
324  }
325 
332  public function getDBDiskSpace()
333  {
335  $dbBackup = $this->objectManager->create(\Magento\Framework\Backup\Db::class);
336  return $dbBackup->getDBSize();
337  }
338 
347  private function getCorrectFileNameWithoutPrefix(\Magento\Framework\Backup\Db $dbRollback, $rollbackFile)
348  {
349  $namePrefix = $dbRollback->getTime() . '_' . $dbRollback->getType();
350  //delete prefix.
351  $fileNameWithoutPrefix = str_replace($namePrefix, '', $rollbackFile);
352  //change '_' to ' '.
353  $fileNameWithoutPrefix = str_replace('_', ' ', $fileNameWithoutPrefix);
354  //delete file extension.
355  $fileNameWithoutPrefix = pathinfo($fileNameWithoutPrefix, PATHINFO_FILENAME);
356 
357  return $fileNameWithoutPrefix;
358  }
359 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$objectManager
Definition: bootstrap.php:17
getFSDiskSpace($type=Factory::TYPE_FILESYSTEM)
$type
Definition: item.phtml:13
__construct(ObjectManagerInterface $objectManager, LoggerInterface $log, DirectoryList $directoryList, File $file, Helper $fsHelper)