1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of rsync-lib |
5
|
|
|
* |
6
|
|
|
* (c) Alberto Fernández <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AFM\Rsync; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Rsync wrapper. Many options are not implemented, |
16
|
|
|
* but you can use setOptionalParameters |
17
|
|
|
* |
18
|
|
|
* @author Alberto Fernández <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Rsync extends AbstractProtocol |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $executable = "/usr/bin/rsync"; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $archive = true; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $skipNewerFiles = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var bool |
39
|
|
|
*/ |
40
|
|
|
protected $followSymLinks = true; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
protected $dryRun = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $optionalParameters = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
protected $verbose = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool |
59
|
|
|
*/ |
60
|
|
|
protected $deleteFromTarget = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var bool |
64
|
|
|
*/ |
65
|
|
|
protected $deleteExcluded = false; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
protected $exclude = array(); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
protected $excludeFrom = null; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var bool |
79
|
|
|
*/ |
80
|
|
|
protected $recursive = true; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var bool |
84
|
|
|
*/ |
85
|
|
|
protected $times = false; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var bool |
89
|
|
|
*/ |
90
|
|
|
protected $showOutput = true; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var bool |
94
|
|
|
*/ |
95
|
|
|
protected $compression = false; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var bool |
99
|
|
|
*/ |
100
|
|
|
protected $remoteOrigin = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var bool |
104
|
|
|
*/ |
105
|
|
|
protected $removeSource = false; |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @var bool |
109
|
|
|
*/ |
110
|
|
|
protected $info = false; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var SSH |
114
|
|
|
*/ |
115
|
|
|
protected $ssh; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Injects and validates config |
119
|
|
|
* |
120
|
|
|
* @param array $options |
121
|
|
|
*/ |
122
|
|
|
public function __construct(Array $options = array()) |
123
|
|
|
{ |
124
|
|
|
$this->setOption($options, 'executable', 'setExecutable'); |
125
|
|
|
$this->setOption($options, 'archive', 'setArchive'); |
126
|
|
|
$this->setOption($options, 'update', 'setSkipNewerFiles'); |
127
|
|
|
$this->setOption($options, 'follow_symlinks', 'setFollowSymLinks'); |
128
|
|
|
$this->setOption($options, 'dry_run', 'setDryRun'); |
129
|
|
|
$this->setOption($options, 'option_parameters', 'setOptionalParameters'); |
130
|
|
|
$this->setOption($options, 'verbose', 'setVerbose'); |
131
|
|
|
$this->setOption($options, 'delete_from_target', 'setDeleteFromTarget'); |
132
|
|
|
$this->setOption($options, 'delete_excluded', 'setDeleteExcluded'); |
133
|
|
|
$this->setOption($options, 'exclude', 'setExclude'); |
134
|
|
|
$this->setOption($options, 'excludeFrom', 'setExcludeFrom'); |
135
|
|
|
$this->setOption($options, 'recursive', 'setRecursive'); |
136
|
|
|
$this->setOption($options, 'times', 'setTimes'); |
137
|
|
|
$this->setOption($options, 'show_output', 'setShowOutput'); |
138
|
|
|
$this->setOption($options, 'ssh', 'setSshOptions'); |
139
|
|
|
$this->setOption($options, 'compression', 'setCompression'); |
140
|
|
|
$this->setOption($options, 'remote_origin', 'setRemoteOrigin'); |
141
|
|
|
$this->setOption($options, 'remove_source', 'setRemoveSource'); |
142
|
|
|
$this->setOption($options, 'info', 'setInfo'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param $options |
147
|
|
|
*/ |
148
|
|
|
public function setSshOptions($options) |
149
|
|
|
{ |
150
|
|
|
if(is_null($this->ssh)) |
151
|
|
|
$this->ssh = new SSH($options); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Sync $origin directory with $target one. |
156
|
|
|
* If SSH was configured, you must use absolute path |
157
|
|
|
* in the target directory |
158
|
|
|
* |
159
|
|
|
* @param $origin |
160
|
|
|
* @param $target |
161
|
|
|
* |
162
|
|
|
* @throws \InvalidArgumentException If the command failed |
163
|
|
|
*/ |
164
|
|
|
public function sync($origin, $target) |
165
|
|
|
{ |
166
|
|
|
$command = $this->getCommand($origin, $target); |
167
|
|
|
|
168
|
|
|
$command->execute($this->showOutput); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getExecutable() |
175
|
|
|
{ |
176
|
|
|
return $this->executable; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function getArchive() |
183
|
|
|
{ |
184
|
|
|
return $this->archive; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param $archive |
189
|
|
|
*/ |
190
|
|
|
public function setArchive($archive) |
191
|
|
|
{ |
192
|
|
|
$this->archive = $archive; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $skipNewerFiles |
197
|
|
|
*/ |
198
|
|
|
public function setSkipNewerFiles($skipNewerFiles) |
199
|
|
|
{ |
200
|
|
|
$this->skipNewerFiles = $skipNewerFiles; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return bool |
205
|
|
|
*/ |
206
|
|
|
public function getSkipNewerFiles() |
207
|
|
|
{ |
208
|
|
|
return $this->skipNewerFiles; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $followSymLinks |
213
|
|
|
*/ |
214
|
|
|
public function setFollowSymLinks($followSymLinks) |
215
|
|
|
{ |
216
|
|
|
$this->followSymLinks = $followSymLinks; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return bool |
221
|
|
|
*/ |
222
|
|
|
public function getFollowSymLinks() |
223
|
|
|
{ |
224
|
|
|
return $this->followSymLinks; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param $dryRun |
229
|
|
|
*/ |
230
|
|
|
public function setDryRun($dryRun) |
231
|
|
|
{ |
232
|
|
|
$this->dryRun = $dryRun; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @return bool |
237
|
|
|
*/ |
238
|
|
|
public function getDryRun() |
239
|
|
|
{ |
240
|
|
|
return $this->dryRun; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @param $optionalParameters |
245
|
|
|
*/ |
246
|
|
|
public function setOptionalParameters($optionalParameters) |
247
|
|
|
{ |
248
|
|
|
$this->optionalParameters = $optionalParameters; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return array |
253
|
|
|
*/ |
254
|
|
|
public function getOptionalParameters() |
255
|
|
|
{ |
256
|
|
|
return $this->optionalParameters; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param $verbose |
261
|
|
|
*/ |
262
|
|
|
public function setVerbose($verbose) |
263
|
|
|
{ |
264
|
|
|
$this->verbose = $verbose; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @return bool |
269
|
|
|
*/ |
270
|
|
|
public function getVerbose() |
271
|
|
|
{ |
272
|
|
|
return $this->verbose; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @param $deleteExcluded |
277
|
|
|
*/ |
278
|
|
|
public function setDeleteExcluded($deleteExcluded) |
279
|
|
|
{ |
280
|
|
|
$this->deleteExcluded = $deleteExcluded; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
|
|
public function getDeleteExcluded() |
287
|
|
|
{ |
288
|
|
|
return $this->deleteExcluded; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* @param $deleteFromTarget |
293
|
|
|
*/ |
294
|
|
|
public function setDeleteFromTarget($deleteFromTarget) |
295
|
|
|
{ |
296
|
|
|
$this->deleteFromTarget = $deleteFromTarget; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return bool |
301
|
|
|
*/ |
302
|
|
|
public function getDeleteFromTarget() |
303
|
|
|
{ |
304
|
|
|
return $this->deleteFromTarget; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* @param $exclude |
309
|
|
|
*/ |
310
|
|
|
public function setExclude($exclude) |
311
|
|
|
{ |
312
|
|
|
$this->exclude = $exclude; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
|
public function getExclude() |
319
|
|
|
{ |
320
|
|
|
return $this->exclude; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param $exclude |
325
|
|
|
*/ |
326
|
|
|
public function setExcludeFrom($excludeFrom) |
327
|
|
|
{ |
328
|
|
|
$this->excludeFrom = $excludeFrom; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* @return string |
333
|
|
|
*/ |
334
|
|
|
public function getExcludeFrom() |
335
|
|
|
{ |
336
|
|
|
return $this->excludeFrom; |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* @param $recursive |
341
|
|
|
*/ |
342
|
|
|
public function setRecursive($recursive) |
343
|
|
|
{ |
344
|
|
|
$this->recursive = $recursive; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* @return bool |
349
|
|
|
*/ |
350
|
|
|
public function getRecursive() |
351
|
|
|
{ |
352
|
|
|
return $this->recursive; |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* @param bool $times |
357
|
|
|
*/ |
358
|
|
|
public function setTimes($times) |
359
|
|
|
{ |
360
|
|
|
$this->times = $times; |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @return bool |
365
|
|
|
*/ |
366
|
|
|
public function getTimes() |
367
|
|
|
{ |
368
|
|
|
return $this->times; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @param $showOutput |
373
|
|
|
*/ |
374
|
|
|
public function setShowOutput($showOutput) |
375
|
|
|
{ |
376
|
|
|
$this->showOutput = $showOutput; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
/** |
380
|
|
|
* @return bool |
381
|
|
|
*/ |
382
|
|
|
public function getShowOutput() |
383
|
|
|
{ |
384
|
|
|
return $this->showOutput; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param $compression |
389
|
|
|
*/ |
390
|
|
|
public function setCompression($compression) |
391
|
|
|
{ |
392
|
|
|
$this->compression = $compression; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* @return bool |
397
|
|
|
*/ |
398
|
|
|
public function getCompression() |
399
|
|
|
{ |
400
|
|
|
return $this->compression; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @param $remoteOrigin |
405
|
|
|
*/ |
406
|
|
|
public function setRemoteOrigin($remoteOrigin) |
407
|
|
|
{ |
408
|
|
|
$this->remoteOrigin = (bool) $remoteOrigin; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* @return bool |
413
|
|
|
*/ |
414
|
|
|
public function getRemoteOrigin() |
415
|
|
|
{ |
416
|
|
|
return $this->remoteOrigin; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* @param $removeSource |
421
|
|
|
*/ |
422
|
|
|
public function setRemoveSource($removeSource) |
423
|
|
|
{ |
424
|
|
|
$this->removeSource = (bool) $removeSource; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* @return bool |
429
|
|
|
*/ |
430
|
|
|
public function getRemoveSource() |
431
|
|
|
{ |
432
|
|
|
return $this->removeSource; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @param $info |
437
|
|
|
*/ |
438
|
|
|
public function setInfo($info) |
439
|
|
|
{ |
440
|
|
|
$this->info = $info; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* @return bool |
445
|
|
|
*/ |
446
|
|
|
public function getInfo() |
447
|
|
|
{ |
448
|
|
|
return $this->info; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Gets command generated for this current |
453
|
|
|
* rsync configuration. You can use it to test |
454
|
|
|
* or execute it later without using the sync method |
455
|
|
|
* |
456
|
|
|
* @param $origin |
457
|
|
|
* @param $target |
458
|
|
|
* |
459
|
|
|
* @return Command |
460
|
|
|
*/ |
461
|
|
|
public function getCommand($origin, $target) |
462
|
|
|
{ |
463
|
|
|
$command = new Command($this->executable); |
464
|
|
|
|
465
|
|
|
if($this->skipNewerFiles) |
466
|
|
|
$command->addOption("u"); |
467
|
|
|
|
468
|
|
|
if($this->followSymLinks) |
469
|
|
|
$command->addOption("L"); |
470
|
|
|
|
471
|
|
|
if($this->dryRun) |
472
|
|
|
$command->addOption("n"); |
473
|
|
|
|
474
|
|
|
if($this->verbose) |
475
|
|
|
$command->addOption("v"); |
476
|
|
|
|
477
|
|
|
if($this->compression) |
478
|
|
|
$command->addOption("z"); |
479
|
|
|
|
480
|
|
|
// add any optional options we've specified |
481
|
|
|
$extra_options = $this->getOptionalParameters(); |
482
|
|
|
if(!empty($extra_options)) |
483
|
|
|
{ |
484
|
|
|
// if the extra options were given as a flat string, then convert it to an array |
485
|
|
|
if (is_string($extra_options)) |
486
|
|
|
$extra_options = str_split($extra_options); |
487
|
|
|
|
488
|
|
|
// add each extra option we've defined. |
489
|
|
|
if (is_array($extra_options)) |
490
|
|
|
{ |
491
|
|
|
foreach($extra_options as $option) |
492
|
|
|
{ |
493
|
|
|
$command->addOption($option); |
494
|
|
|
} |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
if($this->times) |
499
|
|
|
$command->addArgument('times'); |
500
|
|
|
|
501
|
|
|
if($this->deleteFromTarget) |
502
|
|
|
$command->addArgument('delete'); |
503
|
|
|
|
504
|
|
|
if($this->removeSource) |
505
|
|
|
$command->addArgument('remove-source-files'); |
506
|
|
|
|
507
|
|
|
if($this->deleteExcluded) |
508
|
|
|
$command->addArgument('delete-excluded'); |
509
|
|
|
|
510
|
|
|
if($this->info) |
511
|
|
|
$command->addArgument('info', $this->info); |
512
|
|
|
|
513
|
|
|
if(!empty($this->exclude)) |
514
|
|
|
{ |
515
|
|
|
foreach($this->exclude as $excluded) |
516
|
|
|
{ |
517
|
|
|
$command->addArgument('exclude', $excluded); |
518
|
|
|
} |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
if(!empty($this->excludeFrom)) |
522
|
|
|
{ |
523
|
|
|
$command->addArgument('exclude-from', $this->excludeFrom); |
524
|
|
|
} |
525
|
|
|
|
526
|
|
|
if($this->archive) |
527
|
|
|
$command->addOption("a"); |
528
|
|
|
|
529
|
|
|
if(!$this->archive && $this->recursive) |
530
|
|
|
$command->addOption("r"); |
531
|
|
|
|
532
|
|
|
if(!is_null($this->ssh)) |
533
|
|
|
{ |
534
|
|
|
$ssh = $this->ssh->getConnectionOptions(); |
535
|
|
|
$command->addArgument("rsh", $ssh); |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
if(is_null($this->ssh)) |
539
|
|
|
{ |
540
|
|
|
$command->addParameter($origin); |
541
|
|
|
$command->addParameter($target); |
542
|
|
|
} |
543
|
|
|
elseif($this->remoteOrigin) |
544
|
|
|
{ |
545
|
|
|
$command->addParameter($this->ssh->getHostConnection() . ":" .$origin); |
546
|
|
|
$command->addParameter($target); |
547
|
|
|
} |
548
|
|
|
else |
549
|
|
|
{ |
550
|
|
|
$command->addParameter($origin); |
551
|
|
|
$command->addParameter($this->ssh->getHostConnection() . ":" .$target); |
552
|
|
|
} |
553
|
|
|
|
554
|
|
|
return $command; |
555
|
|
|
} |
556
|
|
|
} |
557
|
|
|
|