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
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Update;
use Magento\Update\Backup\BackupInfo;
use Magento\Update\Status;
/**
* Class for creating Magento codebase backups.
*/
class Backup
{
/**
* @var BackupInfo
*/
protected $backupInfo;
/**
* @var Status
*/
protected $status;
/**
* Initialize dependencies.
*
* @param BackupInfo|null $backupInfo
* @param Status|null $status
*/
public function __construct(BackupInfo $backupInfo = null, Status $status = null)
{
$this->backupInfo = $backupInfo ? $backupInfo : new BackupInfo();
$this->status = $status ? $status : new Status();
}
/**
* Create backup archive using unix zip tool.
*
* @return $this
* @throws \RuntimeException
*/
public function execute()
{
$backupFilePath = $this->backupInfo->getBackupPath() . $this->backupInfo->generateBackupFilename();
$command = $this->buildShellCommand($backupFilePath);
$this->status->add(sprintf('Creating backup archive "%s" ...', $backupFilePath), \Psr\Log\LogLevel::INFO);
exec($command, $output, $return);
if ($return) {
throw new \RuntimeException(
sprintf('Cannot create backup with command "%s": %s', $command, implode("\n", $output),
\Psr\Log\LogLevel::ERROR
)
);
}
$this->status->add(sprintf('Backup archive "%s" has been created.', $backupFilePath), \Psr\Log\LogLevel::INFO);
return $this;
}
/**
* Construct shell command for creating backup archive.
*
* @param string $backupFilePath
* @return string
*/
protected function buildShellCommand($backupFilePath)
{
$excludedElements = '';
foreach ($this->backupInfo->getBlacklist() as $excludedElement) {
$elementPath = $excludedElement;
$fullPath = $this->backupInfo->getArchivedDirectory() . '/' . $elementPath;
$excludedElements .= is_dir($fullPath) ? $elementPath . '\* ' : $elementPath . ' ';
}
$changeDirectoryCommand = sprintf("cd %s", $this->backupInfo->getArchivedDirectory());
$zipCommand = sprintf("zip -r %s . -x %s", $backupFilePath, $excludedElements);
return $changeDirectoryCommand . ' && ' . $zipCommand;
}
}