←Back to Work

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.

GoMongoDBGoogle Drive APIDockerCoolify

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

  1. Select a backup.
  2. Enter target MongoDB URL/database.
  3. Type RESTORE.
  4. 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

  1. Push the repository to GitHub.
  2. Create a Coolify application.
  3. Configure environment variables/secrets.
  4. Deploy.
  5. Expose WEB_PORT if the dashboard should be public.

Troubleshooting

docker run --rm <image> mongodump --version

Check:

  • Coolify logs
  • MongoDB connectivity
  • Google Drive/OAuth configuration
  • mongodump availability
  • BACKUP_TIMEZONE
  • Port conflicts
  • mongodb_dump_failed
  • drive_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.