Validate tar restores for metadata, hard links and sparse files

Validate tar restores for metadata, hard links and sparse files

Validate tar restores for metadata, hard links and sparse files

A backup job can exit with code zero and its archive can list files successfully, yet the restored service may still be unable to read directories because ownership has changed. Sparse files may also be expanded during restoration and quickly fill the disk. For a dedicated server used as an offsite backup destination, acceptance testing must cover more than whether an archive exists: it must also verify restored contents, file relationships and disk usage.

This walkthrough uses Debian 12, GNU tar and an ext4 file system with extended attribute support. All operations are confined to temporary test directories. Qsfp uses individually connected, direct-attached NVMe drives without hardware RAID. File archives, disk redundancy and offsite copies must be planned separately; none can replace the others.

1. Define the limits of file archiving

File-level archives are suitable for configuration files that are no longer being written, offline exports and static data. They are not suitable for directly archiving a database directory that is still being updated. Even if every file is read successfully, different files may reflect different points in the transaction history.

A successful content check proves only that the restored contents match the source files used for verification. It does not prove that the source files were application-consistent when archived.

Before starting, establish how consistency will be achieved: stop the relevant writes, use the application's own export function, or create a consistent snapshot according to the application's requirements. A snapshot is not automatically application-consistent. If “file changed as we read it” appears, investigate the process writing to the files rather than ignoring the warning and declaring the job successful.

This test preserves standard permissions, numeric ownership, access control lists and extended attributes, and checks hard links and sparse files. It does not guarantee that all security semantics will survive restoration across systems. Account mappings, security policies and file system capabilities on the destination must still be checked separately.

2. Build a test set that covers edge cases

Run the following commands from an account with administrative privileges. Install the tools first, then enter a root shell. All subsequent examples run in that same shell. The system generates the temporary path; no production directories are used.

sudo apt-get update
sudo apt-get install tar acl attr coreutils
sudo -i
umask 077
LAB=$(mktemp -d /var/tmp/tar-restore.XXXXXXXX)
mkdir "$LAB/source" "$LAB/restore"
cd "$LAB/source"
printf 'backup acceptance\n' > payload.txt
chmod 0640 payload.txt
chown 12345:12345 payload.txt
ln payload.txt payload-hardlink.txt
ln -s payload.txt payload-link.txt
setfacl -m u:12346:r-- payload.txt
setfattr -n user.backup_test -v accepted payload.txt
truncate -s 1G sparse.bin
printf 'tail\n' | dd of=sparse.bin bs=1 \
  seek=1073741819 conv=notrunc status=none
printf '%s\n' "$LAB"

The numeric UID and GID are test data, not credentials or identifiers for any provisioned account. Access control lists can change the group permission bits shown in permission listings. Capture the baseline only after setting all attributes rather than relying on the earlier chmod argument.

The hard link checks whether two paths still refer to the same inode. The symbolic link checks whether the link text is preserved. The sparse file distinguishes logical length from actually allocated space. Creating a sparse file does not immediately write its full logical capacity to disk, but the restore destination should still have sufficient space available.

3. Record a baseline, then create a verifiable archive

Store the baseline separately from the archive so that archive damage does not also make the acceptance criteria inaccessible. The following commands record regular file contents, permissions, numeric identities, modification times, extended attributes and access control lists.

cd "$LAB/source"
sha256sum payload.txt payload-hardlink.txt sparse.bin \
  > "$LAB/content.sha256"
stat -c '%n|%u|%g|%a|%Y' payload.txt sparse.bin \
  > "$LAB/meta.before"
getfacl -n payload.txt > "$LAB/acl.before"
getfattr -d -m '^user\.' payload.txt > "$LAB/xattr.before"

tar --create --file="$LAB/backup.tar" \
  --format=pax --acls --xattrs --sparse \
  --numeric-owner -C "$LAB/source" .

cd "$LAB"
sha256sum backup.tar > backup.tar.sha256
sha256sum -c backup.tar.sha256
tar --list --file=backup.tar

Check the exit status of every command. Stop if any step fails; do not treat output from subsequent commands as evidence of success. Automated workflows must also propagate failures explicitly so that a successful final command cannot mask earlier errors.

