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 bool |
114
|
|
|
*/ |
115
|
|
|
protected $compareDest = false; |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @var bool |
119
|
|
|
*/ |
120
|
|
|
protected $pruneEmptyDirs = false; |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @var SSH |
124
|
|
|
*/ |
125
|
|
|
protected $ssh; |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Injects and validates config |
129
|
|
|
* |
130
|
|
|
* @param array $options |
131
|
|
|
*/ |
132
|
|
|
public function __construct(Array $options = array()) |
133
|
|
|
{ |
134
|
|
|
$this->setOption($options, 'executable', 'setExecutable'); |
135
|
|
|
$this->setOption($options, 'archive', 'setArchive'); |
136
|
|
|
$this->setOption($options, 'update', 'setSkipNewerFiles'); |
137
|
|
|
$this->setOption($options, 'follow_symlinks', 'setFollowSymLinks'); |
138
|
|
|
$this->setOption($options, 'dry_run', 'setDryRun'); |
139
|
|
|
$this->setOption($options, 'option_parameters', 'setOptionalParameters'); |
140
|
|
|
$this->setOption($options, 'verbose', 'setVerbose'); |
141
|
|
|
$this->setOption($options, 'delete_from_target', 'setDeleteFromTarget'); |
142
|
|
|
$this->setOption($options, 'delete_excluded', 'setDeleteExcluded'); |
143
|
|
|
$this->setOption($options, 'exclude', 'setExclude'); |
144
|
|
|
$this->setOption($options, 'excludeFrom', 'setExcludeFrom'); |
145
|
|
|
$this->setOption($options, 'recursive', 'setRecursive'); |
146
|
|
|
$this->setOption($options, 'times', 'setTimes'); |
147
|
|
|
$this->setOption($options, 'show_output', 'setShowOutput'); |
148
|
|
|
$this->setOption($options, 'ssh', 'setSshOptions'); |
149
|
|
|
$this->setOption($options, 'compression', 'setCompression'); |
150
|
|
|
$this->setOption($options, 'remote_origin', 'setRemoteOrigin'); |
151
|
|
|
$this->setOption($options, 'remove_source', 'setRemoveSource'); |
152
|
|
|
$this->setOption($options, 'info', 'setInfo'); |
153
|
|
|
$this->setOption($options, 'compare_dest', 'setCompareDest'); |
154
|
|
|
$this->setOption($options, 'prune_empty_dirs', 'setPruneEmptyDirs'); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $options |
159
|
|
|
*/ |
160
|
|
|
public function setSshOptions($options) |
161
|
|
|
{ |
162
|
|
|
$this->ssh = new SSH($options); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sync $origin directory with $target one. |
167
|
|
|
* If SSH was configured, you must use absolute path |
168
|
|
|
* in the target directory |
169
|
|
|
* |
170
|
|
|
* @param $origin |
171
|
|
|
* @param $target |
172
|
|
|
* |
173
|
|
|
* @throws \InvalidArgumentException If the command failed |
174
|
|
|
*/ |
175
|
|
|
public function sync($origin, $target) |
176
|
|
|
{ |
177
|
|
|
$command = $this->getCommand($origin, $target); |
178
|
|
|
|
179
|
|
|
$command->execute($this->showOutput); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @return string |
184
|
|
|
*/ |
185
|
|
|
public function getExecutable() |
186
|
|
|
{ |
187
|
|
|
return $this->executable; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return bool |
192
|
|
|
*/ |
193
|
|
|
public function getArchive() |
194
|
|
|
{ |
195
|
|
|
return $this->archive; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param $archive |
200
|
|
|
*/ |
201
|
|
|
public function setArchive($archive) |
202
|
|
|
{ |
203
|
|
|
$this->archive = $archive; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return bool |
208
|
|
|
*/ |
209
|
|
|
public function getPruneEmptyDirs() |
210
|
|
|
{ |
211
|
|
|
return $this->pruneEmptyDirs; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param $pruneEmptyDirs |
216
|
|
|
*/ |
217
|
|
|
public function setPruneEmptyDirs($pruneEmptyDirs) |
218
|
|
|
{ |
219
|
|
|
$this->pruneEmptyDirs = $pruneEmptyDirs; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $skipNewerFiles |
225
|
|
|
*/ |
226
|
|
|
public function setSkipNewerFiles($skipNewerFiles) |
227
|
|
|
{ |
228
|
|
|
$this->skipNewerFiles = $skipNewerFiles; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return bool |
233
|
|
|
*/ |
234
|
|
|
public function getSkipNewerFiles() |
235
|
|
|
{ |
236
|
|
|
return $this->skipNewerFiles; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param $followSymLinks |
241
|
|
|
*/ |
242
|
|
|
public function setFollowSymLinks($followSymLinks) |
243
|
|
|
{ |
244
|
|
|
$this->followSymLinks = $followSymLinks; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
|
|
public function getFollowSymLinks() |
251
|
|
|
{ |
252
|
|
|
return $this->followSymLinks; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @param $dryRun |
257
|
|
|
*/ |
258
|
|
|
public function setDryRun($dryRun) |
259
|
|
|
{ |
260
|
|
|
$this->dryRun = $dryRun; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @return bool |
265
|
|
|
*/ |
266
|
|
|
public function getDryRun() |
267
|
|
|
{ |
268
|
|
|
return $this->dryRun; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* @param $optionalParameters |
273
|
|
|
*/ |
274
|
|
|
public function setOptionalParameters($optionalParameters) |
275
|
|
|
{ |
276
|
|
|
$this->optionalParameters = $optionalParameters; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return array |
281
|
|
|
*/ |
282
|
|
|
public function getOptionalParameters() |
283
|
|
|
{ |
284
|
|
|
return $this->optionalParameters; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param $verbose |
289
|
|
|
*/ |
290
|
|
|
public function setVerbose($verbose) |
291
|
|
|
{ |
292
|
|
|
$this->verbose = $verbose; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
/** |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function getVerbose() |
299
|
|
|
{ |
300
|
|
|
return $this->verbose; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @param $deleteExcluded |
305
|
|
|
*/ |
306
|
|
|
public function setDeleteExcluded($deleteExcluded) |
307
|
|
|
{ |
308
|
|
|
$this->deleteExcluded = $deleteExcluded; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @return bool |
313
|
|
|
*/ |
314
|
|
|
public function getDeleteExcluded() |
315
|
|
|
{ |
316
|
|
|
return $this->deleteExcluded; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param $deleteFromTarget |
321
|
|
|
*/ |
322
|
|
|
public function setDeleteFromTarget($deleteFromTarget) |
323
|
|
|
{ |
324
|
|
|
$this->deleteFromTarget = $deleteFromTarget; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* @return bool |
329
|
|
|
*/ |
330
|
|
|
public function getDeleteFromTarget() |
331
|
|
|
{ |
332
|
|
|
return $this->deleteFromTarget; |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* @param $exclude |
337
|
|
|
*/ |
338
|
|
|
public function setExclude($exclude) |
339
|
|
|
{ |
340
|
|
|
$this->exclude = $exclude; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
|
public function getExclude() |
347
|
|
|
{ |
348
|
|
|
return $this->exclude; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* @param $exclude |
353
|
|
|
*/ |
354
|
|
|
public function setExcludeFrom($excludeFrom) |
355
|
|
|
{ |
356
|
|
|
$this->excludeFrom = $excludeFrom; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* @return string |
361
|
|
|
*/ |
362
|
|
|
public function getExcludeFrom() |
363
|
|
|
{ |
364
|
|
|
return $this->excludeFrom; |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* @param $recursive |
369
|
|
|
*/ |
370
|
|
|
public function setRecursive($recursive) |
371
|
|
|
{ |
372
|
|
|
$this->recursive = $recursive; |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* @return bool |
377
|
|
|
*/ |
378
|
|
|
public function getRecursive() |
379
|
|
|
{ |
380
|
|
|
return $this->recursive; |
381
|
|
|
} |
382
|
|
|
|
383
|
|
|
/** |
384
|
|
|
* @param bool $times |
385
|
|
|
*/ |
386
|
|
|
public function setTimes($times) |
387
|
|
|
{ |
388
|
|
|
$this->times = $times; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* @return bool |
393
|
|
|
*/ |
394
|
|
|
public function getTimes() |
395
|
|
|
{ |
396
|
|
|
return $this->times; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* @param $showOutput |
401
|
|
|
*/ |
402
|
|
|
public function setShowOutput($showOutput) |
403
|
|
|
{ |
404
|
|
|
$this->showOutput = $showOutput; |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @return bool |
409
|
|
|
*/ |
410
|
|
|
public function getShowOutput() |
411
|
|
|
{ |
412
|
|
|
return $this->showOutput; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* @param $compression |
417
|
|
|
*/ |
418
|
|
|
public function setCompression($compression) |
419
|
|
|
{ |
420
|
|
|
$this->compression = $compression; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
/** |
424
|
|
|
* @return bool |
425
|
|
|
*/ |
426
|
|
|
public function getCompression() |
427
|
|
|
{ |
428
|
|
|
return $this->compression; |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param $remoteOrigin |
433
|
|
|
*/ |
434
|
|
|
public function setRemoteOrigin($remoteOrigin) |
435
|
|
|
{ |
436
|
|
|
$this->remoteOrigin = (bool)$remoteOrigin; |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* @return bool |
441
|
|
|
*/ |
442
|
|
|
public function getRemoteOrigin() |
443
|
|
|
{ |
444
|
|
|
return $this->remoteOrigin; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
/** |
448
|
|
|
* @param $removeSource |
449
|
|
|
*/ |
450
|
|
|
public function setRemoveSource($removeSource) |
451
|
|
|
{ |
452
|
|
|
$this->removeSource = (bool)$removeSource; |
453
|
|
|
} |
454
|
|
|
|
455
|
|
|
/** |
456
|
|
|
* @return bool |
457
|
|
|
*/ |
458
|
|
|
public function getRemoveSource() |
459
|
|
|
{ |
460
|
|
|
return $this->removeSource; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @param $info |
465
|
|
|
*/ |
466
|
|
|
public function setInfo($info) |
467
|
|
|
{ |
468
|
|
|
$this->info = $info; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
/** |
472
|
|
|
* @return bool |
473
|
|
|
*/ |
474
|
|
|
public function getInfo() |
475
|
|
|
{ |
476
|
|
|
return $this->info; |
477
|
|
|
} |
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* @param $dest |
481
|
|
|
*/ |
482
|
|
|
public function setCompareDest($dest) |
483
|
|
|
{ |
484
|
|
|
$this->compareDest = $dest; |
485
|
|
|
} |
486
|
|
|
|
487
|
|
|
/** |
488
|
|
|
* @return string |
489
|
|
|
*/ |
490
|
|
|
public function getCompareDest() |
491
|
|
|
{ |
492
|
|
|
return $this->compareDest; |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Gets command generated for this current |
497
|
|
|
* rsync configuration. You can use it to test |
498
|
|
|
* or execute it later without using the sync method |
499
|
|
|
* |
500
|
|
|
* @param $origin |
501
|
|
|
* @param $target |
502
|
|
|
* |
503
|
|
|
* @return Command |
504
|
|
|
*/ |
505
|
|
|
public function getCommand($origin, $target) |
506
|
|
|
{ |
507
|
|
|
$command = new Command($this->executable); |
508
|
|
|
|
509
|
|
|
if ($this->skipNewerFiles) { |
510
|
|
|
$command->addOption("u"); |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
if ($this->followSymLinks) { |
514
|
|
|
$command->addOption("L"); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
if ($this->dryRun) { |
518
|
|
|
$command->addOption("n"); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
if ($this->verbose) { |
522
|
|
|
$command->addOption("v"); |
523
|
|
|
} |
524
|
|
|
|
525
|
|
|
if ($this->compression) { |
526
|
|
|
$command->addOption("z"); |
527
|
|
|
} |
528
|
|
|
|
529
|
|
|
// add any optional options we've specified |
530
|
|
|
$extra_options = $this->getOptionalParameters(); |
531
|
|
|
if (!empty($extra_options)) { |
532
|
|
|
// if the extra options were given as a flat string, then convert it to an array |
533
|
|
|
if (is_string($extra_options)) { |
534
|
|
|
$extra_options = str_split($extra_options); |
535
|
|
|
} |
536
|
|
|
|
537
|
|
|
// add each extra option we've defined. |
538
|
|
|
if (is_array($extra_options)) { |
539
|
|
|
foreach ($extra_options as $option) { |
540
|
|
|
$command->addOption($option); |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
} |
544
|
|
|
|
545
|
|
|
if ($this->times) { |
546
|
|
|
$command->addArgument('times'); |
547
|
|
|
} |
548
|
|
|
|
549
|
|
|
if ($this->deleteFromTarget) { |
550
|
|
|
$command->addArgument('delete'); |
551
|
|
|
} |
552
|
|
|
|
553
|
|
|
if ($this->removeSource) { |
554
|
|
|
$command->addArgument('remove-source-files'); |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
if ($this->deleteExcluded) { |
558
|
|
|
$command->addArgument('delete-excluded'); |
559
|
|
|
} |
560
|
|
|
|
561
|
|
|
if ($this->info) { |
562
|
|
|
$command->addArgument('info', $this->info); |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
if ($this->compareDest) { |
566
|
|
|
$command->addArgument('compare-dest', $this->compareDest); |
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
if (!empty($this->exclude)) { |
570
|
|
|
foreach ($this->exclude as $excluded) { |
571
|
|
|
$command->addArgument('exclude', $excluded); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
if (!empty($this->excludeFrom)) { |
576
|
|
|
$command->addArgument('exclude-from', $this->excludeFrom); |
577
|
|
|
} |
578
|
|
|
|
579
|
|
|
if ($this->archive) { |
580
|
|
|
$command->addOption("a"); |
581
|
|
|
} |
582
|
|
|
|
583
|
|
|
if (!$this->archive && $this->recursive) { |
584
|
|
|
$command->addOption("r"); |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
if ($this->pruneEmptyDirs) { |
588
|
|
|
$command->addArgument('prune-empty-dirs'); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
if (!is_null($this->ssh)) { |
592
|
|
|
$ssh = $this->ssh->getConnectionOptions(); |
593
|
|
|
$command->addArgument("rsh", $ssh); |
594
|
|
|
} |
595
|
|
|
|
596
|
|
|
if (is_null($this->ssh)) { |
597
|
|
|
$command->addParameter($origin); |
598
|
|
|
$command->addParameter($target); |
599
|
|
|
} elseif ($this->remoteOrigin) { |
600
|
|
|
$command->addParameter($this->ssh->getHostConnection().":".$origin); |
601
|
|
|
$command->addParameter($target); |
602
|
|
|
} else { |
603
|
|
|
$command->addParameter($origin); |
604
|
|
|
$command->addParameter($this->ssh->getHostConnection().":".$target); |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
return $command; |
608
|
|
|
} |
609
|
|
|
} |
610
|
|
|
|