1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli\Executable; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Executable; |
5
|
|
|
use phpbu\App\Cli\Process; |
6
|
|
|
use phpbu\App\Exception; |
7
|
|
|
use SebastianFeldmann\Cli\CommandLine; |
8
|
|
|
use SebastianFeldmann\Cli\Command\Executable as Cmd; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Rsync executable class. |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 2.1.0 |
20
|
|
|
*/ |
21
|
|
|
class Rsync extends Abstraction implements Executable |
22
|
|
|
{ |
23
|
|
|
use OptionMasker; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Source |
27
|
|
|
* |
28
|
|
|
* @var \phpbu\App\Cli\Executable\Rsync\Location |
29
|
|
|
*/ |
30
|
|
|
private $source; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Target |
34
|
|
|
* |
35
|
|
|
* @var \phpbu\App\Cli\Executable\Rsync\Location |
36
|
|
|
*/ |
37
|
|
|
private $target; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Password to use to authenticate |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $password; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Path to password file |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $passwordFile; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Raw args |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $args; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Files to ignore, extracted from config string separated by ":" |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $excludes = []; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Remove deleted files remotely as well |
69
|
|
|
* |
70
|
|
|
* @var boolean |
71
|
|
|
*/ |
72
|
|
|
protected $delete; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Compress data. |
76
|
|
|
* |
77
|
|
|
* @var boolean |
78
|
|
|
*/ |
79
|
|
|
protected $compressed = false; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Constructor. |
83
|
15 |
|
* |
84
|
|
|
* @param string $path |
85
|
15 |
|
*/ |
86
|
15 |
|
public function __construct(string $path = '') |
87
|
15 |
|
{ |
88
|
|
|
$this->setup('rsync', $path); |
89
|
|
|
$this->setMaskCandidates(['password']); |
90
|
|
|
$this->source = new Rsync\Location(); |
91
|
|
|
$this->target = new Rsync\Location(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
2 |
|
* Set custom args |
96
|
|
|
* |
97
|
2 |
|
* @param string $args |
98
|
2 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
99
|
|
|
*/ |
100
|
|
|
public function useArgs(string $args) : Rsync |
101
|
|
|
{ |
102
|
|
|
$this->args = $args; |
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
8 |
|
* Set password environment variable RSYNC_PASSWORD. |
108
|
|
|
* |
109
|
8 |
|
* @param string $password |
110
|
8 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
111
|
|
|
*/ |
112
|
|
|
public function usePassword(string $password) |
113
|
|
|
{ |
114
|
|
|
$this->password = $password; |
115
|
|
|
return $this; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
4 |
|
* Set path to password file. |
120
|
|
|
* |
121
|
4 |
|
* @param string $file |
122
|
4 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
123
|
|
|
*/ |
124
|
|
|
public function usePasswordFile(string $file) |
125
|
|
|
{ |
126
|
|
|
$this->passwordFile = $file; |
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
5 |
|
/** |
132
|
|
|
* Set source user. |
133
|
5 |
|
* |
134
|
5 |
|
* @param string $user |
135
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
136
|
|
|
*/ |
137
|
|
|
public function fromUser(string $user) : Rsync |
138
|
|
|
{ |
139
|
|
|
$this->source->setUser($user); |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
10 |
|
/** |
144
|
|
|
* Set source host. |
145
|
10 |
|
* |
146
|
10 |
|
* @param string $host |
147
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
148
|
|
|
*/ |
149
|
|
|
public function fromHost(string $host) : Rsync |
150
|
|
|
{ |
151
|
|
|
$this->source->setHost($host); |
152
|
|
|
return $this; |
153
|
|
|
} |
154
|
|
|
|
155
|
5 |
|
/** |
156
|
|
|
* Set source path. |
157
|
5 |
|
* |
158
|
5 |
|
* @param string $path |
159
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
160
|
|
|
*/ |
161
|
|
|
public function fromPath(string $path) : Rsync |
162
|
|
|
{ |
163
|
|
|
$this->source->setPath($path); |
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
4 |
|
/** |
168
|
|
|
* Use compression. |
169
|
4 |
|
* |
170
|
4 |
|
* @param bool $bool |
171
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
172
|
|
|
*/ |
173
|
|
|
public function compressed(bool $bool) : Rsync |
174
|
|
|
{ |
175
|
|
|
$this->compressed = $bool; |
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
4 |
|
/** |
180
|
|
|
* Sync to host. |
181
|
4 |
|
* |
182
|
4 |
|
* @param string $host |
183
|
|
|
* @return \phpbu\App\Cli\Executable\Rsync |
184
|
|
|
*/ |
185
|
|
|
public function toHost(string $host) : Rsync |
186
|
|
|
{ |
187
|
|
|
$this->target->setHost($host); |
188
|
|
|
return $this; |
189
|
|
|
} |
190
|
|
|
|
191
|
11 |
|
/** |
192
|
|
|
* Set path to sync to. |
193
|
11 |
|
* |
194
|
11 |
|
* @param string $path |
195
|
11 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
196
|
|
|
*/ |
197
|
11 |
|
public function toPath(string $path) : Rsync |
198
|
2 |
|
{ |
199
|
2 |
|
$this->target->setPath($path); |
200
|
9 |
|
return $this; |
201
|
1 |
|
} |
202
|
|
|
|
203
|
8 |
|
/** |
204
|
1 |
|
* Set user to connect as. |
205
|
|
|
* |
206
|
|
|
* @param string $user |
207
|
7 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
208
|
|
|
*/ |
209
|
|
|
public function toUser(string $user) : Rsync |
210
|
7 |
|
{ |
211
|
7 |
|
$this->target->setUser($user); |
212
|
|
|
return $this; |
213
|
7 |
|
} |
214
|
2 |
|
|
215
|
2 |
|
/** |
216
|
2 |
|
* Exclude files. |
217
|
2 |
|
* |
218
|
|
|
* @param array $excludes |
219
|
7 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
220
|
7 |
|
*/ |
221
|
|
|
public function exclude(array $excludes) : Rsync |
222
|
|
|
{ |
223
|
|
|
$this->excludes = $excludes; |
224
|
7 |
|
return $this; |
225
|
|
|
} |
226
|
7 |
|
|
227
|
|
|
/** |
228
|
7 |
|
* Use --delete option. |
229
|
|
|
* |
230
|
|
|
* @param bool $bool |
231
|
9 |
|
* @return \phpbu\App\Cli\Executable\Rsync |
232
|
|
|
*/ |
233
|
|
|
public function removeDeleted(bool $bool) : Rsync |
234
|
|
|
{ |
235
|
|
|
$this->delete = $bool; |
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
11 |
|
/** |
240
|
|
|
* Rsync CommandLine generator. |
241
|
11 |
|
* |
242
|
|
|
* @return \SebastianFeldmann\Cli\CommandLine |
243
|
11 |
|
* @throws \phpbu\App\Exception |
244
|
|
|
*/ |
245
|
2 |
|
protected function createCommandLine() : CommandLine |
246
|
1 |
|
{ |
247
|
1 |
|
$password = !empty($this->password) ? 'RSYNC_PASSWORD=' . escapeshellarg($this->password) . ' ' : ''; |
248
|
2 |
|
$process = new CommandLine(); |
249
|
2 |
|
$cmd = new Cmd($password . $this->binary); |
250
|
11 |
|
$process->addCommand($cmd); |
251
|
|
|
|
252
|
|
|
if (!empty($this->args)) { |
253
|
|
|
$cmd->addOption($this->args); |
254
|
|
|
} else { |
255
|
|
|
// make sure source and target are valid |
256
|
|
|
$this->validateLocations(); |
257
|
|
|
|
258
|
|
|
// use archive mode, verbose and compress if not already done |
259
|
|
|
$options = '-av' . ($this->compressed ? 'z' : ''); |
260
|
|
|
$cmd->addOption($options); |
261
|
|
|
$this->configureExcludes($cmd, $this->excludes); |
262
|
|
|
$cmd->addOptionIfNotEmpty('--delete', $this->delete, false); |
263
|
|
|
$cmd->addOptionIfNotEmpty('--password-file', $this->passwordFile); |
264
|
|
|
$cmd->addArgument($this->source->toString()); |
265
|
|
|
$cmd->addArgument($this->target->toString()); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
return $process; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Makes sure source and target are valid. |
273
|
|
|
* |
274
|
|
|
* @throws \phpbu\App\Exception |
275
|
|
|
*/ |
276
|
|
|
protected function validateLocations() |
277
|
|
|
{ |
278
|
|
|
if (!$this->source->isValid()) { |
279
|
|
|
throw new Exception('source path is missing'); |
280
|
|
|
} |
281
|
|
|
if (!$this->target->isValid()) { |
282
|
|
|
throw new Exception('target path is missing'); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Configure excludes. |
288
|
|
|
* |
289
|
|
|
* @param \SebastianFeldmann\Cli\Command\Executable $cmd |
290
|
|
|
* @param array $excludes |
291
|
|
|
*/ |
292
|
|
|
protected function configureExcludes(Cmd $cmd, array $excludes) |
293
|
|
|
{ |
294
|
|
|
foreach ($excludes as $ex) { |
295
|
|
|
$cmd->addOption('--exclude', $ex); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|