tar.xz - High Compression Archives
.tar.xz combines tar (archiving directories) with XZ (LZMA2) compression. Use it when you want smaller archives than tar.gz and you can afford slower compression and (often) slower restores.
- Create:
tar -cJf out.tar.xz /path/to/dir - Extract:
tar -xJf out.tar.xz -C /tmp/restore-test - List:
tar -tJf out.tar.xz - Test:
xz -t out.tar.xz
Restore checklist
Use this checklist before restoring a .tar.xz backup into a live WordPress docroot.
- Verify integrity:
xz -t backup.tar.xz - List contents:
tar -tJf backup.tar.xz | head - Extract to staging:
/tmp/restore-test/ - Confirm key paths exist:
wp-content/uploads/,wp-config.php - Only then copy/sync into place and restart services
sudo rm -rf /tmp/restore-test
sudo mkdir -p /tmp/restore-test
tar -xJvf /home/wpbackup/wp_full.tar.xz -C /tmp/restore-test
Avoid absolute paths in tar archives
If you create an archive from an absolute path, tar can store leading directories that make restores confusing.
Preferred pattern:
tar -C /var/www/html -cJvf /home/wpbackup/wp-files.tar.xz .
Advanced tuning (optional)
If your tar build does not expose every XZ knob you want, you can use XZ_OPT to pass options to the underlying xz process.
XZ_OPT='-T0 -9' tar -C /var/www/html -cJf /home/wpbackup/wp-files-max.tar.xz .
Aggressive settings (-9, multi-threading, large sites) can spike CPU and I/O. Schedule this off-peak or lower the level.
Cheat sheet (extra)
| Task | Command |
|---|---|
| Create (relative paths) | tar -C /var/www/html -cJf out.tar.xz . |
| Extract to staging | tar -xJf out.tar.xz -C /tmp/restore-test |
| List contents | tar -tJf out.tar.xz |
| Test archive | xz -t out.tar.xz |
| Show stats | xz -l out.tar.xz |
When to use tar.xz
tar.xz is a "size-first" format:
- Good fit: cold storage snapshots, moving large sites over slow links, long retention backups.
- Avoid for: high-frequency operational backups where restore speed matters.
If you want a more operational default, consider tar.zst (often faster) or tar.gz (widest compatibility).
Prerequisites
sudo apt update
sudo apt install -y xz-utils tar
- Ensure
xz-utilsis installed (providesxzcompression engine). - Have enough disk space for uncompressed files.
- Verify access to your WordPress directory (
/var/www/htmlor/home/dev_wpstrategist/public_html).
Core syntax and common patterns
# =====================================================================
# 3. Core Syntax Formula — .tar.xz (WordPress VPS Context)
# =====================================================================
# ──────────────────────────────
# 1. Create an uncompressed archive (.tar)
tar -cvf [DESTINATION_PATH]/[ARCHIVE_NAME].tar [SOURCE_PATHS...]
# Example:
tar -cvf /home/wpbackup/sitefiles.tar /home/dev_wpstrategist/public_html
# Input directory: /home/dev_wpstrategist/public_html
# Output file: /home/wpbackup/sitefiles.tar
# ──────────────────────────────
# 2. Create an XZ-compressed archive (.tar.xz)
tar -cJvf [DESTINATION_PATH]/[ARCHIVE_NAME].tar.xz [SOURCE_PATHS...]
# Example:
tar -cJvf /home/wpbackup/sitefiles.tar.xz /home/dev_wpstrategist/public_html
# Input directory: /home/dev_wpstrategist/public_html
# Output file: /home/wpbackup/sitefiles.tar.xz
# ──────────────────────────────
# 3. Create maximum compression archive (.tar.xz)
tar -cJvf [DEST_PATH]/[ARCHIVE_NAME].tar.xz --xz -9 [SOURCE_PATHS...]
# Example:
tar -cJvf /home/wpbackup/wp_full_max.tar.xz --xz -9 /home/dev_wpstrategist/public_html
# Compression level: 9 (max)
# ──────────────────────────────
# 4. Extract a .tar.xz archive
tar -xJvf [INPUT_FILE.tar.xz] -C [OUTPUT_DIRECTORY]
# Example:
tar -xJvf /home/wpbackup/sitefiles.tar.xz -C /home/restore
# Input file: /home/wpbackup/sitefiles.tar.xz
# Output directory: /home/restore
# ──────────────────────────────
# 5. List contents without extracting
tar -tJvf [INPUT_FILE.tar.xz]
# Example:
tar -tJvf /home/wpbackup/sitefiles.tar.xz
# Output: List of files printed to terminal
# ──────────────────────────────
# 6. Test archive integrity
xz -t [INPUT_FILE.tar.xz]
# Example:
xz -t /home/wpbackup/sitefiles.tar.xz
# Output: "OK" if valid archive
# ──────────────────────────────
# 7. Decompress XZ layer only (keeping .tar)
xz -d [INPUT_FILE.tar.xz]
# Example:
xz -d /home/wpbackup/sitefiles.tar.xz
# Output file: /home/wpbackup/sitefiles.tar
# ──────────────────────────────
# 8. Compress existing .tar to .tar.xz
xz -z [INPUT_FILE.tar]
# Example:
xz -z /home/wpbackup/sitefiles.tar
# Output file: /home/wpbackup/sitefiles.tar.xz
# ──────────────────────────────
# 9. Create archive with date stamp
tar -cJvf /home/wpbackup/site_$(date +%F).tar.xz /var/www/html
# Output file: site_2025-10-26.tar.xz
# ──────────────────────────────
# 10. Backup WordPress + Database
mysqldump -u root -p'DB_PASS' wp_db > /home/tmp/db.sql && \
tar -cJvf /home/wpbackup/wp_full_$(date +%F).tar.xz /var/www/html /home/tmp/db.sql
# Input: WP files + DB dump
# Output file: /home/wpbackup/wp_full_YYYY-MM-DD.tar.xz
# =====================================================================
Key options
| Option | Description | Example | Effect |
|---|---|---|---|
-c | Create archive | tar -cvf file.tar folder | Start new archive |
-x | Extract archive | tar -xvf file.tar | Unpack files |
-t | List archive | tar -tvf file.tar | Show contents |
-v | Verbose mode | tar -cvf file.tar folder | Print file names |
-f | File name follows | tar -cvf archive.tar folder | Required before filename |
-J | Use XZ compression | tar -cJvf file.tar.xz folder | Compress with XZ |
--xz | Explicitly enable XZ mode | tar -c --xz -f file.tar.xz folder | Alternative to -J |
-C | Change directory | tar -xJvf file.tar.xz -C /home/restore | Extract to path |
--exclude | Skip file/directory | --exclude=cache | Ignore selected folder |
WordPress VPS use cases
| Scenario | Command | Goal |
|---|---|---|
| Full-site backup | tar -cJvf /home/wpbackup/wp_full.tar.xz /var/www/html | Complete compressed backup |
| File + DB backup | mysqldump -u root -pPASS wp_db > db.sql && tar -cJvf /home/wpbackup/full.tar.xz /var/www/html db.sql | Combine both data sources |
| Exclude cache folders | tar -cJvf backup.tar.xz --exclude=cache /var/www/html | Smaller archive |
| Restore backup | tar -xJvf backup.tar.xz -C /home/restore | Extract files |
| Verify archive | xz -t backup.tar.xz | Confirm integrity |
| Automate daily | tar -cJvf /home/wpbackup/wp_$(date +%F).tar.xz /var/www/html | Rotation-based backup |
Comparison
| Format | Command | Speed | Compression | File Size | Supported By |
|---|---|---|---|---|---|
.tar | tar -cvf | ⭐⭐⭐⭐ | ❌ None | Large | All Linux |
.tar.gz | tar -czvf | ⭐⭐⭐ | ⭐⭐ | Medium | All Linux |
.tar.xz | tar -cJvf | ⭐⭐ | ⭐⭐⭐⭐ | Smallest | Modern Linux |
.tar.zst | tar --zstd -cvf | ⭐⭐⭐⭐ | ⭐⭐⭐ | Medium | Ubuntu 22+/Debian 11+ |
Troubleshooting
| Issue | Cause | Fix |
|---|---|---|
tar: Removing leading '/' | Normal notice | Safe to ignore |
| “Cannot open: Permission denied” | Lacks permission | Use sudo |
| “Unexpected EOF” | Corrupted file | Redownload or re-upload |
| “No space left on device” | Disk full | Free space or mount new volume |
| “Command not found: xz” | xz-utils missing | Install using apt install xz-utils |
Quick lab
| Step | Command | Expected Output |
|---|---|---|
| 1 | cd /var/www/html | Enter WordPress directory |
| 2 | tar -cJvf /home/wpbackup/wp_full.tar.xz . | Creates compressed archive |
| 3 | tar -tJvf /home/wpbackup/wp_full.tar.xz | Lists archive content |
| 4 | xz -t /home/wpbackup/wp_full.tar.xz | Integrity test shows “OK” |
| 5 | tar -xJvf /home/wpbackup/wp_full.tar.xz -C /home/restore | Files extracted |
Cheat sheet
| Task | Syntax |
|---|---|
Create .tar.xz | tar -cJvf archive.tar.xz folder |
Extract .tar.xz | tar -xJvf archive.tar.xz |
| List contents | tar -tJvf archive.tar.xz |
| Test archive | xz -t archive.tar.xz |
| Exclude folder | tar -cJvf backup.tar.xz --exclude=cache folder |
| Compress existing tar | xz -z archive.tar |
| Decompress to tar | xz -d archive.tar.xz |
| Add date to filename | tar -cJvf backup_$(date +%F).tar.xz folder |
Mini quiz
| # | Question | Options | Answer |
|---|---|---|---|
| 1 | What does -J flag in tar do? | a) gzip b) xz c) zip | b) |
| 2 | How to list files inside a .tar.xz? | a) tar -xJvf b) tar -tJvf | b) |
| 3 | Which command tests .tar.xz integrity? | a) tar -t b) xz -t | b) |
| 4 | Which flag extracts to directory? | a) -C b) -v | a) |
| 5 | Best use case for .tar.xz? | a) Fastest backup b) Highest compression | b) |
Compression levels
tar.xz uses XZ compression levels from 0 (fastest) to 9 (smallest, slowest). The default is usually level 6; choose higher levels only when space matters more than time.
Range overview
| Level | Syntax | Speed | Compression Ratio | Use Case |
|---|---|---|---|---|
-0 | tar -cJvf backup.tar.xz --xz -0 folder | 🔥 Fastest | 😐 Lowest | For quick local archives |
-3 | tar -cJvf backup.tar.xz --xz -3 folder | ⚡ Fast | 🙂 Moderate | For daily backups |
-6 (default) | tar -cJvf backup.tar.xz folder | ⚖️ Balanced | ⚖️ Balanced | Default when level not set |
-9 | tar -cJvf backup.tar.xz --xz -9 folder | 🐢 Slowest | 🏋️♂️ Best | For maximum space savings |
Default level: -6 (medium balance between speed and size)
Example commands
Fast compression (levels 1-3)
tar -cJvf /home/wpbackup/wp_fast.tar.xz --xz -3 /var/www/html
- Faster archive creation
- Moderate compression
Standard compression (level 6)
tar -cJvf /home/wpbackup/wp_standard.tar.xz --xz -6 /var/www/html
- Default balance between CPU usage and compression ratio
- Ideal for regular WordPress backups
Maximum compression (level 9)
tar -cJvf /home/wpbackup/wp_max.tar.xz --xz -9 /var/www/html
- Slow compression (CPU-intensive)
- Best for cold storage or long-term archives
Verify compression ratio
After creating your archive, check compression ratio:
xz -l /home/wpbackup/wp_max.tar.xz
Expected Output Example:
Strms Blocks Compressed Uncompressed Ratio Check Filename
1 1 512.1 MiB 2.4 GiB 0.21 CRC64 wp_max.tar.xz
In this example, compression ratio = 0.21, meaning ~79% reduction.
Multi-threaded compression (optional)
By default, xz uses one CPU core.
For faster compression on multi-core VPS:
tar -cf - /var/www/html | xz -9 -T0 -c > /home/wpbackup/wp_multi.tar.xz
| Flag | Description |
|---|---|
-T0 | Use all available CPU cores |
-9 | Max compression |
-c | Write to stdout |
Quick reference
| Purpose | Command | Notes |
|---|---|---|
| Fastest | tar -cJvf backup.tar.xz --xz -1 folder | ~2× faster, larger size |
| Balanced | tar -cJvf backup.tar.xz folder | Default level 6 |
| Maximum | tar -cJvf backup.tar.xz --xz -9 folder | Slow but smallest |
| Multi-core max | `tar -cf - folder | xz -9 -T0 -c > backup.tar.xz` |
Verification commands
Run these right after creating an archive, before you upload it offsite.
xz -t /home/wpbackup/sitefiles.tar.xz
tar -tJf /home/wpbackup/sitefiles.tar.xz | sed -n '1,25p'
If both commands succeed and the listing looks like the paths you expect, the archive is usually safe to store.