Skip to content

Commit 63d6cf8

Browse files
authored
Initial version backups (#5)
* Initial version backups * Adding savefromremote:latest, updating readme * Confirmation on deployment only * Update order
1 parent 84daebd commit 63d6cf8

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
`dep sitehost:prepare:deploy stage=uat --branch=master`
1515

16+
`dep sitehost:backup`
17+
18+
`dep savefromremote:latest`
19+
1620
`dep savefromremote`
1721

1822
`dep savefromremote:db`
@@ -33,7 +37,7 @@ To keep normal set up - add to your docker file environment
3337
1. Make sure you are running the latest docker image (you can pull this from inside docker desktop). deployer 7 currently works with 8.1 and 8.0 docker images.
3438
2. Install the new module `composer require --dev PlasticStudio/ps-deployer ` this now contains all the tasks that were present in the old deploy.php
3539
3. Rename your `deploy.php` to `deploy-backup.php`
36-
4. Creaste new `deploy.php` and add below new code all.
40+
4. Create new `deploy.php` and add below new code all.
3741
```
3842
<?php
3943
@@ -167,6 +171,9 @@ dep deploy stage=prod --tag=1.0.1
167171

168172
Make sure your `deployer.php` paths are set up correctly
169173

174+
Make a new backup, then save both db and assets from remote
175+
`dep savefromremote:latest`
176+
170177
Save both db and assets from remote
171178
`dep savefromremote`
172179

ps_silverstripe.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,95 @@
192192

193193

194194

195+
task('sitehost:backup', function () {
196+
if (testLocally('[ -f /var/www/sitehost-api-key.txt ]')) {
197+
$config = file_get_contents('/var/www/sitehost-api-key.txt');
198+
set('sitehost_api_key', trim($config));
199+
}
200+
201+
if (!get('sitehost_api_key')) {
202+
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_api_key not set - You may need to add a the sitehost-api-key.txt to your parent directory or update your docker image</error>');
203+
return;
204+
}
205+
206+
if (!get('sitehost_client_id')) {
207+
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_client_id not set</error>');
208+
return;
209+
}
210+
211+
if (!get('sitehost_server_name')) {
212+
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_server_name not set</error>');
213+
return;
214+
}
215+
216+
if (!get('sitehost_stack_name')) {
217+
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_stack_name not set</error>');
218+
return;
219+
}
220+
221+
$ch = curl_init();
222+
curl_setopt($ch, CURLOPT_URL, "https://api.sitehost.nz/1.2/cloud/stack/backup.json");
223+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
224+
curl_setopt($ch, CURLOPT_POST, 1);
225+
$body = array(
226+
'apikey' => get('sitehost_api_key'),
227+
'client_id' => get('sitehost_client_id'),
228+
'server' => get('sitehost_server_name'),
229+
'name' => get('sitehost_stack_name'),
230+
);
231+
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
232+
writeln('<info>Trigger a containter backup {{sitehost_stack_name}} on {{sitehost_server_name}}</info>');
233+
234+
$backupResponse = curl_exec($ch);
235+
$backupStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
236+
237+
writeln('<info>Response from Sitehost: ' . $backupResponse . '</info>');
238+
239+
curl_close($ch);
240+
241+
$backupResponse = json_decode($backupResponse, true);
242+
243+
// if response.status is true, start looping the job endpoint to wait for a completed response
244+
if ($backupStatusCode == 200 && isset($backupResponse['return']) && isset($backupResponse['return']['job_id'])) {
245+
246+
writeln('<info>Waiting for backup to complete...</info>');
247+
248+
$job_url = "https://api.sitehost.nz/1.2/job/get.json?apikey=" . get('sitehost_api_key') . "&job_id=" . $backupResponse['return']['job_id'] . "&type=scheduler"; // scheduler or daemon
249+
250+
$ch = curl_init();
251+
252+
// Loop until the job is completed
253+
do {
254+
sleep(5); //. Check every 5 seconds for completed backup
255+
256+
curl_setopt($ch, CURLOPT_URL, $job_url);
257+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
258+
$jobResponse = curl_exec($ch);
259+
$jobStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
260+
261+
// Decode the response and check the status
262+
$jobStatus = json_decode($jobResponse, true);
263+
264+
} while ($jobStatus['return']['state'] != 'Completed');
265+
266+
curl_close($ch);
267+
268+
writeln('<info>Backup completed</info>');
269+
270+
}
271+
});
272+
273+
274+
275+
task('sitehost:deploymentbackup', function () {
276+
277+
if (askConfirmation('Do you want to run a backup before deploying?')) {
278+
invoke("sitehost:backup");
279+
} else {
280+
writeln('Ok, skipping backup.');
281+
}
282+
283+
})->select('stage=prod');
195284

196285

197286

@@ -259,6 +348,12 @@
259348
'savefromremote:assets'
260349
]);
261350

351+
task('savefromremote:latest', [
352+
'sitehost:backup',
353+
'savefromremote:db',
354+
'savefromremote:assets'
355+
]);
356+
262357
/**
263358
* Save DB from server.
264359
* Grabs the most recent backup i.e. previous nights DB
@@ -329,6 +424,7 @@
329424
desc('Deploy your project');
330425
task('deploy', [
331426
'confirm',
427+
'sitehost:deploymentbackup',
332428
'deploy:prepare',
333429
'deploy:vendors',
334430
// TODO: check if required 'deploy:clear_paths',

0 commit comments

Comments
 (0)