--sparse identifies sparse regions, while --acls and --xattrs record the corresponding metadata. Do not add --dereference, which can replace symbolic links with their target contents. Do not use --hard-dereference either, as it turns hard-linked files into separate, independent files.

Checksums can detect accidental corruption, but they do not protect against an attacker replacing both the archive and its checksum file. Where tamper protection is required, the checksum manifest also needs an independent, trusted storage or signing mechanism.

4. Restore into an empty directory and check each assertion

First check that the archive members contain no absolute paths or paths that escape the destination directory, then restore into an empty directory. This example uses an archive created from a controlled test set. Never extract an archive from an unknown source directly as root, and never extract it over the system root directory.

tar --extract --file="$LAB/backup.tar" \
  --directory="$LAB/restore" \
  --acls --xattrs --xattrs-include='user.*' \
  --numeric-owner --same-owner --same-permissions

cd "$LAB/restore"
sha256sum -c "$LAB/content.sha256"
stat -c '%n|%u|%g|%a|%Y' payload.txt sparse.bin \
  > "$LAB/meta.after"
getfacl -n payload.txt > "$LAB/acl.after"
getfattr -d -m '^user\.' payload.txt > "$LAB/xattr.after"
diff -u "$LAB/meta.before" "$LAB/meta.after"
diff -u "$LAB/acl.before" "$LAB/acl.after"
diff -u "$LAB/xattr.before" "$LAB/xattr.after"
test payload.txt -ef payload-hardlink.txt
test "$(readlink payload-link.txt)" = payload.txt
stat -c '%n logical=%s blocks=%b block-unit=%B' sparse.bin
du -h sparse.bin
du -h --apparent-size sparse.bin

All content checks should pass, the three diff commands should report no differences, and both test commands should return zero. Hard-link validation compares file identity within the restore directory; the restored inode number does not need to match the one in the source directory.

File length alone does not prove sparseness

The logical length reported by stat should be 1073741824 bytes. Determine the actual allocation by multiplying the block count by the block unit; it should be substantially smaller than the logical length. Different file systems do not need to allocate exactly the same number of blocks, and ls -l alone cannot establish whether sparseness has been preserved.

This example restores only user.* extended attributes. Attributes such as security labels and file capabilities must be handled in accordance with the destination system's policies; do not broaden the restore scope without assessing the consequences. Although numeric ownership is preserved, the same UID may identify a different account on the destination. Verify identity mappings before a production migration.

5. Make restore results a backup acceptance requirement

A complete acceptance test should retain at least four kinds of evidence: a description of source data consistency, the archive and checksum manifest, command results from an isolated restore, and reports comparing contents and metadata. Do not delete the source data before acceptance testing is complete, and do not hand the restore directory directly to a production service.

Another NVMe drive in the same server can provide temporary storage, but it still shares the source data's whole-server failure domain. After moving the archive to an independent location, verify the copy there and restore from that copy rather than checking only the local original.

Before integrating the process into automation, also test failure cases. Restore as a regular user and check whether an ownership restoration failure stops the workflow. Restore onto a destination that does not support the relevant attributes and confirm that warnings are not suppressed. Run out-of-space tests in a separate environment with an enforced capacity limit; do not fill a production file system.

The final acceptance criteria are correct contents, expected ownership and link relationships, reasonable disk usage, and detectable failures. The archive command is only the starting point. A repeatable restore from an independent copy is the endpoint of backup acceptance testing.

Frequently asked questions

Does a successful tar archive prove that a backup is recoverable?

No. Extract it into an empty directory and verify contents, numeric ownership, permissions, extended attributes, hard links and allocated space. Application consistency needs a separate check.

Do archives on two NVMe drives in one server count as offsite backup?

No. Both remain exposed to a server failure or deletion through the same account. Copy the archive to an independent failure domain, verify its checksum there and perform an isolated restore.

Qsfp AMD EPYC 7543P dedicated server from $470.9/month

Use these requirements to configure a server

Review memory, storage, bandwidth, IPs, and OS, then confirm the dedicated server configuration.