How to Use rsync with –max-size to Exclude Large Files from Backup


3 views

When performing system backups using rsync, large files (like database dumps, media files, or archives) can significantly slow down the transfer process. This becomes particularly problematic when you need to prioritize smaller, critical configuration files that are essential for system recovery.

rsync provides two powerful options for size-based filtering:

--max-size=SIZE  # exclude files larger than specified size
--min-size=SIZE  # exclude files smaller than specified size

These options accept size specifications in these formats:

  • K or k - kilobytes (1024 bytes)
  • M or m - megabytes
  • G or g - gigabytes

To sync only files smaller than 10MB:

rsync -avz --max-size=10m ~/ example.com:backup/

For a two-phase backup strategy:

# Phase 1: Small files first
rsync -avz --max-size=10m ~/ example.com:backup/

# Phase 2: All remaining files
rsync -avz ~/ example.com:backup/

Combine size filters with other rsync options for more control:

# Exclude files larger than 5MB but keep directories
rsync -avz --max-size=5m --include='*/' --exclude='*' ~/ example.com:backup/

# Sync files between 1KB and 10MB only
rsync -avz --min-size=1k --max-size=10m ~/ example.com:backup/

1. The size comparison is done on the file's actual size, not its disk usage

2. Directory sizes aren't considered - only individual files

3. For more complex filtering, consider combining with --exclude-from

For scenarios requiring more complex size-based logic:

find ~/ -type f -size -10M -print0 | rsync -avz --files-from=- --from0 ~/ example.com:backup/

When performing regular backups using rsync, large files (like database dumps, media files, or VM images) can significantly slow down the transfer process. This becomes particularly problematic when:

  • You're backing up over a slow network connection
  • You need quick access to critical configuration files
  • Storage space on the target machine is limited

The solution lies in rsync's --max-size parameter, which lets you specify a maximum file size for inclusion in the transfer:

rsync -avz --max-size=10m ~/ user@example.com:backup/

This command will only transfer files smaller than 10MB. The size suffix can be:

  • k or K for kilobytes
  • m or M for megabytes
  • g or G for gigabytes

For more complex backup scenarios, combine size filters with other rsync options:

# Two-phase backup (small files first, then everything)
rsync -avz --max-size=10m --exclude='*.tmp' ~/ user@example.com:backup/
rsync -avz --min-size=10m ~/ user@example.com:backup/

The --min-size parameter does the opposite, only transferring files larger than the specified size.

Case 1: Exclude large media files but keep config files

rsync -avz --max-size=5m --include='.*' --include='*.conf' --exclude='*' ~/ user@example.com:backup/

Case 2: Backup documents but skip large archives

rsync -avz --max-size=20m --include='*.doc' --include='*.pdf' --include='*.txt' --exclude='*' ~/Documents/ user@example.com:backup/

Remember that size filtering requires rsync to:

  • Scan all files before transfer
  • Maintain the file list in memory
  • Perform additional comparison operations

For directories with millions of files, consider combining with --prune-empty-dirs to skip scanning empty directories.

For ultimate control, use rsync's filter rules with size conditions:

rsync -avz --filter='- *SIZE/gt:10m' ~/ user@example.com:backup/

The filter syntax SIZE/gt:10m means "greater than 10MB" and the minus sign excludes matching files.