Skip to content

Commit

Permalink
Initial version backups (#5)
Browse files Browse the repository at this point in the history
* Initial version backups

* Adding savefromremote:latest, updating readme

* Confirmation on deployment only

* Update order
  • Loading branch information
brad-ps authored Nov 22, 2023
1 parent 84daebd commit 63d6cf8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

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

`dep sitehost:backup`

`dep savefromremote:latest`

`dep savefromremote`

`dep savefromremote:db`
Expand All @@ -33,7 +37,7 @@ To keep normal set up - add to your docker file environment
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.
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
3. Rename your `deploy.php` to `deploy-backup.php`
4. Creaste new `deploy.php` and add below new code all.
4. Create new `deploy.php` and add below new code all.
```
<?php
Expand Down Expand Up @@ -167,6 +171,9 @@ dep deploy stage=prod --tag=1.0.1

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

Make a new backup, then save both db and assets from remote
`dep savefromremote:latest`

Save both db and assets from remote
`dep savefromremote`

Expand Down
96 changes: 96 additions & 0 deletions ps_silverstripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,95 @@



task('sitehost:backup', function () {
if (testLocally('[ -f /var/www/sitehost-api-key.txt ]')) {
$config = file_get_contents('/var/www/sitehost-api-key.txt');
set('sitehost_api_key', trim($config));
}

if (!get('sitehost_api_key')) {
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>');
return;
}

if (!get('sitehost_client_id')) {
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_client_id not set</error>');
return;
}

if (!get('sitehost_server_name')) {
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_server_name not set</error>');
return;
}

if (!get('sitehost_stack_name')) {
writeln('<error>SKIPPING SITEHOST BACKUP - sitehost_stack_name not set</error>');
return;
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sitehost.nz/1.2/cloud/stack/backup.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$body = array(
'apikey' => get('sitehost_api_key'),
'client_id' => get('sitehost_client_id'),
'server' => get('sitehost_server_name'),
'name' => get('sitehost_stack_name'),
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
writeln('<info>Trigger a containter backup {{sitehost_stack_name}} on {{sitehost_server_name}}</info>');

$backupResponse = curl_exec($ch);
$backupStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

writeln('<info>Response from Sitehost: ' . $backupResponse . '</info>');

curl_close($ch);

$backupResponse = json_decode($backupResponse, true);

// if response.status is true, start looping the job endpoint to wait for a completed response
if ($backupStatusCode == 200 && isset($backupResponse['return']) && isset($backupResponse['return']['job_id'])) {

writeln('<info>Waiting for backup to complete...</info>');

$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

$ch = curl_init();

// Loop until the job is completed
do {
sleep(5); //. Check every 5 seconds for completed backup

curl_setopt($ch, CURLOPT_URL, $job_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jobResponse = curl_exec($ch);
$jobStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

// Decode the response and check the status
$jobStatus = json_decode($jobResponse, true);

} while ($jobStatus['return']['state'] != 'Completed');

curl_close($ch);

writeln('<info>Backup completed</info>');

}
});



task('sitehost:deploymentbackup', function () {

if (askConfirmation('Do you want to run a backup before deploying?')) {
invoke("sitehost:backup");
} else {
writeln('Ok, skipping backup.');
}

})->select('stage=prod');



Expand Down Expand Up @@ -259,6 +348,12 @@
'savefromremote:assets'
]);

task('savefromremote:latest', [
'sitehost:backup',
'savefromremote:db',
'savefromremote:assets'
]);

/**
* Save DB from server.
* Grabs the most recent backup i.e. previous nights DB
Expand Down Expand Up @@ -329,6 +424,7 @@
desc('Deploy your project');
task('deploy', [
'confirm',
'sitehost:deploymentbackup',
'deploy:prepare',
'deploy:vendors',
// TODO: check if required 'deploy:clear_paths',
Expand Down

0 comments on commit 63d6cf8

Please sign in to comment.