Created
January 14, 2026 10:39
-
-
Save kehers/fe868aeb3d4c93a82ea8fbedf3b5885f to your computer and use it in GitHub Desktop.
Mongodb to S3 backup.
This file contains hidden or 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
| #!/bin/bash | |
| DB_NAME="mydb" | |
| TIMESTAMP=$(date +%Y%m%d_%H%M%S) | |
| S3_PATH="s3://my-bucket/mongodb-backup/backup-$TIMESTAMP.archive.gz" | |
| mongodump \ | |
| --host=localhost \ | |
| --port=27017 \ | |
| --username=username \ | |
| --password=%DBpassw0rd% \ | |
| --authenticationDatabase=admin \ | |
| --readPreference=secondary \ | |
| --db=$DB_NAME \ | |
| --excludeCollection=dontbackup \ | |
| --archive | gzip | aws s3 cp - $S3_PATH |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Prerequisite: Install
awscliand authenticate it. Best way to authenticate is to create an EC2 IAM role with a permission that grants S3 upload access to the required bucket and then add that IAM Role to the EC2 instance you will run this from.