15.3. Creating File-Based Backups

File-based backups copy the visible tree: files, directories, and the metadata that makes them usable. They are the foundation under the repository tools of Section 15.4 - which use the same preservation rules internally - and they remain the right tool for controlled copies and staged restores. Their weakness is history: a copy is one moment, and keeping versions means arranging multiple copies, which is exactly the work a repository automates.

A restore that drops metadata produces files that exist and do not work: the service account cannot read its configuration, the setgid directory stops group-sharing, the hard-linked file consumes double space and drifts when one name is edited. The preservation targets, and the flags each tool needs, are:

Preservation target rsync GNU tar
Ownership and group -o, -g (both in -a) with --numeric-ids --numeric-owner on create and extract
Permissions, modes, timestamps -p, -t (in -a) stored by default
POSIX ACLs -A --acls on create and extract
Extended attributes -X --xattrs on create and extract
Hard links -H (rsync makes a second pass to find them) stored by default
Symbolic links -l (in -a) stored by default
Sparse files -S --sparse
Device and special files -D (in -a) stored by default

Two conventions matter as much as the flags. --numeric-ids and --numeric-owner record and apply numeric user and group IDs instead of names: names are resolved on the machine doing the reading, so a copy restored onto a system where www-data has a different ID would otherwise be reinterpreted silently. And extraction flags must match creation flags - an archive created without --acls holds no ACLs to extract, and the loss is silent.

The baseline recorded in Section 15.2.1 is the acceptance test: every method below is judged by whether it reproduces the www-data ownership, the john:r-- ACL, the user.handbook attribute, the link count of 2, the symlink target, and the setgid mode.

15.3.2. Using rsync for Controlled Copies and Restore Trees

rsync's value is control: it shows what it would do, transfers only differences, and writes a plain directory tree that can be browsed and copied back without any tool. Its options used in this chapter:

Option Description
-a archive mode: recursive, preserving symlinks, permissions, timestamps, owner, group, device and special files
-H preserve hard links by matching them within the transfer
-A preserve POSIX ACLs
-X preserve extended attributes
-S handle sparse files efficiently
--numeric-ids record and apply numeric user and group IDs, not names
--one-file-system do not cross filesystem boundaries
--exclude-from FILE skip paths listed in the file
--dry-run report what would be transferred without copying
--stats print a transfer summary
--itemize-changes list each change with a code (Section 15.3.4)
--delete remove destination files that no longer exist at the source
--link-dest DIR hard-link unchanged files to a previous copy (Section 15.3.4)

One syntactic rule prevents the classic accident: a trailing slash on the source means "the contents of this directory", and its absence means "the directory itself". rsync -a /srv/appdata/ DEST/ copies reports/, config/, and the rest into DEST; without the slash, DEST receives an appdata/ level. All copies in this chapter use the trailing-slash form.

Copy the practice data into john's workspace, on client1, as root:

# install -d -m 755 /home/john/debian-handbook/backup-restore/file-copy
# rsync -aHAXS --numeric-ids /srv/appdata/ /home/john/debian-handbook/backup-restore/file-copy/

install -d creates the destination; the rsync transfer itself is silent on success. Now hold the copy against the baseline:

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

# file: home/john/debian-handbook/backup-restore/file-copy/config/app.conf
user.handbook="chapter15"

getfattr: Removing leading '/' from absolute path names

The copy is owned by www-data, mode 640, link count 2, with the ACL entry and the extended attribute intact - the baseline from Section 15.2.1 reproduced line for line. (getfattr prints its reminder about relative paths on standard error; it is informational.) Check the links:

# ls -li /home/john/debian-handbook/backup-restore/file-copy/config/app.conf* && readlink /home/john/debian-handbook/backup-restore/file-copy/config/app.conf.current && stat -c "%a %n" /home/john/debian-handbook/backup-restore/file-copy/shared
393243 -rw-r-----+ 2 www-data www-data 51 Nov 21 00:19 /home/john/debian-handbook/backup-restore/file-copy/config/app.conf
393243 -rw-r-----+ 2 www-data www-data 51 Nov 21 00:19 /home/john/debian-handbook/backup-restore/file-copy/config/app.conf.bak
393242 lrwxrwxrwx 1 root     root      8 Nov 21 00:20 /home/john/debian-handbook/backup-restore/file-copy/config/app.conf.current -> app.conf
app.conf
2775 /home/john/debian-handbook/backup-restore/file-copy/shared

