1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Backup\Sync; |
3
|
|
|
|
4
|
|
|
use Aws\S3\S3Client; |
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
|
|
|
* Amazon S3 Sync base 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 3.0.0 |
21
|
|
|
*/ |
22
|
|
|
abstract class AmazonS3 implements Simulator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* AWS key |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $key; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* AWS secret |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $secret; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* AWS S3 bucket |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $bucket; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AWS S3 region |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
protected $region; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* AWS remote path / object key |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $path; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* AWS acl |
61
|
|
|
* 'private' by default |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $acl; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Use multi part config |
69
|
|
|
* |
70
|
|
|
* @var boolean |
71
|
|
|
*/ |
72
|
|
|
protected $multiPartUpload; |
73
|
|
|
|
74
|
6 |
|
/** |
75
|
|
|
* Min multi part upload size |
76
|
6 |
|
* |
77
|
|
|
* @var int |
78
|
|
|
*/ |
79
|
6 |
|
protected $minMultiPartUploadSize = 5242880; |
80
|
1 |
|
|
81
|
|
|
/** |
82
|
5 |
|
* Max stream upload size |
83
|
1 |
|
* |
84
|
|
|
* @var int |
85
|
4 |
|
*/ |
86
|
1 |
|
protected $maxStreamUploadSize = 104857600; |
87
|
|
|
|
88
|
3 |
|
/** |
89
|
1 |
|
* Configure the sync. |
90
|
|
|
* |
91
|
2 |
|
* @see \phpbu\App\Backup\Sync::setup() |
92
|
1 |
|
* @param array $config |
93
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
94
|
1 |
|
*/ |
95
|
1 |
|
public function setup(array $config) |
96
|
1 |
|
{ |
97
|
1 |
|
if (!class_exists('\\Aws\\S3\\S3Client')) { |
98
|
1 |
|
throw new Exception('Amazon SDK not loaded: use composer to install "aws/aws-sdk-php"'); |
99
|
1 |
|
} |
100
|
1 |
|
if (!Arr::isSetAndNotEmptyString($config, 'key')) { |
101
|
|
|
throw new Exception('AWS key is mandatory'); |
102
|
|
|
} |
103
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'secret')) { |
104
|
|
|
throw new Exception('AWS secret is mandatory'); |
105
|
|
|
} |
106
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'bucket')) { |
107
|
|
|
throw new Exception('AWS S3 bucket name is mandatory'); |
108
|
|
|
} |
109
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'region')) { |
110
|
|
|
throw new Exception('AWS S3 region is mandatory'); |
111
|
|
|
} |
112
|
|
|
if (!Arr::isSetAndNotEmptyString($config, 'path')) { |
113
|
|
|
throw new Exception('AWS S3 path / object-key is mandatory'); |
114
|
|
|
} |
115
|
|
|
$this->key = $config['key']; |
116
|
|
|
$this->secret = $config['secret']; |
117
|
|
|
$this->bucket = $config['bucket']; |
118
|
|
|
$this->region = $config['region']; |
119
|
|
|
$this->path = Str::withTrailingSlash(Str::replaceDatePlaceholders($config['path'])); |
120
|
|
|
$this->acl = Arr::getValue($config, 'acl', 'private'); |
121
|
|
|
$this->multiPartUpload = Str::toBoolean(Arr::getValue($config, 'useMultiPartUpload'), false); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Execute the sync |
126
|
|
|
* |
127
|
|
|
* @see \phpbu\App\Backup\Sync::sync() |
128
|
|
|
* @param \phpbu\App\Backup\Target $target |
129
|
|
|
* @param \phpbu\App\Result $result |
130
|
|
|
* @throws \phpbu\App\Backup\Sync\Exception |
131
|
|
|
*/ |
132
|
|
|
abstract public function sync(Target $target, Result $result); |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Simulate the sync execution. |
136
|
|
|
* |
137
|
|
|
* @param \phpbu\App\Backup\Target $target |
138
|
|
|
* @param \phpbu\App\Result $result |
139
|
|
|
*/ |
140
|
|
|
public function simulate(Target $target, Result $result) |
141
|
|
|
{ |
142
|
|
|
$result->debug( |
143
|
|
|
'sync backup to Amazon S3' . PHP_EOL |
144
|
|
|
. ' region: ' . $this->region . PHP_EOL |
145
|
|
|
. ' key: ' . $this->key . PHP_EOL |
146
|
|
|
. ' secret: ********' . PHP_EOL |
147
|
|
|
. ' location: ' . $this->bucket |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Should multi part upload be used. |
153
|
|
|
* |
154
|
|
|
* @param \phpbu\App\Backup\Target $target |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
protected function useMultiPartUpload(Target $target) |
158
|
|
|
{ |
159
|
|
|
// files bigger 5GB have to be uploaded via multi part |
160
|
|
|
// files uploaded with multi part upload has to be at least 5MB |
161
|
|
|
return ( |
162
|
|
|
$target->getSize() > $this->maxStreamUploadSize || $this->multiPartUpload |
163
|
|
|
) && $target->getSize() > $this->minMultiPartUploadSize; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|