1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Result; |
5
|
|
|
use phpbu\App\Backup\Target; |
6
|
|
|
use phpbu\App\Util\Arr; |
7
|
|
|
use phpbu\App\Util\Str; |
8
|
|
|
use ObjectStorage_Http_Client; |
9
|
|
|
use ObjectStorage; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SoftLayer ObjectStorage Sync |
13
|
|
|
* |
14
|
|
|
* @package phpbu |
15
|
|
|
* @subpackage Backup |
16
|
|
|
* @author Petr Cervenka <[email protected]> |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
19
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
20
|
|
|
* @link http://phpbu.de/ |
21
|
|
|
* @since Class available since Release 1.1.6 |
22
|
|
|
*/ |
23
|
|
|
class SoftLayer implements Simulator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* SoftLayer user |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $user; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SoftLayer secret |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $secret; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* SoftLayer container |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $container; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* SoftLayer host |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $host; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* SoftLayer remote path |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $path; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* (non-PHPDoc) |
62
|
|
|
* |
63
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
64
|
|
|
* @param array $config |
65
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
66
|
|
|
*/ |
67
|
7 |
|
public function setup(array $config) |
68
|
|
|
{ |
69
|
7 |
|
if (!class_exists('\\ObjectStorage')) { |
70
|
|
|
throw new Exception('SoftLayer SDK not loaded: use composer to install "softlayer/objectstorage"'); |
71
|
|
|
} |
72
|
|
|
// check for mandatory options |
73
|
7 |
View Code Duplication |
foreach (['user', 'secret', 'container', 'host', 'path'] as $option) { |
74
|
7 |
|
if (!Arr::isSetAndNotEmptyString($config, $option)) { |
75
|
7 |
|
throw new Exception('SoftLayer ' . $option . ' is mandatory'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
2 |
|
$this->user = $config['user']; |
80
|
2 |
|
$this->secret = $config['secret']; |
81
|
2 |
|
$this->container = $config['container']; |
82
|
2 |
|
$this->host = $config['host']; |
83
|
2 |
|
$this->path = Str::withLeadingSlash( |
84
|
2 |
|
Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])) |
85
|
|
|
); |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* (non-PHPDoc) |
90
|
|
|
* |
91
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
92
|
|
|
* @param \phpbu\App\backup\Target $target |
93
|
|
|
* @param \phpbu\App\Result $result |
94
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
95
|
|
|
*/ |
96
|
|
|
public function sync(Target $target, Result $result) |
97
|
|
|
{ |
98
|
|
|
$sourcePath = $target->getPathname(); |
99
|
|
|
$targetPath = $this->path . $target->getFilename(); |
100
|
|
|
|
101
|
|
|
$options = ['adapter' => ObjectStorage_Http_Client::SOCKET, 'timeout' => 20]; |
102
|
|
|
$objectStorage = new ObjectStorage($this->host, $this->user, $this->secret, $options); |
103
|
|
|
|
104
|
|
|
$result->debug('softlayer source: ' . $sourcePath); |
105
|
|
|
$result->debug('softlayer target: ' . $targetPath); |
106
|
|
|
|
107
|
|
|
try { |
108
|
|
|
/** @var \ObjectStorage_Container $container */ |
109
|
|
|
$container = $objectStorage->with($this->container . $targetPath) |
110
|
|
|
->setLocalFile($sourcePath) |
111
|
|
|
->setMeta('description', 'PHPBU Backup: ' . date('r', time())) |
112
|
|
|
->setHeader('Content-Type', $target->getMimeType()); |
113
|
|
|
$container->create(); |
114
|
|
|
} catch (\Exception $e) { |
115
|
|
|
throw new Exception($e->getMessage(), null, $e); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$result->debug('upload: done'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Simulate the sync execution. |
123
|
|
|
* |
124
|
|
|
* @param \phpbu\App\Backup\Target $target |
125
|
|
|
* @param \phpbu\App\Result $result |
126
|
|
|
*/ |
127
|
1 |
|
public function simulate(Target $target, Result $result) |
128
|
|
|
{ |
129
|
1 |
|
$result->debug( |
130
|
1 |
|
'sync backup to SoftLayer' . PHP_EOL |
131
|
1 |
|
. ' host: ' . $this->host . PHP_EOL |
132
|
1 |
|
. ' user: ' . $this->user . PHP_EOL |
133
|
1 |
|
. ' secret: ********' . PHP_EOL |
134
|
1 |
|
. ' container: ' . $this->container . PHP_EOL |
135
|
1 |
|
. ' location: ' . $this->path |
136
|
|
|
); |
137
|
1 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Execute the remote clean up if needed |
141
|
|
|
* |
142
|
|
|
* @param \phpbu\App\Backup\Target $target |
143
|
|
|
* @param \phpbu\App\Result $result |
144
|
|
|
*/ |
145
|
|
|
public function cleanup(Target $target, Result $result) |
146
|
|
|
{ |
147
|
|
|
// TODO: Implement cleanup() method. |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|