1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Runner; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Compressor; |
5
|
|
|
use phpbu\App\Backup\Source\Status; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Result; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Compression Runner |
11
|
|
|
* |
12
|
|
|
* @package phpbu |
13
|
|
|
* @author Sebastian Feldmann <[email protected]> |
14
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
15
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
16
|
|
|
* @link https://phpbu.de/ |
17
|
|
|
* @since Class available since Release 5.1.0 |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
abstract class Compression extends Process |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Compress the backup if the source did not handle the compression. |
24
|
|
|
* |
25
|
|
|
* @param \phpbu\App\Backup\Source\Status $status |
26
|
|
|
* @param \phpbu\App\Backup\Target $target |
27
|
|
|
* @param \phpbu\App\Result $result |
28
|
|
|
* @throws \phpbu\App\Exception |
29
|
|
|
*/ |
30
|
12 |
|
protected function compress(Status $status, Target $target, Result $result) |
31
|
|
|
{ |
32
|
|
|
// if the target is not compressed yet |
33
|
|
|
// and should be compressed or at least archived (tar) |
34
|
12 |
|
if (!$status->handledCompression() && ($target->shouldBeCompressed() || $status->isDirectory())) { |
35
|
3 |
|
$this->handleCompression($target, $result, $status); |
36
|
|
|
} |
37
|
12 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Handle directory compression for sources which can't handle compression by them self. |
41
|
|
|
* |
42
|
|
|
* @param \phpbu\App\Backup\Target $target |
43
|
|
|
* @param \phpbu\App\Result $result |
44
|
|
|
* @param \phpbu\App\Backup\Source\Status $status |
45
|
|
|
* @throws \phpbu\App\Exception |
46
|
|
|
*/ |
47
|
3 |
|
protected function handleCompression(Target $target, Result $result, Status $status) |
48
|
|
|
{ |
49
|
|
|
// is backup data a directory or a file |
50
|
3 |
|
if ($status->isDirectory()) { |
51
|
2 |
|
$this->compressDirectory($target, $result, $status->getDataPath()); |
52
|
|
|
} else { |
53
|
1 |
|
$this->compressFile($target, $result, $status->getDataPath()); |
54
|
|
|
} |
55
|
3 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Compresses the target if the target is a directory. |
59
|
|
|
* |
60
|
|
|
* @param \phpbu\App\Backup\Target $target |
61
|
|
|
* @param \phpbu\App\Result $result |
62
|
|
|
* @param string $dataToCompress |
63
|
|
|
* @throws \phpbu\App\Exception |
64
|
|
|
*/ |
65
|
2 |
|
protected function compressDirectory(Target $target, Result $result, $dataToCompress) |
66
|
|
|
{ |
67
|
|
|
// archive data |
68
|
2 |
|
$dirCompressor = new Compressor\Directory($dataToCompress); |
69
|
2 |
|
$archiveFile = $this->executeCompressor($dirCompressor, $target, $result); |
70
|
|
|
|
71
|
|
|
// if target should be compressed but tar couldn't handle the compression |
72
|
|
|
// run extra file compression to compress the tar file |
73
|
2 |
|
if ($target->shouldBeCompressed() && !$dirCompressor->canCompress($target)) { |
74
|
|
|
$this->compressFile($target, $result, $archiveFile); |
75
|
|
|
} |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Compresses the target if the target is a single file. |
80
|
|
|
* |
81
|
|
|
* @param \phpbu\App\Backup\Target $target |
82
|
|
|
* @param \phpbu\App\Result $result |
83
|
|
|
* @param string $dataToCompress |
84
|
|
|
* @throws \phpbu\App\Exception |
85
|
|
|
*/ |
86
|
1 |
|
protected function compressFile(Target $target, Result $result, $dataToCompress) |
87
|
|
|
{ |
88
|
1 |
|
$fileCompressor = new Compressor\File($dataToCompress); |
89
|
1 |
|
$this->executeCompressor($fileCompressor, $target, $result); |
90
|
1 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Execute the compressor. |
94
|
|
|
* Returns the path to the created archive file. |
95
|
|
|
* |
96
|
|
|
* @param \phpbu\App\Backup\Compressor\Compressible $compressor |
97
|
|
|
* @param \phpbu\App\Backup\Target $target |
98
|
|
|
* @param \phpbu\App\Result $result |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
abstract protected function executeCompressor( |
102
|
|
|
Compressor\Compressible $compressor, |
103
|
|
|
Target $target, |
104
|
|
|
Result $result |
105
|
|
|
) : string; |
106
|
|
|
} |
107
|
|
|
|