15.6. Restoring Data and Services

Backups are made in a rush and restored in a bigger one. The way to keep restores correct under pressure is to make them small, staged, and verified: find the right version, restore into a staging area, check content and metadata before anything replaces live data, and rebuild services in dependency order. This section restores one file, a whole data tree, a service configuration file, and a database, ending with the checks that let a service reopen.

15.6.1. Finding Backup Versions and Restoring Individual Files

The recovery workflow begins in the catalog, not in the data. borg list REPOSITORY (Sections 15.4.1 and 15.5.4) names every archive with its timestamp and fingerprint; borg diff (Section 15.4.1) narrows a search to the archives around a change; and borg list REPOSITORY::ARCHIVE shows exactly what a chosen version contains. The archive listing for the version to restore here has the same shape as the one printed in Section 15.4.1:

# BORG_PASSPHRASE=handbook-pass borg list ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1::client1-2026-11-20T1400
drwxr-xr-x root   root          0 Sat, 2026-11-21 00:19:49 srv
drwxr-xr-x john   john          0 Sat, 2026-11-21 00:19:53 srv/appdata
...
-rw-r----- john   www-data      23 Sat, 2026-11-21 00:21:47 srv/appdata/reports/quarterly.txt
...

Selected output; the ... omits the unchanged srv/appdata subdirectories and the etc/postgresql and home/john entries, which match the listing in Section 15.4.1.

The stored path is srv/appdata/reports/quarterly.txt - no leading slash, because borg create stored the given paths relative. Extraction patterns must use that form. Restore the single file into a staging directory, on client1, as root:

# install -d -m 755 /srv/restore-one
# cd /srv/restore-one

Both commands are silent, and the cd is half the restore: borg extract writes below the current directory, so the staging choice is made before the command, not by an option.

Option Description
--list print each path as it is extracted
--numeric-ids apply stored numeric user and group IDs without name lookup
# BORG_PASSPHRASE=handbook-pass borg extract --list --numeric-ids ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1::client1-2026-11-20T1400 srv/appdata/reports/quarterly.txt
srv/appdata/reports/quarterly.txt

The archive's tree was rebuilt under the staging directory with one file in it. Prove the restore before using it:

# cmp /srv/appdata/reports/quarterly.txt /srv/restore-one/srv/appdata/reports/quarterly.txt

cmp prints nothing when the files are identical - the live file and the restored copy agree byte for byte (the live file happens to hold the same revision this archive recorded). Check what came back with it:

# stat -c "%U:%G %a %n" /srv/restore-one/srv/appdata/reports/quarterly.txt
john:www-data 640 /srv/restore-one/srv/appdata/reports/quarterly.txt

Ownership and mode returned intact, because the archive stored them and --numeric-ids applied the numbers directly. From this staging tree the file can be compared, copied into place deliberately, or discarded - nothing live was touched during any of it.

15.6.2. Recovering Ownership, Permissions, and Service Configuration

Whole-tree restores repeat the single-file pattern at scale. Restore the entire srv path from the same archive, on client1, as root:

# install -d -m 755 /srv/restore
# cd /srv/restore
# time env BORG_PASSPHRASE=handbook-pass borg extract --numeric-ids ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1::client1-2026-11-20T1400 srv

real    0m4.763s
user    0m0.344s
sys 0m0.063s

The staging commands are silent; time measures the extraction at 4.8 seconds for the tree in this environment. The baseline inspections from Section 15.2.1, run against the restored tree, are the acceptance test:

# stat -c "%U:%G %a %h %n" /srv/restore/srv/appdata/config/app.conf && getfacl -p /srv/restore/srv/appdata/config/app.conf && getfattr -d /srv/restore/srv/appdata/config/app.conf
www-data:www-data 640 2 /srv/restore/srv/appdata/config/app.conf
# file: /srv/restore/srv/appdata/config/app.conf
# owner: www-data
# group: www-data
user::rw-
user:john:r--
group::r--
mask::r--
other::---

# file: srv/restore/srv/appdata/config/app.conf
user.handbook="chapter15"

getfattr: Removing leading '/' from absolute path names

Every baseline property survived the round trip through the repository: www-data ownership, mode 640, the hard link count of 2, the ACL entry with its mask, and the extended attribute. A restore that passes this test produces files the application can use; one that fails it produces files that merely exist.

Service configuration restores follow the same discipline. Extract PostgreSQL's main configuration file - a different archive path, no staging subtleties, and a deliberate comparison against the live file:

