1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpseclib; |
5
|
|
|
use phpbu\App\Result; |
6
|
|
|
use phpbu\App\Backup\Sync; |
7
|
|
|
use phpbu\App\Backup\Target; |
8
|
|
|
use phpbu\App\Util\Arr; |
9
|
|
|
use phpbu\App\Util\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Sftp sync |
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
|
|
|
abstract class Xtp implements Simulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Host to connect to |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $host; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* User to connect with |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $user; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Password to authenticate user |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $password; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Remote path where to put the backup |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $remotePath; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check for loaded libraries or extensions. |
54
|
|
|
* |
55
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
56
|
|
|
*/ |
57
|
|
|
abstract protected function checkRequirements(); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return implemented (*)TP protocol name. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
abstract protected function getProtocolName(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* (non-PHPDoc) |
68
|
|
|
* |
69
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
70
|
|
|
* @param array $config |
71
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
72
|
|
|
*/ |
73
|
|
|
public function setup(array $config) |
74
|
|
|
{ |
75
|
|
|
$this->checkRequirements(); |
76
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'host')) { |
77
|
|
|
throw new Exception('option \'host\' is missing'); |
78
|
|
|
} |
79
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'user')) { |
80
|
|
|
throw new Exception('option \'user\' is missing'); |
81
|
|
|
} |
82
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'password')) { |
83
|
|
|
throw new Exception('option \'password\' is missing'); |
84
|
|
|
} |
85
|
|
|
$path = Arr::getValue($config, 'path', ''); |
86
|
|
|
if ('/' === substr($path, 0, 1)) { |
87
|
|
|
throw new Exception('absolute path is not allowed'); |
88
|
|
|
} |
89
|
|
|
$this->host = $config['host']; |
90
|
|
|
$this->user = $config['user']; |
91
|
|
|
$this->password = $config['password']; |
92
|
|
|
$this->remotePath = Str::withoutTrailingSlash(Str::replaceDatePlaceholders($path)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Simulate the sync execution. |
97
|
|
|
* |
98
|
|
|
* @param \phpbu\App\Backup\Target $target |
99
|
|
|
* @param \phpbu\App\Result $result |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function simulate(Target $target, Result $result) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$result->debug( |
104
|
|
|
'sync backup to ' . $this->getProtocolName() .' server' . PHP_EOL |
105
|
|
|
. ' host: ' . $this->host . PHP_EOL |
106
|
|
|
. ' user: ' . $this->user . PHP_EOL |
107
|
|
|
. ' password: ********' . PHP_EOL |
108
|
|
|
. ' path: ' . $this->remotePath |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
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.