How to Back Up and Restore a Single-Volume Archive in Linux

how to backup and restoring a single volume archive in linux

Backing up data is one of the most fundamental responsibilities of any Linux user or system administrator. Before diving into complex, multi-device backup strategies, every admin needs to master the simplest and most common case: the single-volume archive — one archive file that contains all of your backed-up data, small enough to fit entirely on one piece of media or in one file on disk.

This article explains, from first principles, what an archive is, how the tar command works, how to create and restore a single-volume archive, how compression fits in, and how to troubleshoot common problems — all illustrated with practical, real-world examples.

What Is a Single-Volume Archive?

A single-volume archive is exactly what it sounds like: one file that contains a bundle of other files and directories, with no need to split the data across multiple pieces. Compare this to a multi-volume archive (used when data is too large for one piece of media), which we cover in a separate article.

For most everyday backup jobs — a home directory, a website’s files, a set of configuration files — a single-volume archive is all you need, because modern storage (hard drives, SSDs, cloud storage) can typically hold gigabytes or terabytes in a single file without any issue.

flowchart TD
    A[Source Files and Directories] -->|tar -cvf| B[Single Archive File: backup.tar]
    B -->|tar -xvf| A2[Restored Files and Directories]

The tar Command: The Foundation

The tar utility (Tape Archiver) is the standard Linux tool for creating archives. Its basic syntax is:

tar [options] -f archive-name.tar file-or-directory

Creating an Archive

tar -cvf home-backup.tar /home/username

Breaking this down:

  • -c = create a new archive
  • -v = verbose, print each filename as it’s processed
  • -f = specify the filename of the archive (must be followed immediately by the filename)

This command bundles everything under /home/username into a single file called home-backup.tar.

Listing Contents Without Extracting

Before restoring, it’s good practice to check what’s inside an archive:

tar -tvf home-backup.tar

The -t flag lists the table of contents.

Restoring (Extracting) an Archive

tar -xvf home-backup.tar
  • -x = extract
  • Files are restored relative to the current directory, preserving the original directory structure recorded in the archive.

To restore to a specific location:

tar -xvf home-backup.tar -C /restore/destination

The -C flag changes the extraction directory before extracting.

Adding Compression

A plain tar file is not compressed — it’s just a bundle. To save space, you compress it, typically with gzip or bzip2 or the newer xz.

Creating a Compressed Archive

tar -czvf home-backup.tar.gz /home/username

The -z flag tells tar to pipe the output through gzip.

For better compression at the cost of speed:

tar -cjvf home-backup.tar.bz2 /home/username   # bzip2
tar -cJvf home-backup.tar.xz /home/username    # xz

Restoring a Compressed Archive

tar -xzvf home-backup.tar.gz
tar -xjvf home-backup.tar.bz2
tar -xJvf home-backup.tar.xz

Modern GNU tar can actually auto-detect the compression type even without the -z/-j/-J flag, so tar -xvf home-backup.tar.gz will often “just work” — but specifying the flag explicitly is still good practice for clarity and portability.

Comparison Table: Compression Options

MethodFlagSpeedCompression RatioTypical Use Case
None(none)FastestNoneArchives that will be compressed later, or already-compressed data
gzip-zFastModerateGeneral-purpose, most common default
bzip2-jSlowerBetter than gzipWhen storage space matters more than speed
xz-JSlowestBestLong-term archival, minimizing storage cost

A Practical, Real-World Example

Let’s back up an Nginx web server’s configuration and content directory before making risky changes.

Step 1: Create the backup

sudo tar -czvf /backups/nginx-backup-$(date +%F).tar.gz /etc/nginx /var/www

This uses $(date +%F) to automatically insert today’s date (e.g., 2026-07-24) into the filename, so backups don’t overwrite each other.

Step 2: Confirm it worked

ls -lh /backups/
tar -tzvf /backups/nginx-backup-2026-07-24.tar.gz | head -20

Step 3: Simulate a disaster — restore it

sudo tar -xzvf /backups/nginx-backup-2026-07-24.tar.gz -C /

Because the archive was created with absolute paths (/etc/nginx, /var/www), extracting with -C / restores the files to their original locations.

Preserving Permissions and Ownership

