Sub topic 11
Backing up databases consistently
Assume you can use the Linux command line, SSH, file permissions, and systemd. You also need PostgreSQL familiarity and an initialized restic or Borg repository.
Why copying database files is unsafe
A database changes several files during one transaction. A file-level backup can copy some files before the transaction and others after it. The resulting files may not describe one consistent database state.
Use a database-aware export instead. A logical dump asks the database server to produce a consistent sequence of SQL statements or database records. Back up that dump with restic or Borg.
flowchart TD
A[PostgreSQL database] --> B[Consistent logical dump]
B --> C[restic or Borg backup]
C --> D[Encrypted remote repository]
D --> E[Restore dump to scratch database]
E --> F[Verify data and application behavior]Choose the database export method
The correct command depends on the database engine and storage engine.
| Database | Typical consistent method | Important condition |
|---|---|---|
| PostgreSQL | `pg_dump` or `pg_dumpall` | Connect with a role allowed to read the database |
| MySQL or MariaDB | `mysqldump --single-transaction` | This provides a consistent view for transactional engines such as InnoDB |
| SQLite | Copy while using SQLite backup tools | Do not copy a changing database file blindly |
pg_dumpall exports all PostgreSQL databases and global objects such as roles. For one database, pg_dump usually produces a smaller and easier-to-restore backup.
Stream a PostgreSQL dump into restic
Streaming sends the dump directly to the repository without leaving an unencrypted temporary SQL file on the server.
sudo -u postgres pg_dumpall |
restic backup --stdin --stdin-filename pg_dumpall.sqlThe shell pipe sends the output of pg_dumpall to restic's standard input. The --stdin-filename option gives the stored item a useful name.
This command assumes that RESTIC_REPOSITORY and RESTIC_PASSWORD_FILE are already configured. It creates a restic snapshot containing the dump. The database dump is consistent at the time PostgreSQL creates it, even though the backup process may continue for several minutes.
For a single database, use a database-specific dump:
sudo -u postgres pg_dump --format=custom appdb |
restic backup --stdin --stdin-filename appdb.dumpThe custom format lets pg_restore restore selected objects or run jobs in parallel.
Restore into a scratch location
A backup is not useful until its dump can be read. First extract the dump without replacing a live database.
restic dump latest pg_dumpall.sql > /tmp/pg_dumpall.sqlThen inspect or restore it according to your database recovery plan. For a live recovery, create a clean PostgreSQL instance or a temporary database and run the dump there. Check that roles, schemas, tables, and important application queries work.
A Borg workflow follows the same principle. Create an archive with the dump on standard input:
sudo -u postgres pg_dumpall |
borg create --stdin-name pg_dumpall.sql \
"$BORG_REPO::{hostname}-{now:%Y-%m-%dT%H:%M:%S}"Common errors
- Backing up PostgreSQL data files while the server is running without using a database-aware backup method.
- Assuming a successful restic command proves that the SQL dump is restorable.
- Forgetting roles, permissions, extensions, or schemas when dumping only table data.
- Writing a dump into a world-readable temporary directory.
- Allowing a scheduled backup to overlap with another long-running dump.
Exercise
Why is a logical database dump safer than copying active database files, and what should you do after restoring the dump?
Show answer
The database creates the dump from a consistent view of its data. Restore it into a scratch database and verify schemas, roles, important records, and application queries before trusting the backup.