1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Source; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Rsync as RsyncTrait; |
5
|
|
|
use phpbu\App\Backup\Source; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Cli\Executable; |
8
|
|
|
use phpbu\App\Exception; |
9
|
|
|
use phpbu\App\Result; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Rsync source class. |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
19
|
|
|
* @link http://phpbu.de/ |
20
|
|
|
* @since Class available since Release 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Rsync extends SimulatorExecutable implements Simulator |
23
|
|
|
{ |
24
|
|
|
use RsyncTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Setup. |
28
|
|
|
* |
29
|
|
|
* @see \phpbu\App\Backup\Source |
30
|
|
|
* @param array $conf |
31
|
|
|
* @throws \phpbu\App\Exception |
32
|
|
|
*/ |
33
|
|
|
public function setup(array $conf = []) |
34
|
|
|
{ |
35
|
|
|
$this->setupRsync($conf); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Execute the backup. |
40
|
|
|
* |
41
|
|
|
* @see \phpbu\App\Backup\Source |
42
|
|
|
* @param \phpbu\App\Backup\Target $target |
43
|
|
|
* @param \phpbu\App\Result $result |
44
|
|
|
* @return \phpbu\App\Backup\Source\Status |
45
|
|
|
* @throws \phpbu\App\Exception |
46
|
|
|
*/ |
47
|
|
|
public function backup(Target $target, Result $result) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$rsync = $this->execute($target); |
50
|
|
|
$result->debug($rsync->getCmd()); |
51
|
|
|
|
52
|
|
|
if (!$rsync->wasSuccessful()) { |
53
|
|
|
throw new Exception('rsync failed:' . $rsync->getStdErr()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->createStatus($target); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Configure the Executable to run the 'rsync' command. |
61
|
|
|
* |
62
|
|
|
* @param \phpbu\App\Cli\Executable\Rsync $exec |
63
|
|
|
* @param \phpbu\App\Backup\Target $target |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
protected function configureExecutable(Executable\Rsync $exec, Target $target) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$exec->fromHost($this->host) |
68
|
|
|
->fromUser($this->user) |
69
|
|
|
->fromPath($this->path) |
70
|
|
|
->toPath($this->getRsyncLocation($target)) |
71
|
|
|
->removeDeleted($this->delete) |
72
|
|
|
->exclude($this->excludes); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create backup status. |
77
|
|
|
* |
78
|
|
|
* @param \phpbu\App\Backup\Target |
79
|
|
|
* @return \phpbu\App\Backup\Source\Status |
80
|
|
|
*/ |
81
|
|
|
protected function createStatus(Target $target) |
82
|
|
|
{ |
83
|
|
|
$status = Status::create(); |
84
|
|
|
if (!$this->isDirSync) { |
85
|
|
|
$targetFile = $target->getPathnamePlain(); |
86
|
|
|
is_dir($targetFile) ? $status->uncompressedDirectory($targetFile) : $status->uncompressedFile($targetFile); |
87
|
|
|
} |
88
|
|
|
return $status; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.