1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\Cli; |
5
|
|
|
use phpbu\App\Backup\Sync; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Cli\Executable; |
8
|
|
|
use phpbu\App\Result; |
9
|
|
|
use phpbu\App\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Rsync |
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.1.0 |
21
|
|
|
*/ |
22
|
|
|
class Rsync extends Cli implements Simulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Path to rsync binary. |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $pathToRsync; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Raw args |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $args; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Remote username |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $user; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Target host |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $host; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Target path |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $path; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Files to ignore, extracted from config string separated by ":" |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $excludes; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Should only the created backup be synced or the complete directory |
68
|
|
|
* |
69
|
|
|
* @var boolean |
70
|
|
|
*/ |
71
|
|
|
protected $isDirSync; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove deleted files remotely as well |
75
|
|
|
* |
76
|
|
|
* @var boolean |
77
|
|
|
*/ |
78
|
|
|
protected $delete; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* (non-PHPDoc) |
82
|
|
|
* |
83
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
84
|
|
|
* @param array $options |
85
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
86
|
|
|
*/ |
87
|
9 |
|
public function setup(array $options) |
88
|
|
|
{ |
89
|
9 |
|
$this->pathToRsync = Util\Arr::getValue($options, 'pathToRsync'); |
90
|
|
|
|
91
|
9 |
|
if (Util\Arr::isSetAndNotEmptyString($options, 'args')) { |
92
|
4 |
|
$this->args = $options['args']; |
93
|
4 |
|
} else { |
94
|
5 |
|
if (!Util\Arr::isSetAndNotEmptyString($options, 'path')) { |
95
|
1 |
|
throw new Exception('option \'path\' is missing'); |
96
|
|
|
} |
97
|
4 |
|
$this->path = Util\Str::replaceDatePlaceholders($options['path']); |
98
|
|
|
|
99
|
4 |
|
if (Util\Arr::isSetAndNotEmptyString($options, 'user')) { |
100
|
1 |
|
$this->user = $options['user']; |
101
|
1 |
|
} |
102
|
4 |
|
if (Util\Arr::isSetAndNotEmptyString($options, 'host')) { |
103
|
1 |
|
$this->host = $options['host']; |
104
|
1 |
|
} |
105
|
|
|
|
106
|
4 |
|
$this->excludes = Util\Str::toList(Util\Arr::getValue($options, 'exclude', ''), ':'); |
107
|
4 |
|
$this->delete = Util\Str::toBoolean(Util\Arr::getValue($options, 'delete', ''), false); |
108
|
4 |
|
$this->isDirSync = Util\Str::toBoolean(Util\Arr::getValue($options, 'dirsync', ''), false); |
109
|
|
|
} |
110
|
8 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* (non-PHPDoc) |
114
|
|
|
* |
115
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
116
|
|
|
* @param \phpbu\App\Backup\Target $target |
117
|
|
|
* @param \phpbu\App\Result $result |
118
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
119
|
|
|
*/ |
120
|
2 |
View Code Duplication |
public function sync(Target $target, Result $result) |
121
|
|
|
{ |
122
|
2 |
|
if ($this->args) { |
123
|
|
|
// pro mode define all arguments yourself |
124
|
|
|
// WARNING! no escaping is done by phpbu |
125
|
1 |
|
$result->debug('WARNING: phpbu uses your rsync args without escaping'); |
126
|
1 |
|
} |
127
|
2 |
|
$rsync = $this->execute($target); |
128
|
|
|
|
129
|
2 |
|
$result->debug($rsync->getCmd()); |
130
|
|
|
|
131
|
2 |
|
if (!$rsync->wasSuccessful()) { |
132
|
1 |
|
throw new Exception('rsync failed: ' . $rsync->getStdErr()); |
133
|
|
|
} |
134
|
1 |
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Simulate the sync execution. |
139
|
|
|
* |
140
|
|
|
* @param \phpbu\App\Backup\Target $target |
141
|
|
|
* @param \phpbu\App\Result $result |
142
|
6 |
|
*/ |
143
|
|
|
public function simulate(Target $target, Result $result) |
144
|
6 |
|
{ |
145
|
4 |
|
$result->debug( |
146
|
4 |
|
'sync backup with rsync' . PHP_EOL |
147
|
1 |
|
. $this->getExecutable($target)->getCommandLine() |
148
|
1 |
|
); |
149
|
3 |
|
} |
150
|
3 |
|
|
151
|
3 |
|
/** |
152
|
3 |
|
* Create the Exec to run the 'rsync' command. |
153
|
3 |
|
* |
154
|
3 |
|
* @param \phpbu\App\Backup\Target $target |
155
|
3 |
|
* @return \phpbu\App\Cli\Executable |
156
|
|
|
*/ |
157
|
4 |
|
public function getExecutable(Target $target) |
158
|
6 |
|
{ |
159
|
|
|
if (null == $this->executable) { |
160
|
|
|
$this->executable = new Executable\Rsync($this->pathToRsync); |
161
|
|
|
if (!empty($this->args)) { |
162
|
|
|
$this->executable->useArgs(Util\Str::replaceTargetPlaceholders($this->args, $target->getPathname())); |
163
|
|
|
} else { |
164
|
|
|
$this->executable->syncFrom($this->getSyncSource($target)) |
165
|
|
|
->toHost($this->host) |
166
|
|
|
->toPath($this->path) |
167
|
3 |
|
->asUser($this->user) |
168
|
|
|
->compressed(!$target->shouldBeCompressed()) |
169
|
3 |
|
->removeDeleted($this->delete) |
170
|
|
|
->exclude($this->excludes); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
return $this->executable; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return sync source. |
178
|
|
|
* |
179
|
|
|
* @param \phpbu\App\Backup\Target |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
|
|
public function getSyncSource(Target $target) |
183
|
|
|
{ |
184
|
|
|
return $this->isDirSync ? $target->getPath() : $target->getPathname(); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|