BackupInfo.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update\Backup;
  7. /**
  8. * Data object, which stores information about files to be archived.
  9. */
  10. class BackupInfo
  11. {
  12. /**#@+
  13. * Types of backup
  14. */
  15. const BACKUP_CODE = 'code';
  16. const BACKUP_MEDIA = 'media';
  17. const BACKUP_DB = 'db';
  18. /**#@-*/
  19. /**
  20. * @var string
  21. */
  22. protected $blacklistFilePath;
  23. /**
  24. * @var string[]
  25. */
  26. protected $blacklist;
  27. /**
  28. * Init backup info
  29. *
  30. * @param string $blacklistFilePath
  31. */
  32. public function __construct($blacklistFilePath = null)
  33. {
  34. $this->blacklistFilePath = $blacklistFilePath ? $blacklistFilePath : __DIR__ . '/../etc/backup_blacklist.txt';
  35. }
  36. /**
  37. * Generate backup filename based on current timestamp.
  38. *
  39. * @return string
  40. */
  41. public function generateBackupFilename()
  42. {
  43. $currentDate = gmdate('Y-m-d-H-i-s', time());
  44. return 'backup-' . $currentDate . '.zip';
  45. }
  46. /**
  47. * Return files/directories, which need to be excluded from backup
  48. *
  49. * @throws \RuntimeException
  50. * @return string[]
  51. */
  52. public function getBlacklist()
  53. {
  54. if (null === $this->blacklist) {
  55. $blacklistContent = file_get_contents($this->blacklistFilePath);
  56. if ($blacklistContent === FALSE) {
  57. throw new \RuntimeException('Could not read the blacklist file: ' . $this->blacklistFilePath);
  58. }
  59. /** Ignore commented and empty lines */
  60. $blacklistArray = explode("\n", $blacklistContent);
  61. $blacklistArray = array_filter(
  62. $blacklistArray,
  63. function ($value) {
  64. $value = trim($value);
  65. return (empty($value) || strpos($value, '#') === 0) ? false : true;
  66. }
  67. );
  68. $this->blacklist = $blacklistArray;
  69. }
  70. return $this->blacklist;
  71. }
  72. /**
  73. * Return path to a directory, which need to be archived
  74. *
  75. * @return string
  76. */
  77. public function getArchivedDirectory()
  78. {
  79. return MAGENTO_BP;
  80. }
  81. /**
  82. * Return path, where backup have to be saved
  83. *
  84. * @return string
  85. */
  86. public function getBackupPath()
  87. {
  88. return BACKUP_DIR;
  89. }
  90. /**
  91. * Returns list of backup file paths
  92. *
  93. * @return array
  94. */
  95. public function getBackupFilePaths()
  96. {
  97. $timeStamp = $this->getLastBackupFileTimestamp();
  98. $backupTypes = [self::BACKUP_CODE, self::BACKUP_MEDIA];
  99. $backupPaths = [];
  100. foreach ($backupTypes as $backupType) {
  101. $fileName = BACKUP_DIR . $timeStamp . '_filesystem_' . $backupType .'.tgz';
  102. if (!file_exists($fileName)) {
  103. $backupPaths['error'][] = 'Backup file does not exist for "' . $backupType . '"';
  104. } else {
  105. $backupPaths[$backupType]['filename'] = $fileName;
  106. $backupPaths[$backupType]['type'] = 'rollback';
  107. }
  108. }
  109. $fileName = BACKUP_DIR . $timeStamp .'_' . self::BACKUP_DB .'.gz';
  110. if (!file_exists($fileName)) {
  111. $backupPaths['error'][] = 'Backup file does not exist for "' . self::BACKUP_DB . '"';
  112. } else {
  113. $backupPaths[self::BACKUP_DB]['filename'] = $fileName;
  114. $backupPaths[self::BACKUP_DB]['type'] = 'setup:rollback';
  115. }
  116. return $backupPaths;
  117. }
  118. /**
  119. * Find the timestamp of the last backup file from backup directory.
  120. *
  121. * @throws \RuntimeException
  122. * @return string
  123. */
  124. private function getLastBackupFileTimestamp()
  125. {
  126. $allFileList = scandir(BACKUP_DIR, SCANDIR_SORT_DESCENDING);
  127. $backupFileName = '';
  128. if (isset($allFileList) && !empty($allFileList)) {
  129. $backupFileName = explode('_', $allFileList[0])[0];
  130. }
  131. if (empty($backupFileName)) {
  132. throw new \RuntimeException('Backup directory is empty');
  133. }
  134. return $backupFileName;
  135. }
  136. }