Database Backups with Cloudflare R2

SHARE
Methodology
In a world where everyone is trying to sell you something, hosting and deployment costs can quickly add up. As the DevOps POC for a project, I found the cost of a managed database solution hard to justify.
Combining my recent discovery of Cloudflare R2 (a very generously priced object storage solution) with the need to avoid a $15 DB solution, I created a DB backup tool that sends backups to Cloudflare R2. With the first 10GB free and an implementation to delete old backups, this solution might just be free forever.
In the event of a server attack, having a backup of your data is crucial. While servers can be easily rebuilt, data recovery is more challenging. This approach aligns with security best practices, ensuring that your server and data are protected. Additionally, it provides an extra layer of security by keeping a backup on an external source. For more information on securing your server, check out my tips here
The Codebase
Enough talk, let's dive into the code. You can find the project here
Version 0.0
I built this tool using Bun and Elysia, a Bun backend framework. I chose this stack to experience Bun's renowned speed firsthand.
The code consists of three main controllers:
- Database backup
- Directory archiving
- R2 upload (supporting both databases and directories)
It uses environment variables for configuration, including cron frequency and R2 account keys. Currently, it only supports MySQL databases, but work is in progress to support other major DB types.
Installation
cd /var/www/scripts/
git clone https://github.com/dej10/r2-backup.git
cd r2-backup
pm2 start --interpreter ~/.bun/bin/bun index.ts --name db-backup-worker
clone the repo & run with your favourite js process manager; I recommend PM2
Wrapping up
This R2 Backup tool offers a cost-effective solution for database backups, leveraging Cloudflare R2's generous free tier. Automating the backup process and implementing old backup deletion provides a potentially free-forever backup strategy for small to medium-sized projects.
As the project evolves, future updates may include support for additional database types, enhanced security features, and more granular control over backup retention policies. This tool demonstrates that with a bit of creativity and the right technologies, it's possible to build efficient, low-cost solutions for common DevOps challenges.