Go has no built-in backup or disaster recovery feature, so backups are done by combining the database's own tools with Go code that schedules, verifies and monitors them. A Go program can run pg_dump or snapshot APIs, upload the result to object storage, and test restores. The Go standard library and ecosystem make writing that automation, and making your own services resilient, straightforward.
The best practices are to back up with the database's native tooling, keep copies off-site and encrypted, define recovery time and recovery point objectives, and test restores regularly.
Go is a language and runtime, not a data store. Your data lives in PostgreSQL, MySQL, object storage, files or a queue, and those systems own consistent backups. Go's role is threefold:
Start by defining two numbers. The recovery point objective (RPO) is how much data you can afford to lose; the recovery time objective (RTO) is how long you can be down. They decide whether nightly dumps are enough or you need continuous archiving.
Use the database's own consistent backup mechanism rather than copying files from a running server.
| Database | Logical backup | Point-in-time recovery |
|---|---|---|
| PostgreSQL | pg_dump / pg_dumpall | Base backup plus WAL archiving (pg_basebackup) |
| MySQL / MariaDB | mysqldump, mariadb-dump | Binary logs with a full backup |
| SQLite | VACUUM INTO 'file' or the online backup API | Litestream for continuous replication |
| MongoDB | mongodump | Replica set oplog, or managed snapshots |
Managed services (Amazon RDS, Cloud SQL and similar) provide automated snapshots and point-in-time recovery, which is usually the best choice for RPO and RTO.
This program runs pg_dump, compresses the output with gzip, and writes it to a timestamped file. It streams data and uses a timeout through context.
The file mode 0600 keeps the dump private, O_EXCL prevents overwriting an earlier backup, and the non-zero exit code lets cron, Kubernetes CronJobs or your scheduler alert on failure. Pass credentials through the environment (PGPASSWORD or a .pgpass file) rather than command-line arguments, which are visible in process lists.
A backup on the same disk as the database is not a backup. Copy artifacts to another failure domain, such as object storage in another region or account, using the AWS SDK for Go v2 or the equivalent client for your provider.
crypto/cipher AES-GCM or a tool like age.sha256) beside each file and verify it after upload and before restore.Disaster recovery also depends on how the Go service behaves when parts fail.
SIGTERM with signal.NotifyContext and http.Server.Shutdown, finishing in-flight requests.context deadlines.An untested backup is a hope, not a plan. Schedule a job that restores the latest backup into a scratch database, runs sanity queries (row counts, a recent timestamp) and reports the result. Practise the full runbook, including failover to another region, at least twice a year and record how long it took against your RTO.
Yes, by querying and exporting rows, but it is slower and rarely consistent across tables unless you use a single transaction with a suitable isolation level. Native tools are safer.
Choose it from your RPO. If you can lose at most five minutes, you need continuous archiving such as WAL shipping; if a day is acceptable, a nightly dump may be enough.
No. Replication copies deletions and corruption to the replica too. You need point-in-time backups in addition to replicas.
Use a key management service or secrets manager, separate from the backups themselves, and document how to recover the keys during a disaster.
Go automates and hardens backups rather than replacing database tooling. Use native dump, snapshot and archiving features, run them from a Go job with timeouts and logging, store encrypted copies off-site with retention and versioning, build services that recover cleanly, and prove it all by restoring regularly.