LOGO

How to Exclude Files from Rsync - A Comprehensive Guide

January 16, 2014
Topics:LinuxFiles
How to Exclude Files from Rsync - A Comprehensive Guide

Excluding Files During Rsync Operations

Rsync stands as a highly valuable tool for server administrators. However, its default behavior involves synchronizing all files, which can prove inconvenient when applications generate numerous temporary files.

Managing Rsync Exclusions

To address this, it's essential to understand how to exclude specific files during rsync operations. This ensures that only necessary data is transferred, optimizing performance and storage.

Several methods can be employed to achieve file exclusion. These include utilizing the --exclude option directly within the rsync command, or creating an exclude file containing patterns for files to be omitted.

  • Using the --exclude Option: This allows for specifying exclusion patterns directly on the command line.
  • Employing an Exclude File: A dedicated file lists the patterns of files and directories to be excluded. This approach is beneficial for complex exclusion rules.

The --exclude option accepts patterns that match filenames or directory names. Wildcards, such as '*', can be used to represent multiple characters, providing flexibility in defining exclusion criteria.

For instance, to exclude all files with the ".tmp" extension, you would use the following command-line argument: --exclude="*.tmp".

Alternatively, an exclude file might contain lines like:

*.log

temp/

cache/*

These entries would exclude all log files, the entire "temp" directory, and all files within the "cache" directory.

Implementing Exclusions

To utilize an exclude file, specify it with the --exclude-from option followed by the path to the file. For example: --exclude-from=/path/to/exclude.txt.

Properly configuring file exclusions in rsync is crucial for efficient server administration. It prevents unnecessary data transfer and ensures that backups and synchronizations focus on essential files.

Utilizing Exclusion Lists with Rsync

Employing an exclusion list represents a highly effective strategy for omitting specific files and directories during rsync operations. This method offers flexibility, allowing for easy modification and refinement of exclusions as needed.

Syntax for Exclusion Lists

The fundamental syntax for implementing this approach is as follows:

rsync --exclude-from=/path/to/exclusion-file /path/to/source /path/to/dest

A key aspect of rsync lies in its requirement for relative paths within exclusion lists. When matching exclusions, rsync disregards the initial portion of the source path, which can be counterintuitive.

Understanding Relative Paths

Consider a scenario where you are backing up the /data/web/ directory to a remote server using a command like rsync -a /data/web/ user@server:/backups/data/web/. If you wish to exclude the /data/web/cache/ folder, the exclusion list must reflect this relative structure.

Rsync, when evaluating exclusions, will not check for "/data/web/cache/" but rather "cache/" against the list. Therefore, the exclusion file should contain "cache", not the complete path.

Example Implementation

For instance, if you execute the following command:

rsync -a --exclude-from=/data/exclusions /data/web/ /backups/

To exclude both /data/web/cache and /data/web/temp, the /data/exclusions file would contain:

cache*

temp*

Utilizing Wildcards

The asterisk (*) serves as a wildcard, ensuring that any entry beginning with "cache" is matched. This pattern can be extended to exclude files based on their extensions.

To exclude all .txt files from synchronization, add the following to the exclusion list:

*.txt

This ensures that files matching this pattern are consistently skipped during the rsync process. The system is straightforward once this principle is understood.

Single Item Exclusion in Rsync

While less commonly employed, excluding a single item can be achieved dynamically when required. When configuring a script utilizing rsync, it is generally more efficient to define exclusions within a file for simplified future updates.

Exclusion Syntax

The command structure for excluding a specific item mirrors that of file-based exclusions. Consider the following example:

rsync --exclude=relative/path/to/exclusion /source /dest

Maintaining consistency, the relative path used in the exclusion should align with the path structure as previously discussed.

This method offers a quick solution for one-off exclusions, but a dedicated exclusion file remains the preferred approach for long-term manageability.

Employing a file for exclusions streamlines the process of modifying and maintaining your rsync configurations.

#rsync#exclude files#exclude directories#rsync exclude#rsync --exclude#rsync --exclude-from