Debian 13 Tutorial / Restore tar Backups and Verify Metadata, Links and Service Responses
Creating a tar archive in Debian 13 does not prove that its contents can be recovered. This example creates an uncompressed archive. You need to restore it to an empty path to check the contents, numeric user and group IDs (UID/GID), permissions, access control lists (ACLs), extended attributes, and links, and verify that the services run from the restored copy respond normally. This tutorial backs up and restores a small HTTP service and data while preserving the original, and rejects a corrupted copy before extraction.
Choose the Backup Scope and a Consistent Point in Time
tar is a tool that archives multiple files and their metadata. Archiving files is different from consistently preserving the state of a service at a specific point in time. Copying a database file that is being written does not guarantee transaction consistency, so you must choose an actual program backup, snapshot, or write-stop method.
This exercise involves a Python HTTP server using only the standard library and read-only data files. After stopping the service, code and data are archived, and the systemd unit is also stored separately. It is not an example verifying database recovery, full OS recovery, or external storage replication.
| Item | Preservation and verification criteria |
|---|---|
| File contents and code | Comparison of bytes between the original and restored copies and comparison of HTTP responses |
| UID/GID and mode | Maintain numeric ID and 640 permissions |
| ACL | Maintain specific user read items and mask |
| Extended attributes | Maintain user.lf-course=backup30 |
| Links | Maintain internal hard link relationships and relative symlink targets of the backup |
| Service configuration | Actual unit, runtime, and data paths, and start after restoration |
Prepare the Environment and Dedicated Data
A separate Debian 13 VM with sudo access, Python 3, GNU tar, and a file system that supports ACL and extended attributes is required. The verification environment used tar 1.35, acl 2.3.2, attr 2.5.2, and ext4. A regular user lfstudent existed, using an environment with UID/GID 1000. If using a different account, adjust the owner and the unit's User and Group to the actual account below.
The backup folder is root-only, and the practice path will be newly created. Stop if any account, installation or path check fails. Do not delete existing data to make the exercise fit. Run these Bash blocks in a dedicated lab shell; an exit in the collision guard terminates that shell. If port 8080 is being used by another service, decide on a dedicated port and reflect it consistently throughout the entire example.
sudo apt install tar acl attr python3 curl
id lfstudent
ss -ltn
for p in /srv/lf-backup30 /var/backups/lf-debian13-30 /etc/systemd/system/lf-backup30.service /etc/systemd/system/lf-backup30.service.d; do
if test -e "$p"; then echo "Already exists: $p" >&2; exit 1; fi
done
sudo install -d -m 755 /srv/lf-backup30/source /srv/lf-backup30/restore
sudo install -d -m 700 /var/backups/lf-debian13-30
printf 'backup payload 30\n' | sudo tee /srv/lf-backup30/source/message.txt
sudo chown lfstudent:lfstudent /srv/lf-backup30/source/message.txt
sudo chmod 640 /srv/lf-backup30/source/message.txt
sudo setfacl -m u:nobody:r-- /srv/lf-backup30/source/message.txt
sudo setfattr -n user.lf-course -v backup30 /srv/lf-backup30/source/message.txt
sudo ln /srv/lf-backup30/source/message.txt /srv/lf-backup30/source/message-hard.txt
sudo ln -s message.txt /srv/lf-backup30/source/current.txt
ACL (Access Control List) is a list that specifies access for specific users in addition to the default owner, group, and other user permissions. Here, a read entry for nobody is added for restoration comparison purposes. Directory traversal permissions and ACL mask also affect actual access, so do not judge accessibility by a single ACL entry alone.
Extended attributes are a feature that attaches additional information in a name-value form to a file. This example tests one attribute in the user.* namespace. It does not generalize to preserving security.* attributes, SELinux labels, or file capabilities. The example also creates two types of file link. A hard link is another name for the same file object, and a symlink points to the target path.
Save the complete HTTP example and normal response
Save the entire following program in app.py. It reads current.txt from LF_DATA_DIR and returns it as is, and if the file cannot be read, it returns 503. This is a loopback exercise not exposed externally.
sudo tee /srv/lf-backup30/source/app.py > /dev/null <<'PY'
import os,pathlib
from http.server import BaseHTTPRequestHandler,HTTPServer
data=pathlib.Path(os.environ['LF_DATA_DIR'])
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
try:
body=(data/'current.txt').read_bytes()
status=200
except OSError:
body=b'data unavailable\n'
status=503
self.send_response(status)
self.send_header('Content-Type','text/plain; charset=utf-8')
self.send_header('Content-Length',str(len(body)))
self.end_headers()
self.wfile.write(body)
HTTPServer(('127.0.0.1',8080),Handler).serve_forever()
PY
sudo tee /etc/systemd/system/lf-backup30.service > /dev/null <<'UNIT'
[Unit]
Description=LF backup restore HTTP lab 30
[Service]
Type=exec
User=lfstudent
Group=lfstudent
Environment=LF_DATA_DIR=/srv/lf-backup30/source
ExecStart=/usr/bin/python3 /srv/lf-backup30/source/app.py
UNIT
sudo chmod 644 /srv/lf-backup30/source/app.py
sudo systemctl daemon-reload
sudo systemctl start lf-backup30.service
curl --noproxy '*' -fsS http://127.0.0.1:8080/ | sudo tee /var/backups/lf-debian13-30/http-before.txt
sudo cmp /var/backups/lf-debian13-30/http-before.txt /srv/lf-backup30/source/message.txt
The response is backup payload 30, and if cmp succeeds, it exits without output. If not ready right after starting, request again after a short delay. If it continues to fail, check the actual file path, permissions, and port errors with systemctl status and journalctl -u lf-backup30.service. Do not ignore request failures and do not use an empty response file as a normal reference.
Store metadata and archives while stopped
After stopping the service, preserve the original unit and file metadata. The record serves as a basis for comparison with restoration results. The -cn option outputs items with numeric IDs, omitting the ACL path and owner description headers.
sudo systemctl stop lf-backup30.service
sudo cp -a /etc/systemd/system/lf-backup30.service /var/backups/lf-debian13-30/
sudo getfacl -cn /srv/lf-backup30/source/message.txt | sudo tee /var/backups/lf-debian13-30/acl-before.txt
sudo getfattr --only-values -n user.lf-course /srv/lf-backup30/source/message.txt | sudo tee /var/backups/lf-debian13-30/xattr-before.txt
sudo stat -c '%u:%g:%a' /srv/lf-backup30/source/message.txt | sudo tee /var/backups/lf-debian13-30/stat-before.txt
sudo tar --format=posix --acls --xattrs --xattrs-include='user.*' --numeric-owner -cpf /var/backups/lf-debian13-30/source.tar -C /srv/lf-backup30 source
-c specifies creation, -f specifies the archive path, and -C specifies the base directory. This command preserves the relative path source and stores the backup file outside of it. Use --acls and --xattrs to handle the corresponding metadata, and restrict the scope to include only user.*. The options used can be found in Debian GNU tar manual.
Do not add -h: it would follow symlinks and archive their targets instead of the links. In this example, current.txt is kept as the link itself. Which name of a hard link is recorded first with content may vary, but what matters is that the relationship of the two names being the same object is preserved.
The hash is created based on the backup folder in a separate administrative shell. The block below is executed only within the root shell where paths are quoted and does not modify the normal archive.
sudo bash <<'ROOT'
set -euo pipefail
cd /var/backups/lf-debian13-30
sha256sum source.tar > source.tar.sha256
sha256sum -c source.tar.sha256
tar -tvf source.tar
ROOT
Hash verification serves as a criterion for detecting changes during storage and transmission. If the backup and hash file are tampered with together, authenticity cannot be guaranteed, so it is distinguished from access restrictions, independent storage, or necessary signing procedures. The fact that a tar list can be read does not mean that all file contents and service recovery have been verified.
Reject a Damaged Copy Before Extraction
The following is an exercise in preserving the normal archive and flipping just one byte of a copy. If a copy with the same name exists, it is not overwritten. We do not forcibly attempt recovery from corrupted copies, and if the hash differs from the normal reference, it is not extracted.
sudo test ! -e /var/backups/lf-debian13-30/source-corrupt.tar && sudo cp -a /var/backups/lf-debian13-30/source.tar /var/backups/lf-debian13-30/source-corrupt.tar
Run the Python block only if the preceding copy command succeeds. If the destination already exists or copying fails, stop and inspect it; do not modify an older copy.
sudo python3 - <<'PY'
import hashlib,pathlib
bad=pathlib.Path('/var/backups/lf-debian13-30/source-corrupt.tar')
with bad.open('r+b') as f:
f.seek(1024)
old=f.read(1)
if len(old)!=1:raise SystemExit('Too short')
f.seek(1024);f.write(bytes([old[0]^1]))
good_hash=pathlib.Path('/var/backups/lf-debian13-30/source.tar.sha256').read_text().split()[0]
if hashlib.sha256(bad.read_bytes()).hexdigest()==good_hash:raise SystemExit('Corruption not detected')
print('CORRUPT_COPY_REJECTED_WITHOUT_EXTRACTION')
PY
The verification result was CORRUPT_COPY_REJECTED_WITHOUT_EXTRACTION. This indicates that a corrupted copy was found and does not mean recovery success of the normal backup. In the next section, the hash of the normal source.tar will be rechecked before proceeding.
Restore to an Empty Path and Compare Contents and Metadata
Only restore backups that you have created and verified the source of with root privileges as shown below. Do not extract as root without checking the paths and links of archives received from other sources. Instead of overwriting the existing service directory directly, use an empty dedicated restore path.
sudo bash <<'ROOT'
set -euo pipefail
cd /var/backups/lf-debian13-30
sha256sum -c source.tar.sha256
test -z "$(find /srv/lf-backup30/restore -mindepth 1 -maxdepth 1 -print -quit)"
tar --acls --xattrs --xattrs-include='user.*' --numeric-owner --same-owner -xpf source.tar -C /srv/lf-backup30/restore
cmp /srv/lf-backup30/source/message.txt /srv/lf-backup30/restore/source/message.txt
cmp /srv/lf-backup30/source/app.py /srv/lf-backup30/restore/source/app.py
getfacl -cn /srv/lf-backup30/restore/source/message.txt > acl-after.txt
getfattr --only-values -n user.lf-course /srv/lf-backup30/restore/source/message.txt > xattr-after.txt
stat -c '%u:%g:%a' /srv/lf-backup30/restore/source/message.txt > stat-after.txt
cmp acl-before.txt acl-after.txt
cmp xattr-before.txt xattr-after.txt
cmp stat-before.txt stat-after.txt
test "$(readlink /srv/lf-backup30/restore/source/current.txt)" = message.txt
test "$(stat -c %i /srv/lf-backup30/restore/source/message.txt)" = "$(stat -c %i /srv/lf-backup30/restore/source/message-hard.txt)"
test "$(stat -c %h /srv/lf-backup30/restore/source/message.txt)" = 2
echo RESTORE_METADATA_PASSED
ROOT
-x is for extraction, -p applies the saved permissions, and --same-owner and --numeric-owner is used to preserve numeric ownership. When moving to another server, numeric 1000 may belong to someone else's account, so determine the user and group ID mapping first. Preserving incorrect mappings does not constitute proper restoration.
| Verification items | Actual results |
|---|---|
| message.txt and app.py | Bytes identical to the original |
| Numeric ownership and mode | 1000:1000:640 maintained |
| ACL | user:65534:r-- and items including mask identical |
| user.lf-course | backup30 identical |
| hard link | The inode of the two restored names is identical, link count 2 |
| symlink | current.txt → message.txt retained |
It is not necessary for the restored inode to have the same number as the original inode. It compares whether the two hard links inside the restored copy share the same object. For the numeric items and mask interpretation of ACL results, refer to getfacl documentation, and for querying extended attributes, refer to getfattr documentation.
Running the service with the restored copy only
Move the original to source-held without deleting it to free the original path. Then add a drop-in only to this practice unit to change both the code and data paths to the restored copy. Check to ensure the paths and service are already stopped before running.
sudo test ! -e /srv/lf-backup30/source-held && sudo mv /srv/lf-backup30/source /srv/lf-backup30/source-held
sudo install -d /etc/systemd/system/lf-backup30.service.d
sudo tee /etc/systemd/system/lf-backup30.service.d/20-restored.conf > /dev/null <<'DROP'
[Service]
Environment=LF_DATA_DIR=/srv/lf-backup30/restore/source
ExecStart=
ExecStart=/usr/bin/python3 /srv/lf-backup30/restore/source/app.py
DROP
sudo systemctl daemon-reload
sudo systemctl start lf-backup30.service
curl --noproxy '*' -fsS http://127.0.0.1:8080/ | sudo tee /var/backups/lf-debian13-30/http-after.txt
sudo cmp /var/backups/lf-debian13-30/http-before.txt /var/backups/lf-debian13-30/http-after.txt
sudo systemctl is-active lf-backup30.service
In actual verification, the restored service was active and the response bytes were the same even when the original source path was absent. Restoration for this practice is only complete when file restoration, metadata verification, process start, and HTTP response all pass. Immediately after starting, if requests fail, check the status and logs and retry, but do not consider it complete until responses have been successfully compared.
If there is a problem running the restored version, stop the service while keeping the original and only remove this practice drop-in. You can revert source-held back to the original source and return to the original configuration by performing daemon-reload, starting the service, and checking the response. If a normal source already exists, do not overwrite it and first check the path status.
Preserve backups and clean up only the practice
Perform the following cleanup only when the verification of the restoration is complete and the practice files of the original and restored versions are no longer needed. Records of comparison with the normal archive are kept. If other files have been added to each path, leave rmdir to fail and check the reason.
sudo systemctl stop lf-backup30.service
sudo rm -- /etc/systemd/system/lf-backup30.service.d/20-restored.conf /etc/systemd/system/lf-backup30.service
sudo rmdir /etc/systemd/system/lf-backup30.service.d
sudo systemctl daemon-reload
for d in /srv/lf-backup30/source-held /srv/lf-backup30/restore/source; do
sudo rm -- "$d/current.txt" "$d/message-hard.txt" "$d/message.txt" "$d/app.py"
sudo rmdir "$d"
done
sudo rmdir /srv/lf-backup30/restore /srv/lf-backup30
ss -ltn
sudo ls -lh /var/backups/lf-debian13-30
After verification, the dedicated service, practice paths, and the 8080 listener have been removed, while the good archive, hash, metadata and response records have been preserved. Since this local backup is also on the same host disk, to prepare for total host loss, separate storage location, restoration testing, access control, and retention schedule should be established together.




