Data.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Backup\Helper;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Framework\App\MaintenanceMode;
  10. use Magento\Framework\Filesystem;
  11. /**
  12. * Backup data helper
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  17. {
  18. /**
  19. * @var Filesystem
  20. */
  21. protected $_filesystem;
  22. /**
  23. * @var \Magento\Framework\AuthorizationInterface
  24. */
  25. protected $_authorization;
  26. /**
  27. * @var \Magento\Framework\App\Cache\TypeListInterface
  28. */
  29. protected $_cacheTypeList;
  30. /**
  31. * Construct
  32. *
  33. * @param \Magento\Framework\App\Helper\Context $context
  34. * @param Filesystem $filesystem
  35. * @param \Magento\Framework\AuthorizationInterface $authorization
  36. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  37. */
  38. public function __construct(
  39. \Magento\Framework\App\Helper\Context $context,
  40. Filesystem $filesystem,
  41. \Magento\Framework\AuthorizationInterface $authorization,
  42. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  43. ) {
  44. parent::__construct($context);
  45. $this->_authorization = $authorization;
  46. $this->_filesystem = $filesystem;
  47. $this->_cacheTypeList = $cacheTypeList;
  48. }
  49. /**
  50. * Get all possible backup type values with descriptive title
  51. *
  52. * @return array
  53. */
  54. public function getBackupTypes()
  55. {
  56. return [
  57. \Magento\Framework\Backup\Factory::TYPE_DB => __('Database'),
  58. \Magento\Framework\Backup\Factory::TYPE_MEDIA => __('Database and Media'),
  59. \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT => __('System'),
  60. \Magento\Framework\Backup\Factory::TYPE_SNAPSHOT_WITHOUT_MEDIA => __('System (excluding Media)')
  61. ];
  62. }
  63. /**
  64. * Get all possible backup type values
  65. *
  66. * @return string[]
  67. */
  68. public function getBackupTypesList()
  69. {
  70. return [
  71. \Magento\Framework\Backup\Factory::TYPE_DB,
  72. \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT,
  73. \Magento\Framework\Backup\Factory::TYPE_SNAPSHOT_WITHOUT_MEDIA,
  74. \Magento\Framework\Backup\Factory::TYPE_MEDIA
  75. ];
  76. }
  77. /**
  78. * Get default backup type value
  79. *
  80. * @return string
  81. */
  82. public function getDefaultBackupType()
  83. {
  84. return \Magento\Framework\Backup\Factory::TYPE_DB;
  85. }
  86. /**
  87. * Get directory path where backups stored
  88. *
  89. * @return string
  90. */
  91. public function getBackupsDir()
  92. {
  93. return $this->_filesystem->getDirectoryWrite(DirectoryList::VAR_DIR)->getAbsolutePath('backups');
  94. }
  95. /**
  96. * Get backup file extension by backup type
  97. *
  98. * @param string $type
  99. * @return string
  100. */
  101. public function getExtensionByType($type)
  102. {
  103. $extensions = $this->getExtensions();
  104. return $extensions[$type] ?? '';
  105. }
  106. /**
  107. * Get all types to extensions map
  108. *
  109. * @return array
  110. */
  111. public function getExtensions()
  112. {
  113. return [
  114. \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT => 'tgz',
  115. \Magento\Framework\Backup\Factory::TYPE_SNAPSHOT_WITHOUT_MEDIA => 'tgz',
  116. \Magento\Framework\Backup\Factory::TYPE_MEDIA => 'tgz',
  117. \Magento\Framework\Backup\Factory::TYPE_DB => 'sql'
  118. ];
  119. }
  120. /**
  121. * Generate backup download name
  122. *
  123. * @param \Magento\Backup\Model\Backup $backup
  124. * @return string
  125. */
  126. public function generateBackupDownloadName(\Magento\Backup\Model\Backup $backup)
  127. {
  128. $additionalExtension = $backup->getType() == \Magento\Framework\Backup\Factory::TYPE_DB ? '.sql' : '';
  129. return $backup->getTime() .
  130. '_' .
  131. $backup->getType() .
  132. '_' .
  133. $backup->getName() .
  134. $additionalExtension .
  135. '.' .
  136. $this->getExtensionByType(
  137. $backup->getType()
  138. );
  139. }
  140. /**
  141. * Check Permission for Rollback
  142. *
  143. * @return bool
  144. */
  145. public function isRollbackAllowed()
  146. {
  147. return $this->_authorization->isAllowed('Magento_Backup::rollback');
  148. }
  149. /**
  150. * Get paths that should be ignored when creating system snapshots
  151. *
  152. * @return string[]
  153. */
  154. public function getBackupIgnorePaths()
  155. {
  156. return [
  157. '.git',
  158. '.svn',
  159. $this->_filesystem->getDirectoryRead(MaintenanceMode::FLAG_DIR)
  160. ->getAbsolutePath(MaintenanceMode::FLAG_FILENAME),
  161. $this->_filesystem->getDirectoryRead(DirectoryList::SESSION)->getAbsolutePath(),
  162. $this->_filesystem->getDirectoryRead(DirectoryList::CACHE)->getAbsolutePath(),
  163. $this->_filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath(),
  164. $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('full_page_cache'),
  165. $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('locks'),
  166. $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('report'),
  167. ];
  168. }
  169. /**
  170. * Get paths that should be ignored when rolling back system snapshots
  171. *
  172. * @return string[]
  173. */
  174. public function getRollbackIgnorePaths()
  175. {
  176. return [
  177. '.svn',
  178. '.git',
  179. $this->_filesystem->getDirectoryRead(MaintenanceMode::FLAG_DIR)
  180. ->getAbsolutePath(MaintenanceMode::FLAG_FILENAME),
  181. $this->_filesystem->getDirectoryRead(DirectoryList::SESSION)->getAbsolutePath(),
  182. $this->_filesystem->getDirectoryRead(DirectoryList::LOG)->getAbsolutePath(),
  183. $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('locks'),
  184. $this->_filesystem->getDirectoryRead(DirectoryList::VAR_DIR)->getAbsolutePath('report'),
  185. $this->_filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath('errors'),
  186. $this->_filesystem->getDirectoryRead(DirectoryList::ROOT)->getAbsolutePath('index.php'),
  187. ];
  188. }
  189. /**
  190. * Get backup create success message by backup type
  191. *
  192. * @param string $type
  193. * @return void|string
  194. */
  195. public function getCreateSuccessMessageByType($type)
  196. {
  197. $messagesMap = [
  198. \Magento\Framework\Backup\Factory::TYPE_SYSTEM_SNAPSHOT => __('You created the system backup.'),
  199. \Magento\Framework\Backup\Factory::TYPE_SNAPSHOT_WITHOUT_MEDIA => __(
  200. 'You created the system backup (excluding media).'
  201. ),
  202. \Magento\Framework\Backup\Factory::TYPE_MEDIA => __('You created the database and media backup.'),
  203. \Magento\Framework\Backup\Factory::TYPE_DB => __('You created the database backup.'),
  204. ];
  205. if (!isset($messagesMap[$type])) {
  206. return;
  207. }
  208. return $messagesMap[$type];
  209. }
  210. /**
  211. * Invalidate Cache
  212. *
  213. * @return $this
  214. */
  215. public function invalidateCache()
  216. {
  217. if ($cacheTypes = $this->_cacheConfig->getTypes()) {
  218. $cacheTypesList = array_keys($cacheTypes);
  219. $this->_cacheTypeList->invalidate($cacheTypesList);
  220. }
  221. return $this;
  222. }
  223. /**
  224. * Creates backup's display name from it's name
  225. *
  226. * @param string $name
  227. * @return string
  228. */
  229. public function nameToDisplayName($name)
  230. {
  231. return str_replace('_', ' ', $name);
  232. }
  233. /**
  234. * Extracts information from backup's filename
  235. *
  236. * @param string $filename
  237. * @return \Magento\Framework\DataObject
  238. */
  239. public function extractDataFromFilename($filename)
  240. {
  241. $extensions = $this->getExtensions();
  242. $filenameWithoutExtension = $filename;
  243. foreach ($extensions as $extension) {
  244. $filenameWithoutExtension = preg_replace(
  245. '/' . preg_quote($extension, '/') . '$/',
  246. '',
  247. $filenameWithoutExtension
  248. );
  249. }
  250. $filenameWithoutExtension = substr($filenameWithoutExtension, 0, strrpos($filenameWithoutExtension, "."));
  251. list($time, $type) = explode("_", $filenameWithoutExtension);
  252. $name = str_replace($time . '_' . $type, '', $filenameWithoutExtension);
  253. if (!empty($name)) {
  254. $name = substr($name, 1);
  255. }
  256. $result = new \Magento\Framework\DataObject();
  257. $result->addData(['name' => $name, 'type' => $type, 'time' => $time]);
  258. return $result;
  259. }
  260. /**
  261. * Is backup functionality enabled.
  262. *
  263. * @return bool
  264. * @since 100.2.6
  265. */
  266. public function isEnabled(): bool
  267. {
  268. return $this->scopeConfig->isSetFlag('system/backup/functionality_enabled');
  269. }
  270. }