A simple and lightweight MongoDB backup solution using the official mongodump
utility. This tool creates backups of your MongoDB databases and optionally uploads them to S3.
- Uses official MongoDB tools (
mongodump
) - Simple shell script implementation (see backup.sh)
- Optional S3 backup storage
- Docker support for easy deployment
- Supports MongoDB Atlas and self-hosted MongoDB instances via connection string
- Docker (if using the Docker image)
- MongoDB connection string (for MongoDB Atlas or your MongoDB instance)
- AWS credentials (optional, only if using S3 backup)
-
Create a
.env
file based on the example:cp .env.example .env
-
Edit the
.env
file with your MongoDB connection string:MONGODB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<authdb>?retryWrites=true&w=majority
-
Create a backups directory:
mkdir -p backups
-
Build the Docker image:
docker build -t mongo-backup .
-
Run the container:
docker run -d \ --name mongo-backup \ -v $(pwd)/backups:/backup \ --env-file .env \ mongo-backup
MONGODB_URI
: MongoDB connection string (required)S3_BUCKET
: S3 bucket name for backup storage (optional)S3_PREFIX
: Prefix for S3 backup files (optional, defaults to "mongodb-backups")AWS_ACCESS_KEY_ID
: AWS access key (optional, required for S3)AWS_SECRET_ACCESS_KEY
: AWS secret key (optional, required for S3)AWS_DEFAULT_REGION
: AWS region (optional, defaults to "us-east-1")
This tool is built around a simple shell script (backup.sh) that uses the official MongoDB mongodump
utility. The process is straightforward:
- Creates a timestamped backup directory
- Runs
mongodump
to create the backup - Compresses the backup into a tar.gz file
- Optionally uploads to S3 if configured
- Cleans up temporary files
You can also use the backup script directly if you have MongoDB tools installed:
# Make the script executable
chmod +x backup.sh
# Run the backup
./backup.sh
To enable S3 backup storage:
- Set up your AWS credentials in the
.env
file - Configure the S3 bucket and prefix
- The backups will be automatically uploaded to your S3 bucket
MIT