|
| 1 | +# MongoDB Backup Tool |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Uses official MongoDB tools (`mongodump`) |
| 8 | +- Simple shell script implementation (see [backup.sh](backup.sh)) |
| 9 | +- Optional S3 backup storage |
| 10 | +- Docker support for easy deployment |
| 11 | +- Supports MongoDB Atlas and self-hosted MongoDB instances via connection string |
| 12 | + |
| 13 | +## Prerequisites |
| 14 | + |
| 15 | +- Docker (if using the Docker image) |
| 16 | +- MongoDB connection string (for MongoDB Atlas or your MongoDB instance) |
| 17 | +- AWS credentials (optional, only if using S3 backup) |
| 18 | + |
| 19 | +## Quick Start |
| 20 | + |
| 21 | +1. Create a `.env` file based on the example: |
| 22 | + ```bash |
| 23 | + cp .env.example .env |
| 24 | + ``` |
| 25 | + |
| 26 | +2. Edit the `.env` file with your MongoDB connection string: |
| 27 | + ``` |
| 28 | + MONGODB_URI=mongodb+srv://<username>:<password>@<cluster>.mongodb.net/<authdb>?retryWrites=true&w=majority |
| 29 | + ``` |
| 30 | + |
| 31 | +3. Create a backups directory: |
| 32 | + ```bash |
| 33 | + mkdir -p backups |
| 34 | + ``` |
| 35 | + |
| 36 | +4. Build the Docker image: |
| 37 | + ```bash |
| 38 | + docker build -t mongo-backup . |
| 39 | + ``` |
| 40 | + |
| 41 | +5. Run the container: |
| 42 | + ```bash |
| 43 | + docker run -d \ |
| 44 | + --name mongo-backup \ |
| 45 | + -v $(pwd)/backups:/backup \ |
| 46 | + --env-file .env \ |
| 47 | + mongo-backup |
| 48 | + ``` |
| 49 | + |
| 50 | +## Environment Variables |
| 51 | + |
| 52 | +- `MONGODB_URI`: MongoDB connection string (required) |
| 53 | +- `S3_BUCKET`: S3 bucket name for backup storage (optional) |
| 54 | +- `S3_PREFIX`: Prefix for S3 backup files (optional, defaults to "mongodb-backups") |
| 55 | +- `AWS_ACCESS_KEY_ID`: AWS access key (optional, required for S3) |
| 56 | +- `AWS_SECRET_ACCESS_KEY`: AWS secret key (optional, required for S3) |
| 57 | +- `AWS_DEFAULT_REGION`: AWS region (optional, defaults to "us-east-1") |
| 58 | + |
| 59 | +## How It Works |
| 60 | + |
| 61 | +This tool is built around a simple shell script ([backup.sh](backup.sh)) that uses the official MongoDB `mongodump` utility. The process is straightforward: |
| 62 | + |
| 63 | +1. Creates a timestamped backup directory |
| 64 | +2. Runs `mongodump` to create the backup |
| 65 | +3. Compresses the backup into a tar.gz file |
| 66 | +4. Optionally uploads to S3 if configured |
| 67 | +5. Cleans up temporary files |
| 68 | + |
| 69 | +## Manual Usage (Without Docker) |
| 70 | + |
| 71 | +You can also use the backup script directly if you have MongoDB tools installed: |
| 72 | + |
| 73 | +```bash |
| 74 | +# Make the script executable |
| 75 | +chmod +x backup.sh |
| 76 | + |
| 77 | +# Run the backup |
| 78 | +./backup.sh |
| 79 | +``` |
| 80 | + |
| 81 | +## S3 Backup (Optional) |
| 82 | + |
| 83 | +To enable S3 backup storage: |
| 84 | +1. Set up your AWS credentials in the `.env` file |
| 85 | +2. Configure the S3 bucket and prefix |
| 86 | +3. The backups will be automatically uploaded to your S3 bucket |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +MIT |
0 commit comments