In the world of technology, it is important to have a backup of your important files and data. This ensures that even if something goes wrong with your device, you can still retrieve your valuable information. One popular tool for creating backups is rsync. Rsync is a powerful command-line utility that allows you to efficiently synchronize and transfer files between different locations.
When using rsync for backup purposes, one common requirement is to exclude certain files or directories from the backup. This can be achieved using the --exclude option followed by the path or pattern of the files or directories you want to exclude. For example, if you want to exclude a directory named "temp" from the backup, you can use the following command:
rsync --exclude='temp/' source_directory/ destination_directory/
However, by default, rsync not only excludes the specified files and directories but also deletes them from the destination directory. This behavior can be problematic if you accidentally specify the wrong path or pattern and end up deleting important files from your backup.
To overcome this issue, rsync provides the --delete-excluded option. When this option is used, rsync will delete the excluded files and directories from the destination only if they already exist there. This means that if you accidentally exclude a file or directory, it will not be deleted from the destination unless it was already present before the rsync operation.
Let's understand this with an example. Suppose you have a source directory with the following files:
source_directory/
├── file1.txt
├── file2.txt
└── temp/
├── file3.txt
└── file4.txt
If you run the following rsync command:
rsync --exclude='temp/' source_directory/ destination_directory/
The destination directory will have the following files:
destination_directory/
├── file1.txt
└── file2.txt
As you can see, the "temp" directory and its contents are not present in the destination directory. Now, let's say you accidentally exclude the "file2.txt" file in your next rsync operation:
rsync --exclude='file2.txt' source_directory/ destination_directory/
Since the "file2.txt" file already exists in the destination directory, rsync will delete it, resulting in the following files in the destination directory:
destination_directory/
├── file1.txt
└── temp/
├── file3.txt
└── file4.txt
As you can see, the "file2.txt" file is no longer present in the destination directory.
By using the --delete-excluded option, you can ensure that accidental exclusions do not result in the deletion of important files from your backup. This option provides an extra layer of safety and prevents potential data loss.
It is important to note that the --delete-excluded option should be used with caution. Make sure to double-check your exclude patterns before running rsync to avoid unintentional deletions. Additionally, it is always a good practice to test your rsync commands on a small set of files or directories before performing a full backup to ensure that everything is working as expected.
Conclusion
Creating backups of your important files and data is crucial to protect against unexpected events. Rsync is a powerful tool that allows you to efficiently synchronize and transfer files between different locations. By using the --exclude option, you can exclude specific files or directories from the backup. However, be cautious as rsync deletes excluded files and directories by default. To prevent accidental deletions, you can use the --delete-excluded option, which ensures that excluded files and directories are only deleted from the destination if they already exist there. Remember to double-check your exclude patterns and test your rsync commands before performing a full backup to avoid any data loss.
References
| Source | Link |
|---|---|
| rsync man page | https://linux.die.net/man/1/rsync |
| How to Use rsync to Sync Local and Remote Directories on a VPS | https://www.digitalocean.com/community/tutorials/how-to-use-rsync-to-sync-local-and-remote-directories-on-a-vps |
| Understanding rsync's --delete-excluded Option | https://www.linux.com/training-tutorials/understanding-rsyncs-delete-excluded-option/ |