1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Source; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Source; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Cli\Executable; |
7
|
|
|
use phpbu\App\Exception; |
8
|
|
|
use phpbu\App\Result; |
9
|
|
|
use phpbu\App\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tar source class. |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
19
|
|
|
* @link http://phpbu.de/ |
20
|
|
|
* @since Class available since Release 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Tar extends SimulatorExecutable implements Simulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Tar Executable |
26
|
|
|
* |
27
|
|
|
* @var \phpbu\App\Cli\Executable\Tar |
28
|
|
|
*/ |
29
|
|
|
protected $executable; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Path to executable. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $pathToTar; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Path to backup |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $path; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Remove the packed data |
47
|
|
|
* |
48
|
|
|
* @var boolean |
49
|
|
|
*/ |
50
|
|
|
private $removeDir; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Compression to use. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $compression; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Path where to store the archive. |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $pathToArchive; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Setup. |
68
|
|
|
* |
69
|
|
|
* @see \phpbu\App\Backup\Source |
70
|
|
|
* @param array $conf |
71
|
|
|
* @throws \phpbu\App\Exception |
72
|
|
|
*/ |
73
|
|
|
public function setup(array $conf = []) |
74
|
|
|
{ |
75
|
|
|
$this->pathToTar = Util\Arr::getValue($conf, 'pathToTar'); |
76
|
|
|
$this->path = Util\Arr::getValue($conf, 'path'); |
77
|
|
|
$this->removeDir = Util\Str::toBoolean(Util\Arr::getValue($conf, 'removeDir', ''), false); |
78
|
|
|
|
79
|
|
|
if (empty($this->path)) { |
80
|
|
|
throw new Exception('path option is mandatory'); |
81
|
7 |
|
} |
82
|
|
|
} |
83
|
7 |
|
|
84
|
7 |
|
/** |
85
|
7 |
|
* (non-PHPDoc) |
86
|
7 |
|
* |
87
|
|
|
* @see \phpbu\App\Backup\Source |
88
|
7 |
|
* @param \phpbu\App\Backup\Target $target |
89
|
1 |
|
* @param \phpbu\App\Result $result |
90
|
|
|
* @return \phpbu\App\Backup\Source\Status |
91
|
6 |
|
* @throws \phpbu\App\Exception |
92
|
|
|
*/ |
93
|
|
|
public function backup(Target $target, Result $result) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
// make sure source path is a directory |
96
|
|
|
$this->validatePath(); |
97
|
|
|
// set uncompressed default MIME type |
98
|
|
|
$target->setMimeType('application/x-tar'); |
99
|
|
|
$tar = $this->execute($target); |
100
|
|
|
|
101
|
|
|
$result->debug($tar->getCmd()); |
102
|
3 |
|
|
103
|
|
|
if (!$tar->wasSuccessful()) { |
104
|
|
|
throw new Exception('tar failed: ' . $tar->getStdErr()); |
105
|
3 |
|
} |
106
|
3 |
|
|
107
|
3 |
|
return $this->createStatus($target); |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
/** |
111
|
1 |
|
* Setup the Executable to run the 'tar' command. |
112
|
1 |
|
* |
113
|
|
|
* @param \phpbu\App\Backup\Target |
114
|
3 |
|
* @return \phpbu\App\Cli\Executable |
115
|
|
|
*/ |
116
|
3 |
|
public function getExecutable(Target $target) |
117
|
1 |
|
{ |
118
|
|
|
if (null == $this->executable) { |
119
|
|
|
// check if tar supports requested compression |
120
|
2 |
|
if ($target->shouldBeCompressed()) { |
121
|
|
|
if (!Executable\Tar::isCompressorValid($target->getCompressor()->getCommand())) { |
122
|
|
|
$this->pathToArchive = $target->getPathnamePlain(); |
123
|
|
|
} else { |
124
|
|
|
// compression could be handled by the tar command |
125
|
|
|
$this->pathToArchive = $target->getPathname(); |
126
|
|
|
$this->compression = $target->getCompressor()->getCommand(); |
127
|
|
|
} |
128
|
|
|
} else { |
129
|
6 |
|
// no compression at all |
130
|
|
|
$this->pathToArchive = $target->getPathname(); |
131
|
6 |
|
} |
132
|
|
|
$this->executable = new Executable\Tar($this->pathToTar); |
133
|
3 |
|
$this->executable->archiveDirectory($this->path) |
134
|
2 |
|
->useCompression($this->compression) |
135
|
1 |
|
->archiveTo($this->pathToArchive); |
136
|
1 |
|
} |
137
|
|
|
return $this->executable; |
138
|
1 |
|
} |
139
|
1 |
|
|
140
|
|
|
/** |
141
|
2 |
|
* Check the source to compress. |
142
|
|
|
* |
143
|
1 |
|
* @throws \phpbu\App\Exception |
144
|
|
|
*/ |
145
|
3 |
|
private function validatePath() |
146
|
3 |
|
{ |
147
|
3 |
|
if (!is_dir($this->path)) { |
148
|
3 |
|
throw new Exception('patch to compress has to be a directory'); |
149
|
3 |
|
} |
150
|
3 |
|
} |
151
|
6 |
|
|
152
|
|
|
/** |
153
|
|
|
* Create backup status. |
154
|
|
|
* |
155
|
|
|
* @param \phpbu\App\Backup\Target |
156
|
|
|
* @return \phpbu\App\Backup\Source\Status |
157
|
|
|
*/ |
158
|
|
|
protected function createStatus(Target $target) |
159
|
|
|
{ |
160
|
|
|
$status = Status::create(); |
161
|
|
|
// if tar doesn't handle the compression mark status uncompressed so the app can take care of compression |
162
|
|
|
if (!$this->getExecutable($target)->handlesCompression()) { |
163
|
|
|
$status->uncompressedFile($target->getPathnamePlain()); |
164
|
|
|
} |
165
|
|
|
return $status; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.