Chapter 34. Database Administration, Replication, and Recovery
A database service fails differently from the services around it. A web worker that dies loses one request; a database that dies takes every request's data with it, and a database that comes back with different data than the one the application believed committed has silently corrupted the business record. The work of running a database is therefore mostly about durability and ownership: getting writes onto disk in a form that survives crashes, making copies that survive the loss of the machine, keeping a second machine current enough to take over, and making sure that at any instant exactly one machine accepts writes. Replication answers availability; backups answer deletion, corruption, and operator error - the two are not substitutes, and this chapter keeps them separate throughout.
Debian 13 packages two mature relational servers, PostgreSQL 17 and MariaDB 11.8, and the chapter works through both. PostgreSQL carries the longer arc: a standalone cluster is installed and secured with least-privilege application roles and TLS; maintenance tasks - locks, vacuum, statistics, updates - are exercised on real sessions; backups move from logical dumps to physical base backups plus a write-ahead log archive, which together enable restore to a chosen moment; streaming replication and controlled promotion are built by hand so the mechanisms are visible; then the same three nodes are rebuilt under Patroni and etcd, where leader leases, watchdogs, and role-aware HAProxy checks automate what the manual procedure made you do yourself. MariaDB then gets the same treatment in one pass: server configuration with network accounts and TLS, logical and physical backups, binary-log point-in-time recovery, GTID replication with a controlled promotion, and the diagnostics that keep a promoted server healthy. The last section connects the database back to the rest of the service: credentials, schema changes, health checks, and the monitoring that tells you a backup or a replica has quietly stopped being useful.
The chapter assumes the ground covered elsewhere: Chapter 15 defined recovery objectives, the copy ladder, and why a replica is not a backup; Chapter 32 built quorum, fencing, HAProxy, and the discipline of verifying a failover from an independent client; Chapter 8 and Chapter 9 covered systemd, journals, and process control. Where this chapter repeats a mechanism, it does so only far enough to use it on a database.
The examples use four Debian 13 guests on two isolated practice networks:
| Role | Host | Client address | Replication address |
|---|---|---|---|
| PostgreSQL, Patroni, etcd | db1 |
192.168.236.11 | 192.168.237.11 |
| PostgreSQL, Patroni, etcd | db2 |
192.168.236.12 | 192.168.237.12 |
| PostgreSQL, Patroni, etcd; MariaDB source | db3 |
192.168.236.13 | 192.168.237.13 |
HAProxy, psql and MariaDB client, MariaDB replica |
client1 |
192.168.236.14 | 192.168.237.14 |
Each guest also has a management address on a separate network that no
exercise touches. Client applications and HAProxy live on
192.168.236.0/24; PostgreSQL streaming replication, the Patroni REST
API, and etcd peer traffic live on 192.168.237.0/24, so a client
network problem cannot silently become a replication problem. The
guests are one-vCPU virtual machines on a single hypervisor, and db1
carries a disposable 2 GB virtio disk (/dev/vdb) that serves as the
WAL archive; the sizes, lag values, and recovery times printed in the
captures are measurements from this small lab, not sizing guidance.
Figure 34.1. The four practice hosts and the two networks between them. Client writes pass through HAProxy on client1 and reach whichever database node currently answers as primary; the role check that decides this uses the replication network, as does WAL streaming and the etcd quorum. Management SSH uses a third network, not shown.
Commands entered in john's ordinary shell begin with $; commands
entered in a root shell begin with #. The prompt marker is not
typed. The exercises assume john has the sudo access that
Chapter 5 establishes; the commands are
unchanged on a host where sudo asks for a password. Database server
processes run under their own unprivileged accounts - postgres for
PostgreSQL, mysql for MariaDB - and the examples invoke them with
sudo -u postgres or sudo -u mysql rather than switching the shell's
identity. If you do enter a root shell with sudo -i, leave it with
exit, and remember that root's ~ is not /home/john.
Keep the practice files in a named workspace on every host that needs one:
$ mkdir -p ~/debian-handbook/databases
The command normally prints nothing. Backups, dumps, scripts, and the
failover recorder all live under this directory; the examples never use
/tmp as a workspace, and cleanup stays bounded to what the exercises
create.
In this section
- 34.1. Planning a Database Service
- 34.2. Installing and Securing PostgreSQL
- 34.3. Maintaining PostgreSQL
- 34.4. PostgreSQL Backup and Point-in-Time Recovery
- 34.5. PostgreSQL Replication and Failover
- 34.6. Walkthrough: Automated PostgreSQL Failover with Patroni and Etcd
- 34.7. Testing and Maintaining Automated Database Failover
- 34.8. Operating MariaDB
- 34.9. Connecting Databases to Service Operations