1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Runner; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Compressor; |
5
|
|
|
use phpbu\App\Backup\Source as SourceExe; |
6
|
|
|
use phpbu\App\Backup\Source\Simulator; |
7
|
|
|
use phpbu\App\Backup\Source\Status; |
8
|
|
|
use phpbu\App\Backup\Target; |
9
|
|
|
use phpbu\App\Configuration; |
10
|
|
|
use phpbu\App\Result; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Backup Runner |
14
|
|
|
* |
15
|
|
|
* @package phpbu |
16
|
|
|
* @subpackage app |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 3.0.0 |
22
|
|
|
*/ |
23
|
|
|
class Source |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Run a simulation |
27
|
|
|
* |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
private $isSimulation; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Executes the backup and compression. |
34
|
|
|
* |
35
|
|
|
* @param \phpbu\App\Backup\Source $source |
36
|
|
|
* @param \phpbu\App\Backup\Target $target |
37
|
|
|
* @param \phpbu\App\Result $result |
38
|
|
|
* @param bool $simulate |
39
|
|
|
* @throws \phpbu\App\Exception |
40
|
|
|
*/ |
41
|
|
|
public function run(SourceExe $source, Target $target, Result $result, $simulate) |
42
|
|
|
{ |
43
|
|
|
$this->isSimulation = $simulate; |
44
|
|
|
$action = $simulate ? 'simulate' : 'backup'; |
45
|
|
|
$this->{$action}($source, $target, $result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Executes the backup and compression. |
50
|
|
|
* |
51
|
|
|
* @param \phpbu\App\Backup\Source $source |
52
|
|
|
* @param \phpbu\App\Backup\Target $target |
53
|
|
|
* @param \phpbu\App\Result $result |
54
|
|
|
* @throws \phpbu\App\Exception |
55
|
|
|
*/ |
56
|
|
|
protected function backup(SourceExe $source, Target $target, Result $result) |
57
|
|
|
{ |
58
|
|
|
$status = $source->backup($target, $result); |
59
|
|
|
$this->compress($status, $target, $result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Simulates the backup. |
64
|
|
|
* |
65
|
|
|
* @param \phpbu\App\Backup\Source $source |
66
|
|
|
* @param \phpbu\App\Backup\Target $target |
67
|
|
|
* @param \phpbu\App\Result $result |
68
|
|
|
* @throws \phpbu\App\Exception |
69
|
|
|
*/ |
70
|
|
|
protected function simulate(SourceExe $source, Target $target, Result $result) |
71
|
|
|
{ |
72
|
|
|
if ($source instanceof Simulator) { |
73
|
|
|
$status = $source->simulate($target, $result); |
74
|
|
|
$this->compress($status, $target, $result); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Compress the backup if the source did not handle the compression. |
80
|
|
|
* |
81
|
|
|
* @param \phpbu\App\Backup\Source\Status $status |
82
|
|
|
* @param \phpbu\App\Backup\Target $target |
83
|
|
|
* @param \phpbu\App\Result $result |
84
|
|
|
* @throws \phpbu\App\Exception |
85
|
|
|
*/ |
86
|
|
|
protected function compress(Status $status, Target $target, Result $result) |
87
|
|
|
{ |
88
|
|
|
if ($target->shouldBeCompressed() && !$status->handledCompression()) { |
89
|
|
|
$this->handleCompression($target, $result, $status->getDataPath()); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Handle directory compression for sources which can't handle compression by them self. |
95
|
|
|
* |
96
|
|
|
* @param \phpbu\App\Backup\Target $target |
97
|
|
|
* @param \phpbu\App\Result $result |
98
|
|
|
* @param string $dataToCompress |
99
|
|
|
* @throws \phpbu\App\Exception |
100
|
|
|
*/ |
101
|
|
|
private function handleCompression(Target $target, Result $result, $dataToCompress) |
102
|
|
|
{ |
103
|
|
|
// is backup data a directory or a file |
104
|
|
|
if (is_dir($dataToCompress)) { |
105
|
|
|
$this->compressDirectory($target, $result, $dataToCompress); |
106
|
|
|
} else { |
107
|
|
|
$this->compressFile($target, $result, $dataToCompress); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Compresses the target if the target is a directory. |
113
|
|
|
* |
114
|
|
|
* @param \phpbu\App\Backup\Target $target |
115
|
|
|
* @param \phpbu\App\Result $result |
116
|
|
|
* @param string $dataToCompress |
117
|
|
|
* @throws \phpbu\App\Exception |
118
|
|
|
*/ |
119
|
|
|
private function compressDirectory(Target $target, Result $result, $dataToCompress) |
120
|
|
|
{ |
121
|
|
|
// archive data |
122
|
|
|
$dirCompressor = new Compressor\Directory($dataToCompress); |
123
|
|
|
$archiveFile = $this->executeCompressor($dirCompressor, $target, $result); |
124
|
|
|
|
125
|
|
|
// directory is archived but not compressed because tar couldn't handle the compression |
126
|
|
|
if (!$dirCompressor->canCompress($target)) { |
127
|
|
|
$this->compressFile($target, $result, $archiveFile); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Compresses the target if the target is a single file. |
133
|
|
|
* |
134
|
|
|
* @param \phpbu\App\Backup\Target $target |
135
|
|
|
* @param \phpbu\App\Result $result |
136
|
|
|
* @param string $dataToCompress |
137
|
|
|
* @throws \phpbu\App\Exception |
138
|
|
|
*/ |
139
|
|
|
private function compressFile(Target $target, Result $result, $dataToCompress) |
140
|
|
|
{ |
141
|
|
|
$fileCompressor = new Compressor\File($dataToCompress); |
142
|
|
|
$this->executeCompressor($fileCompressor, $target, $result); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Execute the compressor. |
147
|
|
|
* Returns the path to the created archive file. |
148
|
|
|
* |
149
|
|
|
* @param \phpbu\App\Backup\Compressor\Executable $compressor |
150
|
|
|
* @param \phpbu\App\Backup\Target $target |
151
|
|
|
* @param \phpbu\App\Result $result |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
private function executeCompressor(Compressor\Executable $compressor, Target $target, Result $result) |
155
|
|
|
{ |
156
|
|
|
// if this is a simulation just debug the command that would have been executed |
157
|
|
|
if ($this->isSimulation) { |
158
|
|
|
$result->debug($compressor->getExecutable($target)->getCommandLine()); |
159
|
|
|
return $compressor->getArchiveFile($target); |
160
|
|
|
} else { |
161
|
|
|
return $compressor->compress($target, $result); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|