By default, tar preserves file permissions, ownership, and timestamps as recorded in the archive — but only if you’re extracting as root. If you extract as a normal user, ownership may be changed to that user instead.

To force preservation of original ownership during extraction (as root):

sudo tar -xpvf home-backup.tar

The -p flag means preserve permissions.

Excluding Files From a Backup

Often you don’t want everything in a directory — for example, you might want to skip cache files or log files:

tar -czvf project-backup.tar.gz --exclude='*.log' --exclude='node_modules' /srv/project

You can also use an exclude file listing multiple patterns:

tar -czvf project-backup.tar.gz -X excludes.txt /srv/project

Where excludes.txt contains:

*.log
*.tmp
node_modules/
.git/

Verifying Archive Integrity

Never trust a backup you haven’t verified. Two common approaches:

1. Compare against source using diff:

tar -xzvf backup.tar.gz -C /tmp/verify
diff -r /home/username /tmp/verify/home/username

2. Generate and check a checksum:

sha256sum backup.tar.gz > backup.tar.gz.sha256
sha256sum -c backup.tar.gz.sha256

Automating Backups With cron

A single-volume archive backup is a perfect candidate for automation. Here’s a cron job that backs up /etc every night at 2 AM:

0 2 * * * /usr/bin/tar -czf /backups/etc-backup-$(date +\%F).tar.gz /etc

Note the escaped % (\%) — cron interprets unescaped % characters as newlines, so they must be escaped inside crontab entries.

Real-World Use Cases

  1. Website migrations: Archiving an entire web application directory before moving it to a new server.
  2. Configuration snapshots: Taking a tar snapshot of /etc before a major system upgrade, so changes can be rolled back if something breaks.
  3. Developer environment backups: Archiving a project directory (excluding build artifacts) before a risky refactor.
  4. User data migration: Backing up a single user’s home directory before reinstalling the OS.

Best Practices

  • Always test extraction in a scratch directory before trusting a backup for disaster recovery.
  • Use compression thoughtfully — gzip for speed, xz for maximum space savings on long-term cold storage.
  • Include a timestamp in the filename so successive backups don’t silently overwrite each other.
  • Exclude unnecessary files (caches, logs, build artifacts) to keep archives lean and restore times fast.
  • Store backups off-machine. A backup stored only on the same disk as the original data offers no protection against disk failure.
  • Automate with cron or systemd timers, and monitor for job failures (e.g., email alerts on non-zero exit codes).
  • Document restore procedures so that anyone on the team, not just the person who created the backup, can perform a restore.

Troubleshooting

Problem: tar: Cannot open: Permission denied

You likely need sudo to read protected system files, or to write to the destination directory. Re-run with elevated privileges:

sudo tar -czvf backup.tar.gz /etc

Problem: Archive is larger than expected

Check whether you’re accidentally including large, unnecessary directories like node_modules, .git, or log directories. Use --exclude to trim them out, or inspect with tar -tzvf backup.tar.gz | wc -l to count files.

Problem: gzip: stdin: not in gzip format

This means the file isn’t actually gzip-compressed, even though it might have a .gz extension — perhaps it’s a plain .tar that was renamed. Check with:

file backup.tar.gz

Problem: Restored files have the wrong owner

You likely extracted as a non-root user. Re-extract as root with -p to preserve original ownership:

sudo tar -xpvf backup.tar

Problem: “Not enough space” during extraction

Check available disk space with df -h before extracting large archives, and extract to a partition with sufficient free space.

Conclusion

The single-volume archive is the workhorse of Linux backups. With just a handful of tar flags — -c, -x, -v, -f, -z/-j/-J, and -C — you can confidently back up and restore anything from a single configuration file to an entire application directory. Combine this with compression, exclusion rules, integrity verification, and cron-based automation, and you have a simple, dependable, and completely free backup system built entirely from tools that ship with every Linux distribution.

Further Reading

Total
1
Shares

Leave a Reply

Previous Post
service command in Linux and it perimeters

service Command in Linux: Complete Guide to Managing System Services and Parameters

Next Post
how to backup and restoring a multi volume archive in Linux

How to Back Up and Restore a Multi-Volume Archive in Linux

Related Posts