|
192 | 192 |
|
193 | 193 |
|
194 | 194 |
|
| 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'); |
195 | 284 |
|
196 | 285 |
|
197 | 286 |
|
|
259 | 348 | 'savefromremote:assets' |
260 | 349 | ]); |
261 | 350 |
|
| 351 | +task('savefromremote:latest', [ |
| 352 | + 'sitehost:backup', |
| 353 | + 'savefromremote:db', |
| 354 | + 'savefromremote:assets' |
| 355 | +]); |
| 356 | + |
262 | 357 | /** |
263 | 358 | * Save DB from server. |
264 | 359 | * Grabs the most recent backup i.e. previous nights DB |
|
329 | 424 | desc('Deploy your project'); |
330 | 425 | task('deploy', [ |
331 | 426 | 'confirm', |
| 427 | + 'sitehost:deploymentbackup', |
332 | 428 | 'deploy:prepare', |
333 | 429 | 'deploy:vendors', |
334 | 430 | // TODO: check if required 'deploy:clear_paths', |
|
0 commit comments