ls -li shows inode numbers: both names print 393243, so -H preserved the hard link rather than storing the file twice. The + after the mode marks the ACL on each name. readlink prints the symlink's target unchanged, and shared kept its setgid mode 2775.

A tree like this is also the safest restore target. Restoring into a staging directory lets the administrator compare, cherry-pick, and move files deliberately, instead of overwriting live data with an extraction. Section 15.6 uses that pattern with Borg; here the tree is the whole result.

15.3.3. Creating and Inspecting tar Archives

tar packs a tree into one file with the metadata embedded. Its options used here:

Option Description
-c create an archive from the named paths
-x extract an archive
-t list the archive's table of contents
-v verbose listing during the operation
-f FILE read or write this archive file
-p on extraction, preserve permissions
-C DIR change to DIR before creating or extracting
--acls store and restore POSIX ACLs
--xattrs store and restore extended attributes
--numeric-owner store and apply numeric IDs only

Create an archive of the practice data, on client1, as root:

# tar --acls --xattrs --numeric-owner -cpf /home/john/debian-handbook/backup-restore/appdata.tar -C /srv/appdata .

-C /srv/appdata . stores the tree's contents without the leading /srv/appdata path, so extraction can happen anywhere; the command is silent. Inspect what was written - the pipeline prints the first ten of the archive's entries, enough to see every metadata type at once (the remaining two are the dumps/ directory and its export):

# tar -tvf /home/john/debian-handbook/backup-restore/appdata.tar | head
drwxr-xr-x 1001/1001         0 2026-11-21 00:19 ./
drwxr-xr-x 1001/1001         0 2026-11-21 00:20 ./reports/
-rw-r----- 1001/33          17 2026-11-21 00:20 ./reports/quarterly.txt
-rw-rw-r-- 1001/1001        10 2026-11-21 00:20 ./reports/temporary.txt
drwxr-xr-x 1001/1001         0 2026-11-21 00:20 ./config/
lrwxrwxrwx 0/0               0 2026-11-21 00:20 ./config/app.conf.current -> app.conf
-rw-r----- 33/33            51 2026-11-21 00:19 ./config/app.conf
hrw-r----- 33/33             0 2026-11-21 00:19 ./config/app.conf.bak link to ./config/app.conf
drwx------ 0/0               0 2026-11-21 00:19 ./lost+found/
drwxrwsr-x 1001/1001         0 2026-11-21 00:19 ./shared/

Three details of the listing carry information. Owners print as numbers - 1001/1001 for john, 33/33 for www-data - because --numeric-owner stored IDs without names; a restore therefore maps predictably onto any system. The hard link prints as hrw-r----- ... link to ./config/app.conf, tar's on-disk representation of the second name of one inode. And drwxrwsr-x on ./shared/ shows the setgid bit survived. lost+found rides along because the volume formatter created it; an exclusion would drop it.

Extract the archive into a fresh directory and check the round trip:

# install -d -m 755 /home/john/debian-handbook/backup-restore/tar-restore
# tar --acls --xattrs --numeric-owner -xpf /home/john/debian-handbook/backup-restore/appdata.tar -C /home/john/debian-handbook/backup-restore/tar-restore

install -d creates the target and the extraction is silent. Verify against the baseline:

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

# file: home/john/debian-handbook/backup-restore/tar-restore/config/app.conf
user.handbook="chapter15"

getfattr: Removing leading '/' from absolute path names

Ownership by name resolved from the stored numeric ID, the ACL, the attribute, and the link count all match the original. The example archive is uncompressed; tar czf (gzip) or xz compression shrink it at the cost of CPU, and either is added the same way. One archive per copy is the whole versioning story with tar - which is why the next step in this chapter's design is a repository.

15.3.4. Understanding Deletions and the Limits of Mirroring

