SystemBackup.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backup\Cron;
  7. use Magento\Framework\App\Filesystem\DirectoryList;
  8. use Magento\Store\Model\ScopeInterface;
  9. /**
  10. * Performs scheduled backup.
  11. */
  12. class SystemBackup
  13. {
  14. const XML_PATH_BACKUP_ENABLED = 'system/backup/enabled';
  15. const XML_PATH_BACKUP_TYPE = 'system/backup/type';
  16. const XML_PATH_BACKUP_MAINTENANCE_MODE = 'system/backup/maintenance';
  17. /**
  18. * Error messages
  19. *
  20. * @var array
  21. */
  22. protected $_errors = [];
  23. /**
  24. * Backup data
  25. *
  26. * @var \Magento\Backup\Helper\Data
  27. */
  28. protected $_backupData = null;
  29. /**
  30. * Core registry
  31. *
  32. * @var \Magento\Framework\Registry
  33. */
  34. protected $_coreRegistry = null;
  35. /**
  36. * @var \Psr\Log\LoggerInterface
  37. */
  38. protected $_logger;
  39. /**
  40. * Core store config
  41. *
  42. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  43. */
  44. protected $_scopeConfig;
  45. /**
  46. * Filesystem facade
  47. *
  48. * @var \Magento\Framework\Filesystem
  49. */
  50. protected $_filesystem;
  51. /**
  52. * @var \Magento\Framework\Backup\Factory
  53. */
  54. protected $_backupFactory;
  55. /**
  56. * @var \Magento\Framework\App\MaintenanceMode
  57. */
  58. protected $maintenanceMode;
  59. /**
  60. * @param \Magento\Backup\Helper\Data $backupData
  61. * @param \Magento\Framework\Registry $coreRegistry
  62. * @param \Psr\Log\LoggerInterface $logger
  63. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  64. * @param \Magento\Framework\Filesystem $filesystem
  65. * @param \Magento\Framework\Backup\Factory $backupFactory
  66. * @param \Magento\Framework\App\MaintenanceMode $maintenanceMode
  67. */
  68. public function __construct(
  69. \Magento\Backup\Helper\Data $backupData,
  70. \Magento\Framework\Registry $coreRegistry,
  71. \Psr\Log\LoggerInterface $logger,
  72. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  73. \Magento\Framework\Filesystem $filesystem,
  74. \Magento\Framework\Backup\Factory $backupFactory,
  75. \Magento\Framework\App\MaintenanceMode $maintenanceMode
  76. ) {
  77. $this->_backupData = $backupData;
  78. $this->_coreRegistry = $coreRegistry;
  79. $this->_logger = $logger;
  80. $this->_scopeConfig = $scopeConfig;
  81. $this->_filesystem = $filesystem;
  82. $this->_backupFactory = $backupFactory;
  83. $this->maintenanceMode = $maintenanceMode;
  84. }
  85. /**
  86. * Create Backup
  87. *
  88. * @return $this
  89. * @throws \Exception
  90. */
  91. public function execute()
  92. {
  93. if (!$this->_backupData->isEnabled()) {
  94. return $this;
  95. }
  96. if (!$this->_scopeConfig->isSetFlag(self::XML_PATH_BACKUP_ENABLED, ScopeInterface::SCOPE_STORE)) {
  97. return $this;
  98. }
  99. if ($this->_scopeConfig->isSetFlag(self::XML_PATH_BACKUP_MAINTENANCE_MODE, ScopeInterface::SCOPE_STORE)) {
  100. $this->maintenanceMode->set(true);
  101. }
  102. $type = $this->_scopeConfig->getValue(self::XML_PATH_BACKUP_TYPE, ScopeInterface::SCOPE_STORE);
  103. $this->_errors = [];
  104. try {
  105. $backupManager = $this->_backupFactory->create(
  106. $type
  107. )->setBackupExtension(
  108. $this->_backupData->getExtensionByType($type)
  109. )->setTime(
  110. time()
  111. )->setBackupsDir(
  112. $this->_backupData->getBackupsDir()
  113. );
  114. $this->_coreRegistry->register('backup_manager', $backupManager);
  115. if ($type != \Magento\Framework\Backup\Factory::TYPE_DB) {
  116. $backupManager->setRootDir(
  117. $this->_filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath()
  118. )->addIgnorePaths(
  119. $this->_backupData->getBackupIgnorePaths()
  120. );
  121. }
  122. $backupManager->create();
  123. $message = $this->_backupData->getCreateSuccessMessageByType($type);
  124. $this->_logger->info($message);
  125. } catch (\Exception $e) {
  126. $this->_errors[] = $e->getMessage();
  127. $this->_errors[] = $e->getTrace();
  128. throw $e;
  129. }
  130. if ($this->_scopeConfig->isSetFlag(self::XML_PATH_BACKUP_MAINTENANCE_MODE, ScopeInterface::SCOPE_STORE)) {
  131. $this->maintenanceMode->set(false);
  132. }
  133. return $this;
  134. }
  135. }