Understanding Rsync Compression Levels: A Technical Guide for Developers


2 views

Rsync uses zlib compression by default when the -z or --compress flag is specified. The compression level can be fine-tuned using the --compress-level parameter, which accepts integer values.

The compression level in Rsync can range between:

  • 1 - Fastest compression (least effective)
  • 9 - Best compression (slowest)

Omitting the level defaults to 6, which provides a good balance between speed and compression ratio.

Basic compression:

rsync -az /source/ user@remote:/destination/

Using maximum compression (level 9):

rsync -az --compress-level=9 /source/ user@remote:/destination/

Fast transfer with minimal compression (level 1):

rsync -az --compress-level=1 /source/ user@remote:/destination/

Higher compression levels require more CPU resources but can significantly reduce network transfer time. Benchmarking different levels is recommended for large transfers:

time rsync -az --compress-level=1 /large-files/ remote:/dest/
time rsync -az --compress-level=9 /large-files/ remote:/dest/

For specific file types that don't compress well (e.g., already compressed formats like JPEG or ZIP), you might want to exclude them from compression:

rsync -az --compress-level=6 --exclude='*.jpg' --exclude='*.zip' /source/ remote:/dest/

Rsync's compression feature (-z or --compress) is particularly useful when transferring files over slow network connections. The --compress-level parameter allows fine-grained control over the compression algorithm's behavior.

The valid compression levels for rsync range from 1 to 9:

  • Level 1: Fastest compression (least effective)
  • Level 6: Default compression (balanced speed/ratio)
  • Level 9: Slowest compression (best compression ratio)

Basic usage with default compression level:

rsync -az /source/directory/ user@remote:/destination/

Explicitly setting compression level to maximum (9):

rsync -a --compress --compress-level=9 /source/ user@remote:/dest/

Using shorthand with custom level (level 3 for faster transfers):

rsync -az --compress-level=3 /src/ remote:/dst/

Higher compression levels:

  • Reduce network bandwidth usage
  • Increase CPU usage on both ends
  • May slow down transfers on fast networks

For local transfers or fast networks, consider disabling compression entirely:

rsync -a --no-compress /local/src/ /local/dest/

Use the --stats flag to see compression ratios:

rsync -az --stats --compress-level=6 /src/ remote:/dst/

Sample output showing compression:

Total bytes sent: 100,000
Total bytes received: 25,000
Compression saved 75,000 bytes (75% reduction)