Excluding Files & Directories in Rsync for Efficient Backups

"Stability is the goal of IT operations, but anomalies are the daily reality."
Photo by Spenser Sembrat / Unsplash

πŸš€

When performing backups or data synchronization using Rsync, you often don't need to copy every file. Large logs, cache files, and temporary data can waste bandwidth and storage. Using Rsync’s exclude options, you can optimize backups by ignoring unnecessary files and directories.

πŸ“Œ In this guide, you will learn:
βœ… How to exclude specific files and directories in Rsync
βœ… How to use exclude patterns for efficient backups
βœ… Step-by-step hands-on exercises for practical use
βœ… How to manage large-scale exclusions using an exclude file


πŸ›‘ 1. Why Exclude Files & Directories?

Not all files need to be included in a backup or sync operation. Common reasons for excluding files include:

πŸ”Ή Reducing backup size – Avoid copying large, unnecessary files like logs and cache.
πŸ”Ή Improving performance – Syncing fewer files reduces disk I/O and network bandwidth usage.
πŸ”Ή Preventing conflicts – Exclude system files that should not be overwritten or synced.

βœ… Example: Exclude log files and temporary files

rsync -av --exclude='*.log' --exclude='tmp/' /source/ /backup/

πŸ“Œ This will skip all .log files and the tmp/ directory during synchronization.


πŸ” 2. Using --exclude to Ignore Files & Directories

Rsync allows you to specify files or directories to exclude from synchronization using the --exclude option.


πŸ”Ή 2.1 Excluding a Single File

βœ… Example: Exclude a specific file (debug.log)

rsync -av --exclude='debug.log' /source/ /backup/

πŸ“Œ This skips only the debug.log file but syncs everything else.


πŸ”Ή 2.2 Excluding a Directory

βœ… Example: Exclude a specific directory (cache/)

rsync -av --exclude='cache/' /source/ /backup/

πŸ“Œ This prevents the entire cache/ directory from being copied.

βœ… Example: Exclude multiple directories

rsync -av --exclude='cache/' --exclude='tmp/' /source/ /backup/

πŸ“Œ Now both cache/ and tmp/ are ignored.


πŸ”Ή 2.3 Using Wildcards to Exclude Multiple Files

βœ… Example: Exclude all .log and .tmp files

rsync -av --exclude='*.log' --exclude='*.tmp' /source/ /backup/

πŸ“Œ This skips any files ending in .log or .tmp.

βœ… Example: Exclude all .bak files in any subdirectory

rsync -av --exclude='**/*.bak' /source/ /backup/

πŸ“Œ The **/ wildcard ensures that .bak files are excluded from all subdirectories.


⚑ 3. Managing Large Exclusions with --exclude-from

Instead of listing multiple --exclude options in a command, store exclusion patterns in a file for easier management.


πŸ”Ή 3.1 Creating an Exclude File

βœ… Step 1: Create a file (exclude-list.txt)

nano /home/user/exclude-list.txt

βœ… Step 2: Add exclusion patterns

*.log
cache/
tmp/
*.bak

πŸ“Œ Each line represents a file or directory to be ignored.

βœ… Step 3: Use the exclusion file with Rsync

rsync -av --exclude-from='/home/user/exclude-list.txt' /source/ /backup/

πŸ“Œ Now Rsync will exclude all patterns listed in exclude-list.txt.

βœ… Verify excluded files

rsync -av --exclude-from='/home/user/exclude-list.txt' --dry-run /source/ /backup/

πŸ“Œ Using --dry-run previews the exclusion before making changes.


πŸ› οΈ 4. Common Exclusion Use Cases

Below are real-world scenarios where excluding files is useful.


πŸ”Ή 4.1 Excluding Hidden Files & Directories

Most Linux systems use hidden files (.dotfiles) for configurations. If you don’t want to sync hidden files, exclude them.

βœ… Example: Exclude all hidden files and directories (.*)

rsync -av --exclude='.*' /source/ /backup/

πŸ“Œ Prevents .git/, .ssh/, and other hidden folders from syncing.


πŸ”Ή 4.2 Excluding System-Specific Files

Some files should not be copied, such as:

  • Proc and dev files (/proc, /dev, /sys)
  • Virtual filesystems (/mnt, /media)
  • Temporary system files (/var/tmp, /var/cache)

βœ… Example: Exclude system directories

rsync -av --exclude='/dev/' --exclude='/proc/' --exclude='/sys/' / /backup/

πŸ“Œ This avoids copying virtual system files.


πŸ”Ή 4.3 Excluding Large Unnecessary Files

If you only need documents, you might exclude:

  • Media files (.mp4, .mkv)
  • Backup archives (.zip, .tar.gz)
  • Database dumps (.sql)

βœ… Example: Exclude large media files

rsync -av --exclude='*.mp4' --exclude='*.mkv' /source/ /backup/

βœ… Example: Exclude backup archives

rsync -av --exclude='*.zip' --exclude='*.tar.gz' --exclude='*.sql' /source/ /backup/

πŸ›‘οΈ 5. Troubleshooting Rsync Exclusions

Problem Solution
Excluded files are still being copied Check if the pattern is correct (e.g., tmp/ vs. tmp)
Hidden files are not excluded Use --exclude='.*' to target dotfiles
Exclusion file is not working Verify with --dry-run before execution
Nested directories are still syncing Use --exclude='**/cache/' to match subdirectories

βœ… Test exclusions before running Rsync

rsync -av --exclude='*.tmp' --dry-run /source/ /backup/

πŸ“Œ This simulates the process without making actual changes.


πŸ“Š 6. Summary

Exclusion Method Use Case
--exclude='file' Exclude a single file
--exclude='dir/' Exclude an entire directory
--exclude='*.ext' Exclude all files with a specific extension
--exclude='**/*.ext' Exclude files recursively in subdirectories
--exclude-from=file Manage large exclusion lists easily

βœ… Using Rsync’s exclude options ensures faster, more efficient backups without unnecessary data.


πŸ’¬ Join the Discussion!

Have you used Rsync’s exclude feature before?
What types of files do you exclude from backups?

πŸ’¬ Share your experience in the comments below! πŸš€

πŸ‘‰ Next Up: Using Rsync for Large-Scale Server Migrations


Read more