# install -d -m 755 /srv/restore-config
# cd /srv/restore-config
# BORG_PASSPHRASE=handbook-pass borg extract --numeric-ids ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1::client1-2026-11-20T1400 etc/postgresql/17/main/postgresql.conf

The staging commands and the extraction are all silent. Compare the restored file with the running one:

# cmp /etc/postgresql/17/main/postgresql.conf /srv/restore-config/etc/postgresql/17/main/postgresql.conf

cmp prints nothing: the live configuration and the archived one are identical, which is the expected answer on a healthy system - the point of the comparison is that a difference has somewhere to be seen before the file is replaced. When a restore does change configuration, the sequence is: copy the file into place under /etc, then restart or reload the service so it takes effect, using the service-management procedures of Chapter 8; a copied postgresql.conf does nothing until PostgreSQL restarts.

15.6.3. Restoring Application Data in Dependency Order

A service is unusable until everything under it is back, and the order is fixed by the dependencies themselves:

Stage What it needs first Practice-system example
Storage nothing - recreate volumes and mounts the appdata logical volume, mounted at /srv/appdata
Configuration storage for files to land on /etc/postgresql, restored with the tree
Data storage plus configuration the inventory database, loaded from its dump
Service start all of the above PostgreSQL serving queries again

The database stage is where the export from Section 15.2.1 pays off: restore the dump file first, then let the server's own loader rebuild the database from it. Extract the tree that contains the dump - on client1, as root:

# install -d -m 755 /srv/restore-dump
# cd /srv/restore-dump
# BORG_PASSPHRASE=handbook-pass borg extract --numeric-ids ssh://borgsvc@backup1/var/lib/borgsvc/repos/client1::client1-2026-11-20T1400 srv

All three commands are silent. The dump now sits at the same relative path it was archived from. Check its integrity against the live one before loading it:

# sha256sum /srv/appdata/dumps/inventory.dump /srv/restore-dump/srv/appdata/dumps/inventory.dump
361aa5e15b1ce3cfebd81b82b87071fc8b562cccc15a8ba73e8917fabc0de347  /srv/appdata/dumps/inventory.dump
361aa5e15b1ce3cfebd81b82b87071fc8b562cccc15a8ba73e8917fabc0de347  /srv/restore-dump/srv/appdata/dumps/inventory.dump

Identical digests: the export survived storage, deduplication, and extraction unchanged. Load it into a fresh database, following the backup-and-restore conventions of pg_dump and pg_restore:

# sudo -u postgres psql -v ON_ERROR_STOP=1 -c "CREATE DATABASE inventory_restored;"
CREATE DATABASE
# sudo -u postgres pg_restore -d inventory_restored /srv/restore-dump/srv/appdata/dumps/inventory.dump

pg_restore prints nothing on success and populates the new database from the custom-format dump. (Restoring over an existing database instead would use pg_restore --clean, which drops and recreates each object - a destructive option that belongs only in a deliberate recovery, not a verification.) Count what came back:

# sudo -u postgres psql -d inventory_restored -c "SELECT count(*) FROM parts;"
 count 
-------
     3
(1 row)

Three rows, matching the export from Section 15.2.1. The whole sequence - extract, verify, create, load, count - is the dependency table above in five commands, and it is the same shape for any service with an export tool: dump on the way in, staged restore on the way out, the server's own loader doing the work.

15.6.4. Checking Restored Data Before Reopening a Service

The checks that matter are the ones a failure would break: content counts, checksums, metadata, service health, and finally the application's own behavior. Everything demonstrated in this section doubles as a pre-reopening checklist:

# systemctl is-active postgresql
active

The database service is running - it never stopped in this walkthrough, but on a recovery host this is the first question, and is-active answers it with one line. Around it, the checks already performed: the row count of SELECT count(*) FROM parts; (the data itself), the matching sha256sum digests of the dump before and after loading, the cmp comparison of the configuration file, and the metadata inspections against the Section 15.2.1 baseline.

Two practices complete the picture. First, keep the old data until the new one has passed its checks - the staged restores in this section never overwrote anything live, so "roll back" meant "stop and look again", not "restore a second time". Second, exercise the service the way its users do before reopening it: for a database, that means running the application's own queries against the restored data; for a web service, a request through the real stack. The practice database's three-row check is that principle in miniature - a query that would fail loudly if the restore were subtly wrong. Reopen, then remove the staging trees as bounded cleanup: the directories under /srv created in this section and Section 15.5 exist only to demonstrate restores, and the final cleanup at the end of this chapter lists them.