-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGetLatestBackupTask.php
320 lines (276 loc) · 9.47 KB
/
GetLatestBackupTask.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* @file GetLatestBackupTask.php
*
* Get the latest backup of a site from the Acquia Cloud API.
*
* @code
* <!-- Required parameters only -->
* <getAcquiaBackup dir="artifacts/backups" realm="devcloud" site="mysite" env="prod" />
* <!-- All parameters -->
* <getAcquiaBackup dir="artifacts/backups" realm="devcloud" site="mysite" env="test" database="multisite_db" maxAge="48" propertyName="database_backup_file" />
* @endcode
*
* @copyright 2018 Palantir.net, Inc.
*/
namespace TheBuild\Acquia;
use BuildException;
use PhingFile;
class GetLatestBackupTask extends AcquiaTask {
/**
* Directory for storing downloaded database backups.
*
* Required parameter.
* @var PhingFile
*/
protected $dir;
/**
* The Acquia Cloud hosting realm where the site is running.
* - Acquia Cloud Enterprise: 'prod'
* - Acquia Cloud Professional: 'devcloud'
*
* This also appears in a site's server names, as 'sitename.REALM.hosting.acquia.com'.
*
* Required parameter.
* @var string
*/
protected $realm;
/**
* The Acquia Cloud site account name.
*
* Required parameter.
* @var string
*/
protected $site;
/**
* The Acquia Cloud environment. Generally 'dev', 'test', or 'prod', unless
* a site has RA or other additional environments.
*
* Required parameter.
* @var string
*/
protected $env;
/**
* The name of the database whose backup to download. This will correspond
* with the site name unless your site uses multiple databases or you are
* running Drupal multisites.
*
* Optional parameter; defaults to matching $site.
* @var string
*/
protected $database;
/**
* Maximum age of the database backup in hours. If there is no backup matching
* this age in the current backups.json, the backups.json will be refreshed
* and the newest backup will be downloaded.
*
* Optional parameter.
* @var int
*/
protected $maxAge = 24;
/**
* Name of a property to populate with the path to the latest database backup.
*
* Optional parameter.
* @var string
*/
protected $propertyName;
/**
* Where to store the JSON list of database backups downloaded from the Acquia
* Cloud API. This is set to 'backups.json' in the directory specified by $dir.
* @var PhingFile
*/
protected $backupsFile;
/**
* @throws \IOException
* @throws \NullPointerException
*/
public function main() {
$this->validate();
// If the Acquia database name isn't set, default to using the site name.
if (empty($this->database)) {
$this->database = $this->site;
}
// Store the Acquia Cloud API JSON database backup records in our backups
// directory.
$this->backupsFile = new PhingFile($this->dir, "backups-{$this->site}-{$this->database}-{$this->env}.json");
// Check the database backup records for entries within our time window.
$backups = $this->getCurrentBackupRecords();
// Have we already downloaded any of the entries in our time window?
$downloaded_backups = [];
foreach ($backups as $backup) {
$filename = basename($backup['path']);
$file = new PhingFile($this->dir, $filename);
if ($file->exists()) {
$downloaded_backups[] = $backup;
}
}
// Pick out the newest current backup record, preferring already downloaded
// backups.
$newest_backup = FALSE;
if (!empty($downloaded_backups)) {
$newest_backup = end($downloaded_backups);
$this->log("Using previously downloaded backup from " . $this->formatBackupTime($newest_backup) . " ({$newest_backup['id']})");
}
elseif (!empty($backups)) {
$newest_backup = end($backups);
$this->log("Using backup from " . $this->formatBackupTime($newest_backup) . " ({$newest_backup['id']})");
}
// If we don't have a current enough backup record, check the API directly.
if (!$newest_backup) {
$this->downloadBackupRecords($this->backupsFile);
// Always return something, regardless of the time window.
$backups = $this->getBackupRecords($this->backupsFile);
$newest_backup = end($backups);
$this->log("Using backup from " . $this->formatBackupTime($newest_backup) . " ({$newest_backup['id']})");
}
// This means that we didn't have a current record in our backups json, and the Acquia Cloud API returned empty or
// malformed JSON.
if (empty($newest_backup)) {
throw new BuildException('Failed to find a backup record.');
}
// Download the backup if it does not yet exist on the filesystem.
$filename = basename($newest_backup['path']);
$file = new PhingFile($this->dir, $filename);
if (!$file->exists()) {
$this->log("Downloading the backup to " . $file->getAbsolutePath());
$this->downloadBackup($newest_backup, $file);
}
else {
$this->log("Existing backup found at " . $file->getAbsolutePath());
}
// Set the property value if a propertyName was provided.
if ($this->propertyName) {
$project = $this->getProject();
$project->setNewProperty($this->propertyName, $file->getAbsolutePath());
}
}
/**
* Download a backup from Acquia Cloud.
*
* @param array $backup
* @param PhingFile $destination
* @throws \HTTP_Request2_Exception
* @throws \HTTP_Request2_LogicException
*/
protected function downloadBackup(array $backup, PhingFile $destination) {
$stream = fopen($destination->getAbsolutePath(), 'wb');
if (!$stream) {
throw new BuildException('Can not write to ' . $destination->getAbsolutePath());
}
// Use an HTTP_Request2 with the Observer pattern in order to download large
// backups.
// @see HTTP/Request2/Observer/UncompressingDownload.php
// @see https://cloudapi.acquia.com/#GET__sites__site_envs__env_dbs__db_backups-instance_route
$request = $this->createRequest("/sites/{$this->realm}:{$this->site}/envs/{$this->env}/dbs/{$this->database}/backups/{$backup['id']}/download.json");
$request->setConfig('store_body', FALSE);
$observer = new \HTTP_Request2_Observer_UncompressingDownload($stream, 5000000000);
$request->attach($observer);
$response = $request->send();
fclose($stream);
$this->log("Downloaded " . $response->getHeader('content-length')/1000000 . "MB to " . $destination->getAbsolutePath());
}
/**
* Get backup records that are within the desired time window.
* @return array
*/
protected function getCurrentBackupRecords() {
try {
$backups = $this->getBackupRecords($this->backupsFile);
}
catch (BuildException $e) {
$backups = [];
}
$current_backups = [];
$threshold_time = new \DateTime("-{$this->maxAge} hours");
$backup_time = new \DateTime();
foreach ($backups as $backup) {
$backup_time->setTimestamp($backup['started']);
if ($backup_time > $threshold_time) {
$current_backups[] = $backup;
}
}
return $current_backups;
}
/**
* Get the array of backup records from the Acquia Cloud API JSON output,
* sorted from oldest to newest.
*
* @param $file
* @return array
* @throws BuildException
*/
protected function getBackupRecords($file) {
if ($file->exists()) {
$backups = json_decode($file->contents(), TRUE);
// If the backup records have loaded as an array, and the first record
// has the property that we're using, then it is *probably* valid data.
if (isset($backups[0]['started'])) {
// Sort the backups by start time so that the newest is always last.
usort($backups, function($a, $b) {
if ($a['started'] == $b['started']) { return 0; }
return ($a['started'] < $b['started']) ? -1 : 1;
});
return $backups;
}
elseif (count($backups) === 0) {
// The site might not have been backed up yet.
throw new BuildException('No Acquia Cloud backups found: ' . $file->getCanonicalPath());
}
}
throw new BuildException('Acquia Cloud backup records could not be loaded from JSON: ' . $file->getCanonicalPath());
}
/**
* Download the latest list of backup records from the Acquia Cloud API.
*/
protected function downloadBackupRecords(PhingFile $backups_file) {
$json = $this->getApiResponseBody("/sites/{$this->realm}:{$this->site}/envs/{$this->env}/dbs/{$this->database}/backups.json");
$writer = new \FileWriter($backups_file);
$writer->write($json);
}
/**
* Format the backup time to display in log messages.
*
* @param $backup
* @return string
*/
protected function formatBackupTime($backup) {
$time = new \DateTime('now');
$time->setTimestamp($backup['started']);
return $time->format(DATE_RFC850);
}
/**
* Setter functions.
*/
public function setRealm($value) {
$this->realm = $value;
}
public function setSite($value) {
$this->site = $value;
}
public function setEnv($value) {
$this->env = $value;
}
public function setDatabase($value) {
$this->database = $value;
}
public function setDir($value) {
$this->dir = new PhingFile($value);
}
public function setMaxAge($value) {
$this->maxAge = (int) $value;
}
public function setPropertyName($value) {
$this->propertyName = $value;
}
/**
* Verify that the required parameters are available.
*/
protected function validate() {
foreach (['dir', 'realm', 'site', 'env'] as $attribute) {
if (empty($this->$attribute)) {
throw new BuildException("$attribute attribute is required.", $this->location);
}
}
}
}