Create.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backup\Controller\Adminhtml\Index;
  7. use Magento\Framework\App\Action\HttpPostActionInterface;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\Filesystem;
  10. /**
  11. * Create backup controller
  12. */
  13. class Create extends \Magento\Backup\Controller\Adminhtml\Index implements HttpPostActionInterface
  14. {
  15. /**
  16. * Create backup action.
  17. *
  18. * @return void|\Magento\Backend\App\Action
  19. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  20. */
  21. public function execute()
  22. {
  23. if (!$this->isRequestAllowed()) {
  24. return $this->_redirect('*/*/index');
  25. }
  26. $response = new \Magento\Framework\DataObject();
  27. /**
  28. * @var \Magento\Backup\Helper\Data $helper
  29. */
  30. $helper = $this->_objectManager->get(\Magento\Backup\Helper\Data::class);
  31. try {
  32. $type = $this->getRequest()->getParam('type');
  33. if ($type == \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT && $this->getRequest()->getParam(
  34. 'exclude_media'
  35. )
  36. ) {
  37. $type = \Magento\Framework\Backup\Factory::TYPE_SNAPSHOT_WITHOUT_MEDIA;
  38. }
  39. $backupManager = $this->_backupFactory->create(
  40. $type
  41. )->setBackupExtension(
  42. $helper->getExtensionByType($type)
  43. )->setTime(
  44. time()
  45. )->setBackupsDir(
  46. $helper->getBackupsDir()
  47. );
  48. $backupManager->setName($this->getRequest()->getParam('backup_name'));
  49. $this->_coreRegistry->register('backup_manager', $backupManager);
  50. if ($this->getRequest()->getParam('maintenance_mode')) {
  51. if (!$this->maintenanceMode->set(true)) {
  52. $response->setError(
  53. __(
  54. 'You need more permissions to activate maintenance mode right now.'
  55. ) . ' ' . __(
  56. 'To create the backup, please deselect '
  57. . '"Put store into maintenance mode" or update your permissions.'
  58. )
  59. );
  60. $backupManager->setErrorMessage(
  61. __("Something went wrong while putting your store into maintenance mode.")
  62. );
  63. return $this->getResponse()->representJson($response->toJson());
  64. }
  65. }
  66. if ($type != \Magento\Framework\Backup\Factory::TYPE_DB) {
  67. /** @var Filesystem $filesystem */
  68. $filesystem = $this->_objectManager->get(\Magento\Framework\Filesystem::class);
  69. $backupManager->setRootDir($filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath())
  70. ->addIgnorePaths($helper->getBackupIgnorePaths());
  71. }
  72. $successMessage = $helper->getCreateSuccessMessageByType($type);
  73. $backupManager->create();
  74. $this->messageManager->addSuccessMessage($successMessage);
  75. $response->setRedirectUrl($this->getUrl('*/*/index'));
  76. } catch (\Magento\Framework\Backup\Exception\NotEnoughFreeSpace $e) {
  77. $errorMessage = __('You need more free space to create a backup.');
  78. } catch (\Magento\Framework\Backup\Exception\NotEnoughPermissions $e) {
  79. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->info($e->getMessage());
  80. $errorMessage = __('You need more permissions to create a backup.');
  81. } catch (\Exception $e) {
  82. $this->_objectManager->get(\Psr\Log\LoggerInterface::class)->info($e->getMessage());
  83. $errorMessage = __('We can\'t create the backup right now.');
  84. }
  85. if (!empty($errorMessage)) {
  86. $response->setError($errorMessage);
  87. $backupManager->setErrorMessage($errorMessage);
  88. }
  89. if ($this->getRequest()->getParam('maintenance_mode')) {
  90. $this->maintenanceMode->set(false);
  91. }
  92. $this->getResponse()->representJson($response->toJson());
  93. }
  94. /**
  95. * Check if request is allowed.
  96. *
  97. * @return bool
  98. */
  99. private function isRequestAllowed()
  100. {
  101. return $this->getRequest()->isAjax() && $this->getRequest()->isPost();
  102. }
  103. }