Using rsync include-from to Sync Specific Files or Directories
Rsync is a powerful command-line tool that allows you to efficiently synchronize files and directories between different locations. It is widely used in the tech industry for backup and remote file transfer purposes. One of the useful features of rsync is the ability to specify which files or directories to include or exclude during the synchronization process. In this article, we will explore how to use the include-from option in rsync to sync specific files or directories.
Understanding rsync include-from
The include-from option in rsync allows you to specify a file that contains a list of patterns. These patterns define which files or directories should be included in the synchronization process. By default, rsync includes all files and directories in the source location. However, by using the include-from option, you can narrow down the synchronization to only the files or directories that match the specified patterns.
Creating the include-from file
The first step in using the include-from option is to create a file that contains the patterns for the files or directories you want to include. This file can be created using any text editor. Each pattern should be on a separate line in the file. The patterns can be simple file or directory names or can use wildcards to match multiple files or directories.
For example, let's say you want to sync only the files with the .txt extension from the source directory. You can create an include-from file called include.txt with the following content:
*.txt
Using rsync include-from
Once you have created the include-from file, you can use it with the rsync command to sync only the specified files or directories. The basic syntax of the rsync command with include-from is as follows:
rsync --include-from=include.txt source_directory/ destination_directory/
In the above command, replace include.txt with the path to your actual include-from file. Also, replace source_directory with the path to the source directory you want to sync and destination_directory with the path to the destination directory.
For example, if your include-from file is located in the same directory as the rsync command and you want to sync the files from the source directory /home/user/documents to the destination directory /backup, the command would be:
rsync --include-from=include.txt /home/user/documents/ /backup
This command will sync only the files with the .txt extension from the source directory to the destination directory.
Using wildcards in include-from patterns
As mentioned earlier, you can use wildcards in the include-from patterns to match multiple files or directories. The most commonly used wildcards are the asterisk (*) and the question mark (?).
The asterisk (*) represents any number of characters and can be used to match multiple files or directories. For example, if you want to sync all files with the .docx extension, you can use the following pattern in your include-from file:
*.docx
The question mark (?) represents a single character and can be used to match a single character in a file or directory name. For example, if you want to sync all files with a three-letter extension, you can use the following pattern:
*???
This pattern will match any file with a three-letter extension, such as file.txt, image.jpg, etc.
Conclusion
The rsync include-from option is a powerful tool that allows you to sync specific files or directories during the synchronization process. By creating an include-from file with the desired patterns, you can narrow down the synchronization to only the files or directories that match those patterns. This can be especially useful when you only want to sync a subset of files or directories from a large source location. Experiment with different patterns and wildcards to customize your rsync sync operations.
| Reference | Link |
|---|---|
| rsync man page | https://linux.die.net/man/1/rsync |
| rsync tutorial | https://www.tecmint.com/rsync-local-remote-file-synchronization-commands/ |