xz - High Ratio Compression
xz provides very high compression ratios (LZMA2), but it is typically slower than gzip and zstd. Use it when bandwidth or storage cost matters more than backup and restore time.
- Install:
xz-utils(Debian/Ubuntu) orxz(RHEL family). - Compress:
xz file.sql->file.sql.xz - Decompress:
xz -d file.sql.xzorunxz file.sql.xz - Test:
xz -t file.sql.xz - Directory backups: use
tar -cJf out.tar.xz dir/
When to use xz
Use xz when:
- You want maximum compression for cold storage.
- You are compressing large, mostly-static files (SQL dumps, logs, snapshots).
Avoid xz for high-frequency operational backups where restore speed is critical; prefer zstd or gzip.
Prerequisites
-
Ubuntu 22 / 24 LTS VPS with
xz-utilsinstalled. -
Familiarity with Linux file paths and permissions.
-
WordPress file access such as:
/home/dev_wpstrategist/public_html/,/home/wpbackup/,/var/log/.
Core syntax
# ──────────────────────────────
# 1. Compress a single file
xz [OPTION] [INPUT_FILE]
# Example:
xz /home/dev_wpstrategist/error.log
# → Output: /home/dev_wpstrategist/error.log.xz
# ──────────────────────────────
# 2. Decompress a .xz file
unxz [INPUT_FILE.xz]
# or
xz -d [INPUT_FILE.xz]
# Example:
xz -d /home/dev_wpstrategist/error.log.xz
# ──────────────────────────────
# 3. Compress with specific level (0–9)
xz -# [INPUT_FILE]
# Example (maximum):
xz -9 /home/wpbackup/db.sql
# → Output: /home/wpbackup/db.sql.xz
# ──────────────────────────────
# 4. Keep original file after compression
xz -k [INPUT_FILE]
# ──────────────────────────────
# 5. Compress to specific directory
xz -c [INPUT_FILE] > [OUTPUT_PATH]/[OUTPUT_NAME].xz
# Example:
xz -c /home/dev_wpstrategist/db.sql > /home/wpbackup/db_$(date +%F).sql.xz
# ──────────────────────────────
# 6. Combine with tar to create .tar.xz archive
tar -cJvf [OUTPUT_FILE.tar.xz] [SOURCE_DIRECTORY]
# Example:
tar -cJvf /home/wpbackup/wpfiles_$(date +%F).tar.xz /home/dev_wpstrategist/public_html
# ──────────────────────────────
# 7. Extract .tar.xz archive
tar -xJvf [INPUT_FILE.tar.xz] -C [TARGET_DIRECTORY]
# Example:
tar -xJvf /home/wpbackup/wpfiles_2025-10-25.tar.xz -C /home/dev_wpstrategist/public_html/
# ──────────────────────────────
# 8. Export & compress MySQL DB
mysqldump -u [USER] -p'[PASS]' [DB_NAME] | xz -9 -c > /home/wpbackup/db_$(date +%F).sql.xz
# ──────────────────────────────
# 9. Import DB from .xz
xz -dc [INPUT_FILE.sql.xz] | mysql -u [USER] -p'[PASS]' [DB_NAME]
# ──────────────────────────────
# 10. Test integrity of .xz file
xz -t [INPUT_FILE.xz]
Key options
| Option | Description | Example | Use Case | CPU Impact |
|---|---|---|---|---|
-# | Compression level (0–9) | xz -9 db.sql | Higher = smaller, slower | 🟡–🔴 |
-d | Decompress | xz -d file.xz | Restore archive | 🟢 |
-k | Keep original | xz -k db.sql | Keep uncompressed copy | 🟢 |
-t | Test integrity | xz -t file.xz | Verify before restore | 🟢 |
-v | Verbose mode | xz -v file.sql | Show progress/stats | 🟢 |
-c | Write to stdout | xz -c file > out.xz | Redirect to other directory | 🟢 |
-T# | Multithreaded compression | xz -T4 db.sql | Faster on multi-core VPS | 🟡 |
tar -cJf | Create tar.xz archive | tar -cJf backup.tar.xz dir/ | Full WP backup | 🔴 |
Examples
Compress Single File
xz wp-config.php
Output:
wp-config.php (1/1)
Compressed size: 3.4 KiB → 1.7 KiB (50.0%)
Use Case: Small config file backup.
CPU Impact: Low.
Max Compression Level
xz -9 wpdb.sql
Output:
wpdb.sql (1/1)
Compressed size: 150 MB → 10.9 MB (92.7%)
Use Case: Deep compression for archival DB backup.
CPU Impact: High (slow, heavy CPU).
Keep Original
xz -k wpdb.sql
Output:
wpdb.sql
wpdb.sql.xz
Use Case: Retain raw and compressed copies.
CPU Impact: .
Full WordPress Backup as .tar.xz
tar -cJvf /home/wpbackup/wp_2025-10-25.tar.xz /home/dev_wpstrategist/public_html
Output (snippet):
public_html/wp-admin/
public_html/wp-content/
Compression ratio: 85.3% (480 MB → 70 MB)
Use Case: Compress entire WP directory.
CPU Impact: .
Extract Backup
tar -xJvf /home/wpbackup/wp_2025-10-25.tar.xz -C /home/dev_wpstrategist/public_html
Output:
All files restored successfully.
Use Case: Restore full site archive.
CPU Impact: .
Verify Backup Integrity
xz -t /home/wpbackup/db_2025-10-25.sql.xz && echo "Valid file"
Output:
Valid file
Use Case: Confirm backup not corrupted.
CPU Impact: .
Stream Compression for Remote VPS
tar -cJf - /home/dev_wpstrategist/public_html | ssh root@remote "cat > /backup/wp_$(date +%F).tar.xz"
Use Case: Send compressed backup directly via SSH.
CPU Impact: – (depends size and cores).
WordPress VPS use cases
| Use Case | Description | Example | Benefit |
|---|---|---|---|
| DB Backups | Compress SQL dumps | `mysqldump … | xz -9 -c > …` |
| Site Backups | Tar + XZ archives | tar -cJvf backup.tar.xz /var/www/html | Minimal size |
| Logs | Compress old logs | xz --keep /var/log/nginx/*.log | Free disk |
| Remote Sync | Rsync .xz archives | rsync -avz backup.tar.xz remote:/ | Save bandwidth |
| Cron Jobs | Nightly automation | 0 2 * * * tar -cJf backup_$(date +%F).tar.xz … | Auto compress daily |
Best practices
- Use
xz -9for final long-term archives, butxz -6for daily backups. - Always run
xz -tbefore restore. - Use
Tto leverage multi-core compression (xz -T4= 4 threads). - Combine with
tar -cJfto pack directories and files together. - Use consistent date naming in automation (e.g.,
backup_$(date +%F).tar.xz). - Avoid max level during high VPS load (CPU heavy).
Quick lab
-
Dump and Compress Database
quick-lab-dump-and-compress-db.shmysqldump -u root -p wpdb > wpdb.sqlxz -9 wpdb.sql -
Test Archive
quick-lab-test-xz-file.shxz -t wpdb.sql.xz && echo "Backup OK" -
Restore
quick-lab-restore-db-from-xz.shxz -dc wpdb.sql.xz | mysql -u root -p wpdb -
Automate Daily Backup
quick-lab-automate-daily-tar-xz.shtar -cJvf /home/wpbackup/wp_$(date +%F).tar.xz /home/dev_wpstrategist/public_html
Cheat sheet
xz file # Compress → file.xz
xz -d file.xz # Decompress
xz -k file # Keep original
xz -9 file # Max compression
xz -t file.xz # Test integrity
xz -v file # Verbose output
xz -T4 file # Use 4 threads
tar -cJvf a.tar.xz dir/ # Create .tar.xz
tar -xJvf a.tar.xz -C /target # Extract
Mini quiz
- Which algorithm powers
xzcompression? - What does
tar -cJfdo? - How can you verify if an
.xzfile is valid? - Which flag enables multithreaded compression?
- What level should be used for routine backups vs archival storage?
Restore checklist
Use this checklist before restoring a .tar.xz or .sql.xz backup into a live system.
- Test integrity (
xz -t ...). - Extract to staging (
/tmp/restore-test/). - Confirm expected files exist before copying into place.
sudo rm -rf /tmp/restore-test
sudo mkdir -p /tmp/restore-test
tar -xJvf /home/wpbackup/wp_2025-10-25.tar.xz -C /tmp/restore-test
Advanced tuning (optional)
If you want to pass options to the underlying xz process when creating .tar.xz, you can use XZ_OPT.
XZ_OPT='-T0 -6' tar -C /var/www/html -cJf /home/wpbackup/wp-files.tar.xz .
High compression levels can slow restores. Use xz primarily for cold storage and keep an operational format (for example tar.zst) for fast recovery.