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