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