Infrastructure
Mongo Drive Backup
Production-ready Go service that periodically backs up MongoDB databases and uploads them to Google Drive with scheduled backups, compression, and a web dashboard.
MongoDB Google Drive Backup Service
Production-ready Go service for scheduled and manual MongoDB backups to Google Drive, with realtime monitoring, retention, restore, Docker, and Coolify support.
Architecture
Features
- Scheduled and manual backups
- Google Drive upload
- OAuth 2.0 or Service Account + Shared Drive
- Automatic retention and cleanup
- Realtime WebSocket dashboard
- Backup progress and logs
- Google Drive backup browser
- Authenticated restore
- Graceful shutdown
- Docker & Coolify deployment
- Healthcheck endpoint
- Structured production logs
Requirements
- Go / Docker
- MongoDB
- Google Drive
- OAuth 2.0 or Google Workspace Shared Drive
Environment
MONGODB_URI=
MONGODB_DATABASE=
BACKUP_SCHEDULE=0 2 * * *
BACKUP_TIMEZONE=UTC
GOOGLE_DRIVE_FOLDER_ID=
GOOGLE_SHARED_DRIVE_ID=
GOOGLE_OAUTH_CREDENTIALS_FILE=
GOOGLE_OAUTH_CREDENTIALS_JSON=
GOOGLE_OAUTH_TOKEN_FILE=./token.json
GOOGLE_OAUTH_TOKEN_JSON=
GOOGLE_APPLICATION_CREDENTIALS=
GOOGLE_SERVICE_ACCOUNT_JSON=
BACKUP_RETENTION_DAYS=30
TEMP_BACKUP_DIR=/tmp/mongodb-backups
RUN_BACKUP_ON_START=false
WEB_PORT=8080
WEB_USERNAME=
WEB_PASSWORD=
OAuth 2.0
For personal Gmail:
GOOGLE_OAUTH_CREDENTIALS_JSON={"installed":{...}}
GOOGLE_OAUTH_TOKEN_JSON={"access_token":"...","refresh_token":"..."}
GOOGLE_OAUTH_CALLBACK_URL_PRODUCTION=https://your-domain.com/oauth2callback
Authorize through the dashboard or the authorization URL shown in logs.
Service Account
For Google Workspace:
GOOGLE_APPLICATION_CREDENTIALS=./secrets/service-account.json
GOOGLE_SHARED_DRIVE_ID=your-drive-id
GOOGLE_DRIVE_FOLDER_ID=your-folder-id
The service account must have access to the Shared Drive.
Run
go mod download
go run ./cmd/backup --once
Run continuously:
go run ./cmd/backup
Or:
./run.sh
./run.sh --once
Test
go test ./...
With coverage:
go test ./... -race -coverprofile=coverage.out
go tool cover -func=coverage.out
Dashboard
WEB_PORT=8080 go run ./cmd/backup
Open:
http://localhost:8080
The dashboard provides status, logs, backups, manual backup, restore, OAuth authorization, service control, and next-backup countdown.
When authentication is configured, the dashboard, API, WebSocket, and OAuth routes require login. /healthz remains public for health checks.
Restore
- Select a backup.
- Enter target MongoDB URL/database.
- Type
RESTORE. - Confirm.
The service safely extracts the archive and runs:
mongorestore --drop
MongoDB URLs are never logged or persisted.
Docker
docker build -t mongo-drive-backup .
docker run --rm --env-file .env mongo-drive-backup --once
The image includes mongodump, healthchecks, non-root execution, and graceful shutdown.
Coolify
- Push the repository to GitHub.
- Create a Coolify application.
- Configure environment variables/secrets.
- Deploy.
- Expose
WEB_PORTif the dashboard should be public.
Troubleshooting
docker run --rm <image> mongodump --version
Check:
- Coolify logs
- MongoDB connectivity
- Google Drive/OAuth configuration
mongodumpavailabilityBACKUP_TIMEZONE- Port conflicts
mongodb_dump_faileddrive_upload_failed
Security
- Never commit secrets.
- Use Coolify secrets.
- Never log credentials or MongoDB URLs.
- Keep OAuth tokens out of Git.
- Delete temporary backup files after upload.
- Protect publicly exposed dashboards with authentication.
- Verify restore targets before destructive operations.