dbtool-cli: Automating Database Ops with Go

dbtool-cli: Automating Database Ops with Go

Table of Contents

I created database users by hand for longer than I’d like to admit.

A developer would ask for a schema. I’d open a terminal, remember whether this one was PostgreSQL or MariaDB, look up the grant syntax again, and type it out. Ten minutes, twice a week, forever.

Backups were worse. I had shell scripts doing mysqldump on a cron, and they worked exactly as written: they kept every backup, forever, until a disk filled and something more important fell over at 3am.

Both problems were the same problem. Work a machine should have been doing.

So I wrote a CLI

dbtool-cli is a single command-line tool, written in Go with Cobra, that treats database administration as something you declare rather than something you remember.

Provisioning

One command creates a user, a schema, and scoped privileges. The driver differences live behind a Go interface, so PostgreSQL and MariaDB look identical from the outside.

New users get the narrowest privileges that let them do their job. Not because I’m strict, but because I’ve inherited enough databases where every application account was effectively root.

Backups that tell the truth

This is the part I care most about.

The tool verifies archive integrity before it reports success. A backup job that says “done” without checking is worse than no backup at all, because it buys you confidence you haven’t earned.

Retention is a policy, not a hope. Keep seven daily and four weekly, and the rotation happens on its own. Disk usage flattens out instead of climbing until something breaks.

Why Go

Three reasons, and portability was the big one.

It compiles to a single static binary. No runtime to install on a server, no dependency conflicts with whatever Python the OS happens to ship. Copy the file, run it.

It’s fast enough that nobody notices it running, which matters more than it sounds when a tool is invoked from cron on a box that’s already busy.

And it’s typed. My old bash scripts failed silently and creatively. Go fails at compile time, in my editor, where failures are cheap.

What changed

Developers provision their own environments now, which measured out at roughly a 30% gain in developer productivity. The real win isn’t the percentage though. It’s that the request never reaches me at all, so there’s no queue left to be slow.

Backup storage is flat and predictable. The retention policy handles it, so nobody has to remember to prune anything.

And there’s one command syntax regardless of which database is underneath, which sounds like a small convenience until you realise how much of the friction in operations work is not difficulty at all but the constant low-grade cost of holding two slightly different dialects in your head and picking the wrong one when you’re tired.

I no longer look up grant statements.

View the source on GitHub

call to action

Ready to build your next project with me?

I’m ready to help you build, improve, and launch your next project — just drop a message and let’s get started.

Get Started Now