rd.ROBERTO
D’ALESSIO
IT

Using tar for backups: archive, inspect, restore

An archive is useful when you know what it contains and how to recover it.

Originally published in 2013. Reworked on 23 October 2026; this revision replaces the previous instructions.

An archive is not a backup plan

tar collects files and directories into an archive. Compression saves space; a backup also needs an appropriate selection of data, a safe destination and a recovery check.

A document folder and an application continuously changing its files have different needs. Archiving a website directory does not produce a consistent database backup by itself.

Create an archive with relative paths

This example assumes a documents folder in the working directory. Keep the resulting archive outside that folder.

bash
tar -czf documents-backup.tar.gz documents/

-c creates an archive, -z enables gzip and -f specifies the file. Check your current directory, space and permissions. Do not extend a document example to system directories without reviewing the consequences.

Inspect before extracting

List the contents without writing them to the filesystem:

bash
tar -tzf documents-backup.tar.gz

Confirm that the paths match the expected data. Do not extract an archive obtained from someone else into a directory containing important files without checking it first.

Separate recovery testing from production

Use a dedicated, empty directory for the recovery test. Verify names, contents and permissions before replacing operational data. Consider symbolic links and unexpected paths when working with untrusted material.

Keep the context

Record the backup date, origin and scope. Preserve recovery documentation and keep credentials out of public directories. Choose protected storage for sensitive files.

The useful outcome is recovering the data you need in the environment where it will be used.

Sources and further reading

Keep exploring.

roberto / interactive terminal

A different way to explore the site. Predefined commands, no server access.