Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Adapted from [alonhar/lambda-mongodb-s3-backup](https://github.com/alonhar/lambd

`mongodump` binary is version 4.0.5 (linux-x86_64-amazon).

This will write its logs to CloudWatch and can optionally also write to slack (see Environment Variables below).

___

## Setup instructions
Expand Down Expand Up @@ -39,3 +41,5 @@ ___
| S3_BUCKET | Name of the S3 bucket | Yes |
| S3_STORAGE_CLASS | S3 storage class | No, default is Standard |
| DATE_FORMAT | Backup file name is in the format `[MONGO_DB_NAME]_[DATE_FORMAT]`. For possible date formatting options, refer to [DAY.JS](https://github.com/iamkun/dayjs/blob/master/docs/en/API-reference.md#format) | No. Default is `YYYYMMDD_HHmmss` |
| SLACK_KEY | A slack API key obtained from your slack account (looks like xoxp-xxxxxxxxxxx-xxxxxxxxxxx-xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) | No |
| SLACK_CHANNEL | The slack channel to write to | No |
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const url = require('url');
const dayjs = require('dayjs');
const ZipFolder = require('zip-a-folder');
const exec = require('child_process').exec;
const { WebClient } = require("@slack/web-api");

// ENVIRONMENT VARIABLES
// Mongo
Expand All @@ -20,6 +21,9 @@ const clusterShard = process.env.MONGO_CLUSTER_SHARD;
const bucketName = process.env.S3_BUCKET;
const storageClass = process.env.S3_STORAGE_CLASS || "STANDARD";
const s3bucket = new AWS.S3({ params: { Bucket: bucketName, StorageClass: storageClass } });
// Slack
const slackKey = process.env.SLACK_KEY;
const slackChannel = process.env.SLACK_CHANNEL;

const dateFormat = process.env.DATE_FORMAT || 'YYYYMMDD_HHmmss';

Expand All @@ -34,25 +38,25 @@ module.exports.handler = function(event, context, cb) {
exec(`mongodump -d ${dbName} -u ${username} -p ${password} -o ${folderName} --authenticationDatabase ${authDB} --ssl --port ${port} -h "${replicaSet}/${clusterShard}"`, (error, stdout, stderr) => {

if (error) {
console.log('Mongodump failed: ' + error);
logMessage('Mongodump failed: ' + error)
return;
}

ZipFolder.zipFolder(folderName, filePath, function(err) {
if (err) {
console.log('ZIP failed: ', err);
logMessage('ZIP failed: ', err);
} else {
fs.readFile(filePath, function(err, data) {
s3bucket.upload({ Key: fileName + '.zip', Body: data, ContentType: 'application/zip' }, function(err, data) {
fs.unlink(filePath, function(err) {
if (err) {
console.log('Could not delete temp file: ' + err);
logMessage('Could not delete temp file: ' + err);
}
});
if (err) {
console.log('Upload to S3 failed: ' + err);
logMessage('Upload to S3 failed: ' + err)
} else {
console.log('Backup completed successfully');
logMessage('Backup completed successfully');
}
});
});
Expand All @@ -62,3 +66,22 @@ module.exports.handler = function(event, context, cb) {
});

};

const logMessage = async (message) => {
try {
console.log(message)

if (slackKey && slackChannel) {
const web = new WebClient(slackKey);
return await web.chat.postMessage({
channel: slackChannel,
text: message,
icon_emoji: ":cat:",
as_user: false,
username: "MongoBackupBot"
});
}
} catch (err) {
console.log(err);
}
};
Loading