02 / FILES AND BACKUPS · LESSON 08

Rehearse a file restore

Restore listed files to a separate directory after checking that every required backup exists.

16 min · learn at your pace

Practise recovery before it is urgent

A restore rehearsal tests more than whether a backup exists. It checks that the selected files can be copied to a usable location and compared with the source. Keep the restore target separate from the backup so a mistake does not overwrite the only available copy.

Read the trusted manifest once to check every required regular file, then again to perform the copies. This two-phase structure catches missing inputs before any existing restored file is replaced. It is not a transaction against arbitrary filesystem failures, but it makes the promised failure behavior explicit and testable.

Quote both sides of a copy

Each source and destination path is one argument, including names containing spaces or Unicode. Prefix relative paths with ./ where appropriate so a leading dash is not an option. Use cmp -s to verify byte equality after copying; a comparison failure must stop the operation rather than be announced as success.

This lab persists regular files and directories, not Unix ownership, permissions, symlinks or special files. Restoring those properties on a real server requires a different backup contract and appropriate privileges. Here the goal is a repeatable rehearsal over inert text files in the browser workspace. Successful repeated runs replace only the listed target files and keep unrelated target files.

Try an example.sh
mkdir -p './restore example'
cp './backup/settings.conf' './restore example/settings.conf'
cmp -s './backup/settings.conf' './restore example/settings.conf' && printf 'Bytes verified\n'
YOUR TURN

Put it into practice.

Read restore.txt, a trusted nonempty list of unique basenames (no slash/tab/newline). Restore backup/<name> into an optional output directory argument, default restored, which is a separate safe relative workspace path. Before changing any target, verify every listed source is a regular file; a missing source exits nonzero with Missing backup on stderr and no stdout. Copy and byte-compare each file, preserving unrelated target files and original backups. On success print restored=N and a newline; reruns replace the listed files.

See an explained solution
One way to solve it.sh
out=${1:-restored}
while IFS= read -r name || [ -n "$name" ]; do
 [ -f "./backup/$name" ] || { printf 'Missing backup: %s\n' "$name" >&2; exit 1; }
done < restore.txt
mkdir -p "./$out" || exit 1
count=0
while IFS= read -r name || [ -n "$name" ]; do
 cp "./backup/$name" "./$out/$name" || exit 1
 cmp -s "./backup/$name" "./$out/$name" || exit 1
 count=$((count+1))
done < restore.txt
printf 'restored=%d\n' "$count"

The initial preflight prevents a late missing source from leaving an earlier target replaced. Copies retain the original backup bytes, comparisons verify the result, and narrow target paths leave unrelated files intact on reruns.