This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5150 from withspectrum/off-site-backups
Implement offsite backups
- Loading branch information
Showing
8 changed files
with
102 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// @flow | ||
import AWS from 'aws-sdk'; | ||
const https = require('https'); | ||
const { compose, COMPOSE_DEPLOYMENT_ID } = require('../utils/compose'); | ||
|
||
AWS.config.update({ | ||
accessKeyId: process.env.S3_TOKEN || 'asdf123', | ||
secretAccessKey: process.env.S3_SECRET || 'asdf123', | ||
apiVersions: { | ||
s3: 'latest', | ||
}, | ||
}); | ||
const s3 = new AWS.S3(); | ||
|
||
export default async () => { | ||
const backupListResult = await compose( | ||
`2016-07/deployments/${COMPOSE_DEPLOYMENT_ID}/backups` | ||
); | ||
|
||
const backupListJson = await backupListResult.json(); | ||
|
||
if (!backupListJson._embedded || !backupListJson._embedded.backups) { | ||
console.error( | ||
`Failed to load list of backups of deployment ${COMPOSE_DEPLOYMENT_ID}.` | ||
); | ||
return; | ||
} | ||
|
||
const newestBackup = backupListJson._embedded.backups | ||
.filter(backup => backup.is_downloadable) | ||
.sort((a, b) => new Date(b.created_at) - new Date(a.created_at))[0]; | ||
|
||
if (!newestBackup) { | ||
console.error('Failed to find latest backup.'); | ||
return; | ||
} | ||
|
||
const backupResult = await compose( | ||
`2016-07/deployments/${COMPOSE_DEPLOYMENT_ID}/backups/${newestBackup.id}` | ||
); | ||
const backupJson = await backupResult.json(); | ||
|
||
await new Promise((resolve, reject) => { | ||
https.get(backupJson.download_link, response => { | ||
s3.upload( | ||
{ | ||
Body: response, | ||
Bucket: `spectrum-chat/backups`, | ||
Key: backupJson.name, | ||
}, | ||
function(err) { | ||
if (err) { | ||
console.error(err); | ||
return reject(err); | ||
} | ||
return resolve(); | ||
} | ||
); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// @flow | ||
import fetch from 'node-fetch'; | ||
|
||
const COMPOSE_API_TOKEN = process.env.COMPOSE_API_TOKEN; | ||
|
||
export const COMPOSE_DEPLOYMENT_ID = '5cd38bcf9c5cab000b617356'; | ||
|
||
export const compose = (path: string, fetchOptions?: Object = {}) => { | ||
if (!COMPOSE_API_TOKEN) { | ||
throw new Error('Please specify the COMPOSE_API_TOKEN env var.'); | ||
} | ||
return fetch(`https://api.compose.io/${path}`, { | ||
...fetchOptions, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
...(fetchOptions.headers || {}), | ||
Authorization: `Bearer ${COMPOSE_API_TOKEN}`, | ||
}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters