Excluding Files & Directories in Rsync for Efficient Backups
π
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