This Docker image provides an automated mechanism to back up a MongoDB database to an S3-compatible object storage service. The script (run.sh) performs the following actions:
_FILE environment variables, as well as from a separate env.sh file.mongodump to create a gzip-compressed archive of your MongoDB database.Key Features:
SCHEDULE to run backups at specified cron times.SCHEDULE is not set, the container runs one backup and exits.mongo:6.0, mongo:7.0, or mongo:8.0.BACKUP_KEEP_DAYS.linux/amd64 and linux/arm64.You can provide configuration via environment variables directly or point them to files containing secrets. The env.sh file is sourced at runtime to centralize configuration defaults. Variables ending with _FILE take precedence if the referenced file is available.
Supported Variables:
| Variable | Description | Required | Default |
|---|---|---|---|
MONGODB_HOST | Hostname or IP address of the MongoDB server. | Yes | N/A |
MONGODB_HOST_FILE | File containing the MongoDB host. | No | N/A |
MONGODB_USER | MongoDB username for authentication. | No | N/A |
MONGODB_USER_FILE | File containing the MongoDB username. | No | N/A |
MONGODB_PASS | MongoDB password for authentication. | No | N/A |
MONGODB_PASS_FILE | File containing the MongoDB password. | No | N/A |
AWS_ACCESS_KEY_ID | AWS or S3-compatible access key ID. | Yes | N/A |
AWS_ACCESS_KEY_ID_FILE | File containing the AWS access key ID. | No | N/A |
AWS_SECRET_ACCESS_KEY | AWS or S3-compatible secret access key. | Yes | N/A |
AWS_SECRET_ACCESS_KEY_FILE | File containing the AWS secret access key. | No | N/A |
S3_BUCKET | S3 bucket name to store the backups. | Yes | N/A |
S3_PREFIX | S3 prefix/path inside the bucket (optional). | No | "" |
S3_REGION | AWS region or S3-compatible region (optional). | No | "" |
S3_ENDPOINT | Custom S3 endpoint URL (optional). | No | "" |
S3_ENDPOINT_FILE | File containing the S3 endpoint URL (optional). | No | "" |
BACKUP_KEEP_DAYS | Number of days to keep old backups before deletion. | No | 7 |
SCHEDULE | Cron syntax. If set, backups run on the defined schedule. If not set, one backup runs and exits. (optional). | No | "" |
docker run --rm \
-e MONGODB_HOST="mongo:27017" \
-e S3_BUCKET="my-s3-bucket" \
-e AWS_ACCESS_KEY_ID="your_access_key" \
-e AWS_SECRET_ACCESS_KEY="your_secret_key" \
aspian87/mongodb-backup-s3:6.0
To run daily at 23:30 on Monday, Wednesday, and Friday (30 23 * * 0,2,4 is UTC-based, adjust as needed):
docker run --rm \
-e MONGODB_HOST="mongo:27017" \
-e S3_BUCKET="my-s3-bucket" \
-e AWS_ACCESS_KEY_ID="your_access_key" \
-e AWS_SECRET_ACCESS_KEY="your_secret_key" \
-e SCHEDULE="30 23 * * 0,2,4" \
aspian87/mongodb-backup-s3:6.0
CAUTION: This drops all existing data in MongoDB!
docker exec <container_name> sh restore.sh
If your bucket has more than 1000 backups, the latest may not be the actual latest due to aws s3 ls pagination.
docker exec <container_name> sh restore.sh 20241012030000
This will download and restore mongodb-backup-20241012030000 from S3.
You can integrate this backup service alongside MongoDB in a docker-compose.yml:
version: "3.9"
services:
mongo:
image: mongo:6.0
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
ports:
- "27017:27017"
mongodb-backup:
image: aspian87/mongodb-backup-s3:6.0
environment:
MONGODB_HOST: "mongo:27017"
S3_BUCKET: "my-s3-bucket"
AWS_ACCESS_KEY_ID: "your_access_key"
AWS_SECRET_ACCESS_KEY: "your_secret_key"
# SCHEDULE: "30 23 * * 0,2,4" # Example schedule
BACKUP_KEEP_DAYS: "14"
depends_on:
- mongo
Adjust the tag (6.0, 7.0, 8.0) as needed.
run.sh provides detailed logging to aid debugging:
Contributions are welcome! Please open an issue or pull request for any improvements, bug fixes, or feature requests.
MIT License - Please see the LICENSE file for details.
Content type
Image
Digest
sha256:90ae94937…
Size
324.8 MB
Last updated
almost 2 years ago
docker pull aspian87/mongodb-backup-s3:8.0