A CLI tool to backup and restore MongoDB databases using mongodump/mongorestore and Amazon S3.
- Node.js 18+
- MongoDB Database Tools (
mongodump,mongorestore) installed and available in PATH - AWS credentials with S3 read/write access
macOS:
brew tap mongodb/brew
brew install mongodb-database-toolsUbuntu/Debian (amd64):
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2204-x86_64-100.14.0.deb
sudo dpkg -i mongodb-database-tools-ubuntu2204-x86_64-100.14.0.debDocker: The mongodump tool is included in the official MongoDB images.
Other platforms: Refer to the official MongoDB documentation for installation instructions.
You can run commands directly without installing:
npx mbs3 dump
npx mbs3 restore
npx mbs3 listInstall globally to use anywhere:
npm install -g mbs3Then use directly:
mbs3 dump
mbs3 restore
mbs3 listnpm install mbs3Create a .env file in the directory where you run the command:
# MongoDB connection (required)
MONGODB_URI=mongodb://username:password@localhost:27017
MONGODB_DATABASE=your_database_name
# AWS S3 Configuration (required)
AWS_ACCESS_KEY_ID=your_access_key
AWS_SECRET_ACCESS_KEY=your_secret_key
S3_BUCKET=your-backup-bucket
# Optional: AWS region (default: us-east-1)
AWS_REGION=us-east-1
S3_PREFIX=backups/mongodb
# Optional: Custom S3-compatible endpoint (e.g., MinIO, DigitalOcean Spaces)
# AWS_ENDPOINT=https://your-custom-endpoint.com
# Optional: Custom mongodump/mongorestore path (if not in PATH)
# MONGODUMP_PATH=/usr/local/bin/mongodumpCreate a backup of the MongoDB database and upload to S3:
npx mbs3 dumpOptions:
-k, --keep-local- Keep local backup files after upload-B, --bucket <name>- S3 bucket name (overrides S3_BUCKET env var)-P, --prefix <path>- S3 prefix path (overrides S3_PREFIX env var)-D, --database <name>- MongoDB database name (overrides MONGODB_DATABASE env var)
# Basic dump
npx mbs3 dump
# Keep local backup files
npx mbs3 dump --keep-local
# Dump to a specific bucket and prefix
npx mbs3 dump --bucket my-backup-bucket --prefix backups/production
# Dump a specific database
npx mbs3 dump --database my-other-databaseRestore a MongoDB database from an S3 backup:
npx mbs3 restoreOptions:
-b, --backup <name>- Specific backup folder name to restore (defaults to most recent)-d, --drop- Drop existing collections before restore-k, --keep-local- Keep downloaded backup files after restore-B, --bucket <name>- S3 bucket name (overrides S3_BUCKET env var)-P, --prefix <path>- S3 prefix path (overrides S3_PREFIX env var)-D, --database <name>- MongoDB database name (overrides MONGODB_DATABASE env var)
# Restore most recent backup
npx mbs3 restore
# Restore specific backup
npx mbs3 restore --backup mydb-2024-01-15T10-30-00-000Z
# Drop existing collections and restore
npx mbs3 restore --drop
# Keep downloaded files after restore
npx mbs3 restore --keep-local
# Restore from a specific bucket and prefix
npx mbs3 restore --bucket my-backup-bucket --prefix backups/production
# Restore to a specific database
npx mbs3 restore --database my-other-database --dropList all available backups in S3:
npx mbs3 listOptions:
-B, --bucket <name>- S3 bucket name (overrides S3_BUCKET env var)-P, --prefix <path>- S3 prefix path (overrides S3_PREFIX env var)
# List backups from a specific bucket
npx mbs3 list --bucket my-backup-bucket
# List backups from a specific prefix
npx mbs3 list --prefix backups/productionDelete backups older than a specified number of days:
npx mbs3 pruneOptions:
-d, --days <number>- Delete backups older than this many days (default: 14, or PRUNE_DAYS env var)--dry-run- Preview what would be deleted without actually deleting-B, --bucket <name>- S3 bucket name (overrides S3_BUCKET env var)-P, --prefix <path>- S3 prefix path (overrides S3_PREFIX env var)
# Prune backups older than 14 days (default)
npx mbs3 prune
# Prune backups older than 7 days
npx mbs3 prune --days 7
# Preview what would be deleted (dry run)
npx mbs3 prune --dry-run
# Prune from a specific bucket and prefix
npx mbs3 prune --bucket my-backup-bucket --prefix backups/productionCheck the installed version:
npx mbs3 --versionGet help for any command:
npx mbs3 --help
npx mbs3 dump --help
npx mbs3 restore --helpBackups are:
- Created locally using
mongodumpwith gzip compression - Uploaded to S3 at:
s3://{bucket}/{prefix}/{database}-{timestamp}/ - Local files are cleaned up after successful upload (unless
--keep-localis specified)
s3://your-bucket/
└── backups/
└── mongodb/
└── your_database-2024-01-15T10-30-00-000Z/
└── your_database/
├── collection1.bson.gz
├── collection1.metadata.json.gz
├── collection2.bson.gz
└── collection2.metadata.json.gz
npm run build
docker build -t mbs3 .docker compose up -d mongodbdocker compose --profile backup run --rm backup# Install dependencies
npm install
# Run in development mode
npm run dev dump
npm run dev restore
npm run dev list
# Build the project
npm run build# Build and publish
npm publishMIT