A mirror is a copy of the present, not a record of the past. Watch what happens when the source changes. Delete the file that was created to be deleted, on client1, as root:

# rm /srv/appdata/reports/temporary.txt

The command prints nothing. Ask rsync what a refresh of the copy from Section 15.3.2 would do:

# rsync -a --dry-run --itemize-changes /srv/appdata/ /home/john/debian-handbook/backup-restore/file-copy/
.d..t...... reports/

--itemize-changes prints an eleven-character code per item: a . means no change in that attribute class, d marks a directory, and t marks a timestamp difference. The single line says the only pending work is the reports/ directory's timestamp - no file content, and no deletion. The stale file is the important part: the copy still contains reports/temporary.txt, because rsync without --delete never removes destination files. Confirm it:

# test -e /home/john/debian-handbook/backup-restore/file-copy/reports/temporary.txt

The command succeeds silently - an existing file makes test -e true - so the stale copy is still there. That behavior is the default for a reason: it protects the copy from a source that has been emptied by mistake.

A true mirror needs --delete, and with it the copy inherits every deletion, including mistaken ones. Treat --delete as a destructive option: run it with --dry-run --itemize-changes first, and read the *deleting lines it would print. When the deletion is intended:

# rsync -a --delete /srv/appdata/ /home/john/debian-handbook/backup-restore/file-copy/

The command is silent. Confirm that the stale file is gone from the copy:

# test ! -e /home/john/debian-handbook/backup-restore/file-copy/reports/temporary.txt

The command succeeds silently: the file no longer exists at the destination. (Without the ! the same test would succeed while the file was still present; checking for absence is exactly what the negated form is for.)

Mirrors gain a weak kind of history by keeping one tree per day and hard-linking the unchanged files - rsync's --link-dest. Build the first versioned copy, on client1, as root:

# install -d -m 755 /home/john/debian-handbook/backup-restore/versions
# rsync -aHAXS --numeric-ids /srv/appdata/ /home/john/debian-handbook/backup-restore/versions/2026-11-20/

Both commands are silent. Revise the report - on client1, in john's shell:

$ printf "quarterly report revised\n" >/srv/appdata/reports/quarterly.txt

The redirection writes the file and prints nothing. Restore its ownership, as root:

# chown john:www-data /srv/appdata/reports/quarterly.txt && chmod 0640 /srv/appdata/reports/quarterly.txt

Both parts are silent. Now take the second version with --link-dest pointing at the previous one:

# rsync -aHAXS --numeric-ids --link-dest=../2026-11-20 /srv/appdata/ /home/john/debian-handbook/backup-restore/versions/2026-11-21/

The relative --link-dest path is resolved against the destination, so the same command works for any date-paired directories. The transfer is silent. Prove the economics with inode numbers:

# stat -c "%i %n" /home/john/debian-handbook/backup-restore/versions/2026-11-20/config/app.conf /home/john/debian-handbook/backup-restore/versions/2026-11-21/config/app.conf /home/john/debian-handbook/backup-restore/versions/2026-11-20/reports/quarterly.txt /home/john/debian-handbook/backup-restore/versions/2026-11-21/reports/quarterly.txt
393266 /home/john/debian-handbook/backup-restore/versions/2026-11-20/config/app.conf
393266 /home/john/debian-handbook/backup-restore/versions/2026-11-21/config/app.conf
393268 /home/john/debian-handbook/backup-restore/versions/2026-11-20/reports/quarterly.txt
393275 /home/john/debian-handbook/backup-restore/versions/2026-11-21/reports/quarterly.txt

The unchanged app.conf has inode 393266 in both trees - one copy of the data block, two directory entries - while the revised quarterly.txt has different inodes, because --link-dest copies files whose content changed. Each tree is a complete, browsable snapshot, and the pair costs barely more than one tree plus the day's changes.

The limits are as instructive as the mechanism: both trees must stay intact (editing a file through the older tree mutates the newer one, because they share inodes), the trees must share a filesystem, there is no integrity check, and there is no encryption or compression. Those are precisely the gaps a repository tool closes with chunk-level deduplication and per-archive integrity, which is where the design now turns.