Easily backup your MongoDB database hosted on MongoDB Atlas using mongodump, AWS Lambda functions and S3 buckets.
MIT License
Easily backup your MongoDB database hosted on MongoDB Atlas using mongodump binary, AWS Lambda functions and S3 buckets and slack webhook for notifications.
Adapted from llangit/lambda-mongocluster-s3 🙏.
For a MongoDB Atlas cluster database backup, specify the URI command option like this:
--uri "mongodb+srv://[user]:[pass]@[host]/[name]"
mongodump
binary is version 100.7.4 (mongodb-database-tools-amazon2-x86_64-100.7.4).
You can download the latest binary of mongodump here : mongodb-database-tools. Be sure to download the version corresponding to your AWS Lambda configuration.
npm i
zip -r ../backup_code_function.zip *
Node.js 18.x
s3:PutObject
and s3:ListBucket
permissions for the S3 bucket that you want to back up to, as well as the AWSLambdaBasicExecutionRole
policycron(0 * * * ? *)
will trigger a backup every hour.To allow your Lambda function to connect to MongoDB Atlas, you need to provide it with internet access. To do this, associate a VPC with your function. Here is a great tutorial covering the steps: https://www.youtube.com/watch?v=Z3dMhPxbuG0
Steps:
Go to AWS VPC: Then click the "Create VPC" button.
Specify VPC details:
Create a Security Group: Once the VPC is created, create a security group and enable HTTP, HTTPS, and port 27017.
Associate Elastic IP: It is recommended to associate an elastic IP with your NAT gateway to whitelist it within your MongoDB Atlas configuration.
Test it !
A useful feature is receiving notifications to know if your backup was successfully executed. To do this, go to Slack and create a webhook associated with your workspace in a dedicated channel. A notification will be sent each time a backup request is triggered, informing you of its execution status.
Variable | Description | Required? |
---|---|---|
MONGODUMP_OPTIONS | Your mongodump command options separated by a space (without mongodump at the beginning), for instance --uri "mongodb+srv://[user]:[pass]@[host]/[name]" . Refer to the mongodump docs for a list of available options. Important: do not include the --out or -o option. |
Yes |
S3_BUCKET | Name of the S3 bucket | Yes |
S3_STORAGE_CLASS | S3 storage class for the backup. Refer to the S3 SDK docs for a list of available options. | No. Default is STANDARD
|
ZIP_FILENAME | Name of the ZIP archive | No. Default is mongodb_backup
|
FOLDER_PREFIX | Name of the Parent folder | No. Default is mongodb_backups
|
DATE_FORMAT | Will be appended to ZIP_FILENAME with a _ separator. Refer to the DayJS docs for a list of available formatting options. |
No. Default is YYYYMMDD_HHmmss
|
ENVIRONMENT | Specify your app environment for custom Slack notifications | No. Default is unknown
|
SLACK_WEBHOOK_URL | Your Slack webhook URL | No |
BACKUPS_TO_RETAIN | Number of backups to retain in S3. Older backups will be deleted. | No. Default is 10
|
And there you have it, you have just created your own backup system for your MongoDB database using AWS services. I strongly encourage you to deploy this system yourself instead of relying on subscription-based services or applications, as you will save money in the long run. Remember that AWS is a paid service, but with the free tier, you can get many operations free of charge each month. Be sure to monitor your credit usage.
Feel free to contribute !