Logo ### MongoDB MongoDB + Backups & Retention [![Version](https://img.shields.io/badge/dynamic/json.svg?label=Version&style=for-the-badge&url=https://git.zakscode.com/api/v1/repos/ztimson/mongodb/tags&query=$[0].name)](https://git.zakscode.com/ztimson/mongodb/tags) [![Pull Requests](https://img.shields.io/badge/dynamic/json.svg?label=Pull%20Requests&style=for-the-badge&url=https://git.zakscode.com/api/v1/repos/ztimson/mongodb&query=open_pr_counter)](https://git.zakscode.com/ztimson/mongodb/pulls) [![Issues](https://img.shields.io/badge/dynamic/json.svg?label=Issues&style=for-the-badge&url=https://git.zakscode.com/api/v1/repos/ztimson/mongodb&query=open_issues_count)](https://git.zakscode.com/ztimson/mongodb/issues) ---
DocumentationRelease NotesReport a BugRequest a Feature
---
## Table of Contents - [MongoDB](#top) - [About](#about) - [Environment Variables](#environment-variables) - [Built With](#built-with) - [Setup](#setup) - [Production](#production) ## About Custom MongoDB Docker image extending `mongo:latest` with automated backups, retention management, and replica set configuration. Includes cron-based scheduled `mongodump` operations with compression, automatic stale lock recovery on unclean shutdowns, and zero-config replica set initialization — while preserving all native MongoDB functionality. **Manually trigger a backup:** docker exec -t mongodb backup **Restore a backup:** Decompress the archive and use the official `mongorestore` tool. ### Environment Variables | Variable | Description | Default | Example | |------------------------------|----------------------------------------------------------------------|--------------------------|-----------------------| | `BACKUP_CRON` | Cron schedule expression | | `0 */6 * * *` | | `BACKUP_DIR` | Directory to store backups | `/data/backups` | `/backups` | | `BACKUP_DB` | Database to backup | `$MONGO_INITDB_DATABASE` | `admin` | | `BACKUP_RETENTION` | Number of backups to keep, defaults to unlimited | | `7` | | `MONGO_INITDB_DATABASE` | Default database name | | `momentum` | | `MONGO_INITDB_ROOT_USERNAME` | Root username for mongodump auth | | `root` | | `MONGO_INITDB_ROOT_PASSWORD` | Root password for mongodump auth | | `secret` | | `REPLICA` | Comma-separated list of replica hostnames to init/join a replica set | | `db1:27017,db2:27017` | ### Built With [![Docker](https://img.shields.io/badge/Docker-384d54?style=for-the-badge&logo=docker)](https://docker.com/) [![MongoDB](https://img.shields.io/badge/mongodb-000000?style=for-the-badge&logo=mongodb)](https://www.mongodb.com/) ## Setup

Production

#### Prerequisites - [Docker](https://docs.docker.com/install/) #### Instructions 1. Run the docker image: ```shell docker run -d \ --name mongodb \ --restart unless-stopped \ -p 27017:27017 \ -e MONGO_INITDB_DATABASE=myapp \ -e BACKUP_CRON="0 3 * * *" \ -e BACKUP_RETENTION=7 \ -v ./mongodb/data:/data/db \ -v ./mongodb/backups:/data/backups \ ztimson/mongodb:latest ```