Chapter 15. Backup and Restore

A backup is only as good as the restore it produces. Copies accumulate without effort; what takes design is making sure that a specific loss - a deleted file, a corrupted database, a failed disk, a compromised machine, a destroyed site - meets a copy that is consistent, findable, readable, and outside the reach of whatever caused the loss. This chapter builds that property step by step: it starts with recovery objectives and copy placement, prepares backup sources that are internally consistent, exercises plain file copies with rsync and tar, builds an encrypted, deduplicated, versioned repository with BorgBackup, arranges that repository so a compromised client cannot destroy it, and then restores single files, service data, and finally a whole system.

The walkthrough uses two Debian 13 machines on one management network:

Host Role Practice storage
client1 the system being backed up; PostgreSQL and the practice application data live here /dev/vdb (4 GiB) for an LVM data volume, /dev/vdc (4 GiB) as a spare recovery target
backup1 the repository host /dev/vdb (3 GiB) as an offline-copy disk

Every byte on the three practice disks is erased during the exercises. client1's running root filesystem is never destroyed: whole-system recovery is rehearsed onto /dev/vdc, so the machine keeps working throughout. Use disposable disks or a disposable machine, and do not follow the destructive steps on storage that holds data you have not backed up. The identifiers in the captured output - addresses, key fingerprints, filesystem UUIDs, archive fingerprints, timestamps - belong to one run; repeating the sequence produces new values with the same shape.

Commands entered in john's ordinary shell begin with $ and commands entered in a root shell begin with #; the prompt marker is not typed. john's account and sudo access were established during installation (Chapter 2) and are described in Chapter 5; the same account exists on both practice hosts. From john's shell, sudo -i enters a root shell and exit leaves it. A single elevated command is written $ sudo command. The examples assume the device and mount habits from Chapter 11, the filesystem background from Chapter 12, and the LVM commands from Chapter 13; systemd units and timers follow Chapter 8 and Chapter 10.

Create the reader workspace before the first example. Backups of john's own files are staged under it, and the exclusion lists used later are written there:

$ mkdir -p ~/debian-handbook/backup-restore/source

The command prints nothing. The source subdirectory holds a file that later archives include, so that home-directory data appears in repository listings alongside system data.

The tools come from packages beyond a base installation. On client1, install them with the ordinary interactive procedure from Chapter 6 and answer the confirmation prompt:

$ sudo apt install borgbackup restic rsync acl attr lvm2 postgresql parted grub-pc-bin initramfs-tools

borgbackup provides Borg, restic is installed for the comparison in Section 15.1.4, acl and attr provide getfacl/setfacl and getfattr/setfattr, lvm2 provides the snapshot mechanism, postgresql installs PostgreSQL 17 for the application-data exercises, and grub-pc-bin plus initramfs-tools are needed to rebuild the boot chain on the recovery disk in Section 15.7. apt prints its usual plan and progress; the exact lines depend on what is already installed and are not repeated here. On backup1, the repository side needs fewer tools:

$ sudo apt install borgbackup rsync acl attr openssh-server

apt prints the same kind of plan and progress, which is not repeated here either. The PostgreSQL package creates the postgres administrative account and starts a 17/main cluster automatically; the examples reach the server through that account, which is Debian's standard arrangement.

Record the starting state of both machines. On client1, as root:

# hostname && lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
client1
NAME    SIZE TYPE FSTYPE MOUNTPOINTS
vda     100G disk        
└─vda1  100G part ext4   /
vdb       4G disk        
vdc       4G disk        

hostname prints the machine's name and lsblk lists its block devices; the output confirms the two empty practice disks. On backup1, as root:

# hostname && lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS
backup1
NAME    SIZE TYPE FSTYPE MOUNTPOINTS
vda     100G disk        
└─vda1  100G part ext4   /
vdb       3G disk        

On client1, in john's shell, check the filesystem layout and the name service:

$ df -hT / /home
Filesystem     Type  Size  Used Avail Use% Mounted on
/dev/vda1      ext4   98G  1.4G   92G   2% /
/dev/vda1      ext4   98G  1.4G   92G   2% /

/ and /home are the same filesystem, which matters later: a backup of / with --one-file-system in Section 15.7 includes john's home because it is not a separate mount.

$ getent hosts backup1
192.168.121.118 backup1

The repository host resolves by name, so commands can use backup1 rather than an address that changes with the network. Finally, on client1, as root, confirm the tool versions the examples were written against:

# borg --version && rsync --version | head -1 && tar --version | head -1 && restic version | head -1
borg 1.4.0
rsync  version 3.5.0  protocol version 32
tar (GNU tar) 1.35
restic 0.18.0 compiled with go1.24.4 on linux/amd64

Each line comes from one command in the chain: Borg 1.4.0, rsync 3.5.0, GNU tar 1.35, and restic 0.18.0, all from Debian 13's own packages.

In this section