Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Filesystem Class Reference
Inheritance diagram for Filesystem:
AbstractBackup BackupInterface SourceFileInterface Snapshot Media Nomedia

Public Member Functions

 rollback ()
 
 create ()
 
 validateAvailableDiscSpace ($backupDir, $size)
 
 setUseFtp ($host, $username, $password, $path)
 
 getType ()
 
 addIgnorePaths ($paths)
 
 getIgnorePaths ()
 
 setBackupsDir ($backupsDir)
 
 getFtpPath ()
 
 getFtpConnectString ()
 
- Public Member Functions inherited from AbstractBackup
 setBackupExtension ($backupExtension)
 
 getBackupExtension ()
 
 setResourceModel ($resourceModel)
 
 getResourceModel ()
 
 setTime ($time)
 
 getTime ()
 
 setRootDir ($rootDir)
 
 getRootDir ()
 
 setBackupsDir ($backupsDir)
 
 getBackupsDir ()
 
 getBackupPath ()
 
 getBackupFilename ()
 
 getIsSuccess ()
 
 getErrorMessage ()
 
 setErrorMessage ($errorMessage)
 
 setName ($name, $applyFilter=true)
 
 getName ()
 
 getDisplayName ()
 
 keepSourceFile ()
 
 setKeepSourceFile (bool $keepSourceFile)
 

Protected Member Functions

 _checkBackupsDir ()
 
 _getTarTmpPath ()
 
 getRollBackFtp ()
 
 getRollBackFs ()
 
- Protected Member Functions inherited from AbstractBackup
 _filterName ($name)
 

Protected Attributes

 $_ignorePaths = []
 
 $_useFtp = false
 
 $_ftpHost
 
 $_ftpUser
 
 $_ftpPass
 
 $_ftpPath
 
 $rollBackFtp
 
 $rollBackFs
 
- Protected Attributes inherited from AbstractBackup
 $_name
 
 $_time
 
 $_backupExtension
 
 $_resourceModel
 
 $_rootDir
 
 $_backupsDir
 
 $_lastOperationSucceed = false
 
 $_lastErrorMessage
 

Detailed Description

Class to work with filesystem backups

Author
Magento Core Team core@.nosp@m.mage.nosp@m.ntoco.nosp@m.mmer.nosp@m.ce.co.nosp@m.m

Definition at line 25 of file Filesystem.php.

Member Function Documentation

◆ _checkBackupsDir()

_checkBackupsDir ( )
protected

Check backups directory existence and whether it's writeable

Returns
void
Exceptions
LocalizedException

Definition at line 283 of file Filesystem.php.

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  }
is_writable($path)
Definition: io.php:25
mkdir($pathname, $mode=0777, $recursive=false, $context=null)
Definition: ioMock.php:25

◆ _getTarTmpPath()

_getTarTmpPath ( )
protected

Generate tmp name for tarball

Returns
string

Definition at line 312 of file Filesystem.php.

313  {
314  $tmpName = '~tmp-' . microtime(true) . '.tar';
315  return $this->getBackupsDir() . '/' . $tmpName;
316  }

◆ addIgnorePaths()

addIgnorePaths (   $paths)

Add path that should be ignoring when creating or rolling back backup

Parameters
string | array$paths
Returns
$this

Definition at line 217 of file Filesystem.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$paths
Definition: _bootstrap.php:83

◆ create()

create ( )

Implementation Create Backup functionality for Filesystem

Exceptions
LocalizedException
Returns
boolean

Implements BackupInterface.

Definition at line 105 of file Filesystem.php.

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  }
validateAvailableDiscSpace($backupDir, $size)
Definition: Filesystem.php:166

◆ getFtpConnectString()

getFtpConnectString ( )

Get ftp connection string

Returns
string

Definition at line 272 of file Filesystem.php.

273  {
274  return 'ftp://' . $this->_ftpUser . ':' . $this->_ftpPass . '@' . $this->_ftpHost . $this->_ftpPath;
275  }

◆ getFtpPath()

getFtpPath ( )

Getter for $_ftpPath variable

Returns
string

Definition at line 262 of file Filesystem.php.

263  {
264  return $this->_ftpPath;
265  }

◆ getIgnorePaths()

getIgnorePaths ( )

Get paths that should be ignored while creating or rolling back backup procedure

Returns
array

Definition at line 237 of file Filesystem.php.

238  {
239  return $this->_ignorePaths;
240  }

◆ getRollBackFs()

getRollBackFs ( )
protected
Returns
Fs
Deprecated:
101.0.0

Definition at line 338 of file Filesystem.php.

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  }

◆ getRollBackFtp()

getRollBackFtp ( )
protected
Returns
Ftp
Deprecated:
101.0.0

Definition at line 322 of file Filesystem.php.

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  }

◆ getType()

getType ( )

Get backup type

Returns
string
See also
BackupInterface::getType()

Implements BackupInterface.

Definition at line 206 of file Filesystem.php.

207  {
208  return 'filesystem';
209  }

◆ rollback()

rollback ( )

Implementation Rollback functionality for Filesystem

Exceptions
LocalizedException
Returns
bool

Implements BackupInterface.

Definition at line 85 of file Filesystem.php.

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  }

◆ setBackupsDir()

setBackupsDir (   $backupsDir)

Set directory where backups saved and add it to ignore paths

Parameters
string$backupsDir
Returns
$this
See also
AbstractBackup::setBackupsDir()

Implements BackupInterface.

Definition at line 250 of file Filesystem.php.

251  {
252  parent::setBackupsDir($backupsDir);
253  $this->addIgnorePaths($backupsDir);
254  return $this;
255  }

◆ setUseFtp()

setUseFtp (   $host,
  $username,
  $password,
  $path 
)

Force class to use ftp for rollback procedure

Parameters
string$host
string$username
string$password
string$path
Returns
$this

Definition at line 189 of file Filesystem.php.

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  }

◆ validateAvailableDiscSpace()

validateAvailableDiscSpace (   $backupDir,
  $size 
)

Validate if disk space is available for creating backup

Parameters
string$backupDir
int$size
Returns
void
Exceptions
LocalizedException

Definition at line 166 of file Filesystem.php.

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  }
disk_free_space($path)
Definition: io.php:36

Field Documentation

◆ $_ftpHost

$_ftpHost
protected

Definition at line 46 of file Filesystem.php.

◆ $_ftpPass

$_ftpPass
protected

Definition at line 60 of file Filesystem.php.

◆ $_ftpPath

$_ftpPath
protected

Definition at line 67 of file Filesystem.php.

◆ $_ftpUser

$_ftpUser
protected

Definition at line 53 of file Filesystem.php.

◆ $_ignorePaths

$_ignorePaths = []
protected

Definition at line 32 of file Filesystem.php.

◆ $_useFtp

$_useFtp = false
protected

Definition at line 39 of file Filesystem.php.

◆ $rollBackFs

$rollBackFs
protected

Definition at line 77 of file Filesystem.php.

◆ $rollBackFtp

$rollBackFtp
protected

Definition at line 72 of file Filesystem.php.


The documentation for this class was generated from the following file: