Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Filesystem.php
Go to the documentation of this file.
1 <?php
8 
19 
26 {
32  protected $_ignorePaths = [];
33 
39  protected $_useFtp = false;
40 
46  protected $_ftpHost;
47 
53  protected $_ftpUser;
54 
60  protected $_ftpPass;
61 
67  protected $_ftpPath;
68 
72  protected $rollBackFtp;
73 
77  protected $rollBackFs;
78 
85  public function rollback()
86  {
87  $this->_lastOperationSucceed = false;
88 
89  set_time_limit(0);
90  ignore_user_abort(true);
91 
92  $rollbackWorker = $this->_useFtp ? $this->getRollBackFtp() : $this->getRollBackFs();
93  $rollbackWorker->run();
94 
95  $this->_lastOperationSucceed = true;
97  }
98 
105  public function create()
106  {
107  set_time_limit(0);
108  ignore_user_abort(true);
109 
110  $this->_lastOperationSucceed = false;
111 
112  $this->_checkBackupsDir();
113 
114  $fsHelper = new Helper();
115 
116  $filesInfo = $fsHelper->getInfo(
117  $this->getRootDir(),
120  $this->getIgnorePaths()
121  );
122 
123  if (!$filesInfo['readable']) {
124  throw new NotEnoughPermissions(
125  new Phrase('Not enough permissions to read files for backup')
126  );
127  }
128  $this->validateAvailableDiscSpace($this->getBackupsDir(), $filesInfo['size']);
129 
130  $tarTmpPath = $this->_getTarTmpPath();
131 
132  $tarPacker = new Tar();
133  $tarPacker->setSkipFiles($this->getIgnorePaths())->pack($this->getRootDir(), $tarTmpPath, true);
134 
135  if (!is_file($tarTmpPath) || filesize($tarTmpPath) == 0) {
136  throw new LocalizedException(
137  new Phrase('Failed to create backup')
138  );
139  }
140 
141  $backupPath = $this->getBackupPath();
142 
143  $gzPacker = new Gz();
144  $gzPacker->pack($tarTmpPath, $backupPath);
145 
146  if (!is_file($backupPath) || filesize($backupPath) == 0) {
147  throw new LocalizedException(
148  new Phrase('Failed to create backup')
149  );
150  }
151 
152  @unlink($tarTmpPath);
153 
154  $this->_lastOperationSucceed = true;
156  }
157 
166  public function validateAvailableDiscSpace($backupDir, $size)
167  {
168  $freeSpace = disk_free_space($backupDir);
169  $requiredSpace = 2 * $size;
170  if ($requiredSpace > $freeSpace) {
171  throw new NotEnoughFreeSpace(
172  new Phrase(
173  'Warning: necessary space for backup is ' . (ceil($requiredSpace) / 1024)
174  . 'MB, but your free disc space is ' . (ceil($freeSpace) / 1024) . 'MB.'
175  )
176  );
177  }
178  }
179 
189  public function setUseFtp($host, $username, $password, $path)
190  {
191  $this->_useFtp = true;
192  $this->_ftpHost = $host;
193  $this->_ftpUser = $username;
194  $this->_ftpPass = $password;
195  $this->_ftpPath = $path;
196  return $this;
197  }
198 
206  public function getType()
207  {
208  return 'filesystem';
209  }
210 
217  public function addIgnorePaths($paths)
218  {
219  if (is_string($paths)) {
220  if (!in_array($paths, $this->_ignorePaths)) {
221  $this->_ignorePaths[] = $paths;
222  }
223  } elseif (is_array($paths)) {
224  foreach ($paths as $path) {
225  $this->addIgnorePaths($path);
226  }
227  }
228 
229  return $this;
230  }
231 
237  public function getIgnorePaths()
238  {
239  return $this->_ignorePaths;
240  }
241 
250  public function setBackupsDir($backupsDir)
251  {
252  parent::setBackupsDir($backupsDir);
253  $this->addIgnorePaths($backupsDir);
254  return $this;
255  }
256 
262  public function getFtpPath()
263  {
264  return $this->_ftpPath;
265  }
266 
272  public function getFtpConnectString()
273  {
274  return 'ftp://' . $this->_ftpUser . ':' . $this->_ftpPass . '@' . $this->_ftpHost . $this->_ftpPath;
275  }
276 
283  protected function _checkBackupsDir()
284  {
285  $backupsDir = $this->getBackupsDir();
286 
287  if (!is_dir($backupsDir)) {
288  $backupsDirParentDirectory = basename($backupsDir);
289 
290  if (!is_writeable($backupsDirParentDirectory)) {
291  throw new NotEnoughPermissions(
292  new Phrase('Cant create backups directory')
293  );
294  }
295 
296  mkdir($backupsDir);
297  chmod($backupsDir);
298  }
299 
300  if (!is_writable($backupsDir)) {
301  throw new NotEnoughPermissions(
302  new Phrase('Backups directory is not writeable')
303  );
304  }
305  }
306 
312  protected function _getTarTmpPath()
313  {
314  $tmpName = '~tmp-' . microtime(true) . '.tar';
315  return $this->getBackupsDir() . '/' . $tmpName;
316  }
317 
322  protected function getRollBackFtp()
323  {
324  if (!$this->rollBackFtp) {
325  $this->rollBackFtp = ObjectManager::getInstance()->create(
326  Ftp::class,
327  ['snapshotObject' => $this]
328  );
329  }
330 
331  return $this->rollBackFtp;
332  }
333 
338  protected function getRollBackFs()
339  {
340  if (!$this->rollBackFs) {
341  $this->rollBackFs = ObjectManager::getInstance()->create(
342  Fs::class,
343  ['snapshotObject' => $this]
344  );
345  }
346 
347  return $this->rollBackFs;
348  }
349 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setUseFtp($host, $username, $password, $path)
Definition: Filesystem.php:189
disk_free_space($path)
Definition: io.php:36
is_writable($path)
Definition: io.php:25
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25
validateAvailableDiscSpace($backupDir, $size)
Definition: Filesystem.php:166
$paths
Definition: _bootstrap.php:83