Skip to content

Instantly share code, notes, and snippets.

@kehers
Created January 14, 2026 10:39
Show Gist options
  • Select an option

  • Save kehers/fe868aeb3d4c93a82ea8fbedf3b5885f to your computer and use it in GitHub Desktop.

Select an option

Save kehers/fe868aeb3d4c93a82ea8fbedf3b5885f to your computer and use it in GitHub Desktop.
Mongodb to S3 backup.
#!/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
@kehers
Copy link
Author

kehers commented Jan 14, 2026

Prerequisite: Install awscli and 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment