1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Backup\File\Remote; |
5
|
|
|
use phpbu\App\Result; |
6
|
|
|
use phpbu\App\Backup\Target; |
7
|
|
|
use phpbu\App\Util\Arr; |
8
|
|
|
use phpbu\App\Util\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Amazon S3 Sync base class |
12
|
|
|
* |
13
|
|
|
* @package phpbu |
14
|
|
|
* @subpackage Backup |
15
|
|
|
* @author Sebastian Feldmann <[email protected]> |
16
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
17
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
18
|
|
|
* @link http://phpbu.de/ |
19
|
|
|
* @since Class available since Release 3.0.0 |
20
|
|
|
*/ |
21
|
|
|
abstract class AmazonS3 implements Simulator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* AWS key |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $key; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* AWS secret |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $secret; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* AWS S3 bucket |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $bucket; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* TTL for all items in this bucket. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
protected $bucketTTL; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* AWS S3 region |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
protected $region; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* AWS remote path / object key |
60
|
|
|
* |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
protected $path; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* AWS acl |
67
|
|
|
* 'private' by default |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $acl; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Use multi part config |
75
|
|
|
* |
76
|
|
|
* @var boolean |
77
|
|
|
*/ |
78
|
|
|
protected $multiPartUpload; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Min multi part upload size |
82
|
|
|
* |
83
|
|
|
* @var int |
84
|
|
|
*/ |
85
|
|
|
protected $minMultiPartUploadSize = 5242880; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Max stream upload size |
89
|
|
|
* |
90
|
|
|
* @var int |
91
|
|
|
*/ |
92
|
|
|
protected $maxStreamUploadSize = 104857600; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Configure the sync. |
96
|
|
|
* |
97
|
|
|
* @see \phpbu\App\Backup\Sync::setup() |
98
|
|
|
* @param array $config |
99
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
100
|
|
|
*/ |
101
|
9 |
|
public function setup(array $config) |
102
|
|
|
{ |
103
|
9 |
|
if (!class_exists('\\Aws\\S3\\S3Client')) { |
104
|
|
|
throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// check for mandatory options |
108
|
9 |
|
$this->validateConfig($config); |
109
|
|
|
|
110
|
4 |
|
$this->key = $config['key']; |
111
|
4 |
|
$this->secret = $config['secret']; |
112
|
4 |
|
$this->bucket = $config['bucket']; |
113
|
4 |
|
$this->bucketTTL = Arr::getValue($config, 'bucketTTL'); |
114
|
4 |
|
$this->region = $config['region']; |
115
|
4 |
|
$this->path = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])); |
116
|
4 |
|
$this->acl = Arr::getValue($config, 'acl', 'private'); |
117
|
4 |
|
$this->multiPartUpload = Str::toBoolean(Arr::getValue($config, 'useMultiPartUpload'), false); |
118
|
4 |
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Make sure all mandatory keys are present in given config. |
122
|
|
|
* |
123
|
|
|
* @param array $config |
124
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
125
|
|
|
*/ |
126
|
9 |
|
protected function validateConfig(array $config) |
127
|
|
|
{ |
128
|
9 |
View Code Duplication |
foreach (['key', 'secret', 'bucket', 'region', 'path'] as $option) { |
129
|
9 |
|
if (!Arr::isSetAndNotEmptyString($config, $option)) { |
130
|
9 |
|
throw new Exception('AWS S3 ' . $option . ' is mandatory'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
4 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Execute the sync |
137
|
|
|
* |
138
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
139
|
|
|
* @param \phpbu\App\Backup\Target $target |
140
|
|
|
* @param \phpbu\App\Result $result |
141
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
142
|
|
|
*/ |
143
|
|
|
abstract public function sync(Target $target, Result $result); |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Simulate the sync execution. |
147
|
|
|
* |
148
|
|
|
* @param \phpbu\App\Backup\Target $target |
149
|
|
|
* @param \phpbu\App\Result $result |
150
|
|
|
*/ |
151
|
1 |
|
public function simulate(Target $target, Result $result) |
152
|
|
|
{ |
153
|
1 |
|
$result->debug( |
154
|
1 |
|
'sync backup to Amazon S3' . PHP_EOL |
155
|
1 |
|
. ' region: ' . $this->region . PHP_EOL |
156
|
1 |
|
. ' key: ' . $this->key . PHP_EOL |
157
|
1 |
|
. ' secret: ********' . PHP_EOL |
158
|
1 |
|
. ' location: ' . $this->bucket |
159
|
|
|
); |
160
|
1 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Should multi part upload be used. |
164
|
|
|
* |
165
|
|
|
* @param \phpbu\App\Backup\Target $target |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
protected function useMultiPartUpload(Target $target) |
169
|
|
|
{ |
170
|
|
|
// files bigger 5GB have to be uploaded via multi part |
171
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
172
|
|
|
return ( |
173
|
|
|
$target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload |
174
|
|
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Execute the remote clean up if needed |
179
|
|
|
* |
180
|
|
|
* @param \phpbu\App\Backup\Target $target |
181
|
|
|
* @param \phpbu\App\Result $result |
182
|
|
|
*/ |
183
|
|
|
public function cleanup(Target $target, Result $result) |
184
|
|
|
{ |
185
|
|
|
// TODO: Implement cleanup() method. |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|