Automated MongoDB backup to AWS S3 with retention management. This service performs daily backups of your MongoDB database, uploads them to an S3 bucket, and maintains a 7-day retention policy. Supports both AWS S3 and S3-compatible storage services like DigitalOcean Spaces.
- Automated MongoDB backups using
mongodump - Compression of backups to save storage space
- Secure upload to AWS S3 or S3-compatible storage
- Configurable retention policy (default: 7 days)
- Built for Coolify deployment
- Node.js 22.11.0+
- MongoDB Database Tools (
mongodumpandmongorestore) - AWS S3 bucket or S3-compatible storage (DigitalOcean Spaces, Minio, etc.)
- AWS credentials with S3 access
Before using this tool, you need to install MongoDB Database Tools:
# Import the MongoDB public GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
# Add MongoDB repository
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
# Update package database
sudo apt-get update
# Install MongoDB Database Tools
sudo apt-get install -y mongodb-database-tools# Create a repository file
cat <<EOF | sudo tee /etc/yum.repos.d/mongodb-org-6.0.repo
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
EOF
# Install MongoDB Database Tools
sudo yum install -y mongodb-database-tools# Using Homebrew
brew tap mongodb/brew
brew install mongodb-database-toolsConfigure the service using environment variables:
MONGODB_URI: MongoDB connection stringAWS_ACCESS_KEY_ID: AWS access key or S3-compatible storage access keyAWS_SECRET_ACCESS_KEY: AWS secret key or S3-compatible storage secret keyS3_BUCKET_NAME: Name of the S3 bucket to store backupsAWS_REGION: AWS region (e.g.,us-east-1) - Required only when using AWS S3
S3_ENDPOINT_URL: Endpoint URL for S3-compatible storage (e.g.,https://ams3.digitaloceanspaces.comfor DigitalOcean Spaces)BACKUP_RETENTION_DAYS: Number of days to keep backups (default: 7)BACKUP_DIR: Local directory to store temporary backupsLOG_LEVEL: Logging level (default:info)LOG_FILE: Path to log file (default:mongodb-backup.log)
To use DigitalOcean Spaces or another S3-compatible storage provider instead of AWS S3:
-
Set the required environment variables:
AWS_ACCESS_KEY_ID=your_spaces_key AWS_SECRET_ACCESS_KEY=your_spaces_secret S3_BUCKET_NAME=your-spaces-name -
Set the S3 endpoint URL environment variable:
S3_ENDPOINT_URL=https://ams3.digitaloceanspaces.com -
The
AWS_REGIONvariable is optional when using a custom endpoint. If not provided, a default region will be used.
The application automatically detects that you're using S3-compatible storage instead of AWS S3 and configures the connection appropriately.
-
Clone this repository
-
Create a
.envfile based on.env.example -
Install dependencies:
npm install
-
Ensure MongoDB Database Tools are installed (see above)
-
Run the backup:
npm start
This service includes a Dockerfile for container deployment:
docker build -t mongodb-s3-backup .
docker run -d --env-file .env mongodb-s3-backupThe Dockerfile automatically installs the required MongoDB tools.
To deploy on Coolify:
- Add this repository to Coolify
- Configure the required environment variables
- Deploy the service
The service will run as a background process with a daily scheduled backup at midnight.
When deployed in Docker or Coolify, you can manually trigger a backup by executing:
docker exec <container_id> /app/run-backup.shTo restore a backup:
-
Download the backup from S3
-
Extract the tar.gz file
-
Use
mongorestoreto restore the database:mongorestore --uri="mongodb://username:password@host:port" <path_to_extracted_backup>
MIT