Maintainer: Khelif Hadil
Project URL: https://roadmap.sh/projects/automated-backups
A scheduled GitHub Actions workflow that automatically backs up a MongoDB database every 12 hours and uploads the compressed backup to Backblaze B2 (S3-compatible storage). Includes a restore script to recover the database from any backup.
Database backups are critical to any production system. This project demonstrates how to automate the entire backup lifecycle — dump, compress, upload, and prune — without managing any additional infrastructure.
Built by Khelif Hadil as part of the roadmap.sh DevOps projects.
GitHub Actions (every 12 hours)
│
▼
mongodump → mydb_20260326_120000.tar.gz
│
▼
aws s3 cp → Backblaze B2 bucket
│
▼
Prune old backups (keep latest 10)
| Component | Tool |
|---|---|
| Database | MongoDB Atlas (free M0 tier) |
| Scheduler | GitHub Actions (cron) |
| Backup tool | mongodump (MongoDB Database Tools) |
| Storage | Backblaze B2 (S3-compatible, free tier) |
| Upload tool | AWS CLI (pointed at B2 endpoint) |
.
├── .github/
│ └── workflows/
│ └── db-backup.yml # Runs every 12h, dumps & uploads backup
├── scripts/
│ ├── backup.sh # Manual backup script (alternative to Actions)
│ └── restore.sh # Downloads latest backup from B2 & restores DB
├── .env.example # Environment variable template
└── README.md- Create a free M0 cluster at mongodb.com/atlas.
- Create a database user with read/write access.
- Under Network Access, allow connections from
0.0.0.0/0. - Copy your connection string:
mongodb+srv://<user>:<password>@cluster0.xxxxx.mongodb.net
- Create a free account at backblaze.com.
- Create a bucket (e.g.,
mongo-backups). - Generate an App Key with Read & Write access.
- Note your bucket endpoint (e.g.,
https://s3.us-east-005.backblazeb2.com).
Navigate to Settings > Secrets and variables > Actions and add:
| Secret | Description |
|---|---|
MONGO_URI |
Full MongoDB Atlas connection string |
DB_NAME |
Name of the database to back up |
R2_ACCESS_KEY_ID |
Backblaze B2 App Key ID |
R2_SECRET_ACCESS_KEY |
Backblaze B2 App Key |
R2_ENDPOINT |
Your B2 S3-compatible endpoint |
R2_BUCKET |
Your B2 bucket name |
The workflow runs automatically on this cron schedule:
on:
schedule:
- cron: '0 0,12 * * *' # Every day at 00:00 and 12:00 UTCTo restore the database from the latest backup:
chmod +x scripts/restore.sh
./scripts/restore.shThe script performs the following:
- Lists all backups in B2 and selects the latest one.
- Downloads and extracts the tarball.
- Prompts for confirmation before overwriting the database.
- Executes
mongorestore --dropfor a clean restoration.
The workflow automatically keeps only the 10 most recent backups. It deletes older files from the B2 bucket to ensure you stay within the free storage limits.
Khelif Hadil DevOps enthusiast and backend & database developer.
MIT