1 | <?php |
||
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 | * List of paths to exclude |
||
47 | * --exclude |
||
48 | * |
||
49 | * @var array |
||
50 | */ |
||
51 | private $excludes; |
||
52 | |||
53 | /** |
||
54 | * Special compression program |
||
55 | * --use-compress-program |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | private $compressProgram; |
||
60 | |||
61 | /** |
||
62 | * Force local file resolution |
||
63 | * |
||
64 | * --force-local |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | private $forceLocal; |
||
69 | |||
70 | /** |
||
71 | * Tar should ignore failed reads |
||
72 | * --ignore-failed-read |
||
73 | * |
||
74 | * @var bool |
||
75 | */ |
||
76 | private $ignoreFailedRead; |
||
77 | |||
78 | /** |
||
79 | * Remove the packed data |
||
80 | * |
||
81 | * @var bool |
||
82 | */ |
||
83 | private $removeSourceDir; |
||
84 | |||
85 | /** |
||
86 | * Compression to use. |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | private $compression = ''; |
||
91 | |||
92 | /** |
||
93 | * Throttle cpu usage. |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | private $throttle = ''; |
||
98 | |||
99 | /** |
||
100 | * Path where to store the archive. |
||
101 | * |
||
102 | * @var string |
||
103 | */ |
||
104 | private $pathToArchive; |
||
105 | |||
106 | /** |
||
107 | * Setup. |
||
108 | * |
||
109 | * @see \phpbu\App\Backup\Source |
||
110 | * @param array $conf |
||
111 | * @throws \phpbu\App\Exception |
||
112 | */ |
||
113 | 17 | public function setup(array $conf = []) |
|
128 | |||
129 | /** |
||
130 | * Execute the backup. |
||
131 | * |
||
132 | * @see \phpbu\App\Backup\Source |
||
133 | * @param \phpbu\App\Backup\Target $target |
||
134 | * @param \phpbu\App\Result $result |
||
135 | * @return \phpbu\App\Backup\Source\Status |
||
136 | * @throws \phpbu\App\Exception |
||
137 | */ |
||
138 | 7 | public function backup(Target $target, Result $result) : Status |
|
139 | { |
||
140 | // make sure source path is a directory |
||
141 | 7 | $this->validatePath(); |
|
142 | // set uncompressed default MIME type |
||
143 | 6 | $target->setMimeType('application/x-tar'); |
|
144 | 6 | $tar = $this->execute($target); |
|
145 | |||
146 | 6 | $result->debug($tar->getCmdPrintable()); |
|
147 | |||
148 | 6 | if (!$tar->isSuccessful() && !$this->onlyAcceptedWarnings($tar)) { |
|
149 | 3 | throw new Exception('tar failed: ' . $tar->getStdErr()); |
|
150 | } |
||
151 | |||
152 | 3 | return $this->createStatus($target); |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Determine if there where read errors, but the user is aware. |
||
157 | * The user has acknowledged that he accepts those warnings ba setting 'ignoreFailedRead'. |
||
158 | * |
||
159 | * @param \phpbu\App\Cli\Result $tar |
||
160 | * @return bool |
||
161 | */ |
||
162 | 4 | private function onlyAcceptedWarnings(CliResult $tar) : bool |
|
163 | { |
||
164 | 4 | return $tar->getReturnCode() == 1 && $this->ignoreFailedRead; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Setup the Executable to run the 'tar' command. |
||
169 | * |
||
170 | * @param \phpbu\App\Backup\Target |
||
171 | * @return \phpbu\App\Cli\Executable |
||
172 | */ |
||
173 | 15 | protected function createExecutable(Target $target) : Executable |
|
200 | |||
201 | /** |
||
202 | * Check the source to compress. |
||
203 | * |
||
204 | * @throws \phpbu\App\Exception |
||
205 | */ |
||
206 | 7 | private function validatePath() |
|
212 | |||
213 | /** |
||
214 | * Create backup status. |
||
215 | * |
||
216 | * @param \phpbu\App\Backup\Target |
||
217 | * @return \phpbu\App\Backup\Source\Status |
||
218 | */ |
||
219 | 3 | protected function createStatus(Target $target) : Status |
|
229 | } |
||
230 |