In today’s technological era, data backups are a vital necessity for everyone. With an abundance of data, we need to ensure that our data is always safe, secure, and backed up. In this article, we will explore an incredibly powerful and versatile tool, rsync, which is widely used for backup and synchronization purposes the the system administration.
What is Rsync?
Rsync is a free and open-source utility software that is used for file synchronization and data backup. It efficiently copies and syncs data between two directories, either on the same system or across multiple systems over a network. Rsync uses a delta encoding technique to transfer data more efficiently, which means it only transfers the changes made to the files and not the whole file.
Advantages of Rsync:
Rsync has several advantages over other backup and synchronization tools. Let’s take a look at some of them:
- Efficient Backup: Rsync is known for its efficiency in backing up data. It transfers only the changed parts of the file, which reduces the transfer time and network bandwidth usage.
- Cross-Platform Support: Rsync works on various platforms such as Linux, macOS, Windows, and others. It can also be used to synchronize data between different platforms.
- Secure Data Transfer: Rsync supports encryption, SSH, and tunneling protocols for secure data transfer.
- Backup Automation: Rsync can be used to automate backups, which saves time and effort.
- Preserves Permissions and Ownership: Rsync preserves file permissions and ownership, which is crucial for system administrators.
In this article, we’ll take a closer look at some of the useful rsync command line examples that can make your life easier. Rsync is a powerful utility for copying, synchronizing, and backing up files and directories in Linux. Here are 15 useful rsync command line examples and their detailed explanations that may useful for system administrators in Oracle Linux 8:
1. Copy files locally:
# rsync -av /path/to/source/ /path/to/destination/
This command will copy all files and directories from the source directory to the destination directory using rsync’s archive mode, which preserves file permissions, ownership, timestamps, and symbolic links.
2. Copy files remotely over SSH:
# rsync -av -e ssh /path/to/source/ [email protected]:/path/to/destination/
This command will copy all files and directories from the source directory to the remote host linodelinux.com using SSH encryption, while preserving file attributes.
3. Sync files between two directories:
# rsync -av --delete /path/to/source/ /path/to/destination/
This command will synchronize the contents of the source directory with the destination directory, deleting any files in the destination that are not in the source.
4. Copy only new or changed files:
# rsync -av --update /path/to/source/ /path/to/destination/
This command will copy only the files that are new or have been changed in the source directory to the destination directory, while preserving file attributes.
5. Exclude certain files or directories:
# rsync -av --exclude 'file.txt' --exclude 'dir/' /path/to/source/ /path/to/destination/
This command will copy all files and directories from the source directory to the destination directory, excluding the file.txt and dir/ directories from the transfer.
6. Limit the bandwidth used:
# rsync -av --bwlimit=1000 /path/to/source/ /path/to/destination/
This command will limit the transfer bandwidth to 1000 kilobits per second, which can be useful when transferring files over a slow network connection.
7. Transfer files in compressed format:
# rsync -avz /path/to/source/ /path/to/destination/
This command will compress the files during transfer using rsync’s built-in compression, which can speed up the transfer over slow network connections.
8. Transfer files using checksums:
# rsync -avc /path/to/source/ /path/to/destination/
This command will transfer files based on their checksums, which ensures that the files in the source and destination directories are exactly the same, even if the file timestamps and sizes differ.
9. Transfer files with progress information:
# rsync -av --progress /path/to/source/ /path/to/destination/
This command will display progress information during the transfer, including the amount of data transferred, the transfer speed, and the estimated time remaining.
10. Transfer files while excluding existing files:
# rsync -avn --ignore-existing /path/to/source/ /path/to/destination/
This command will display what would happen if files were transferred, but will not actually transfer any files that already exist in the destination directory.
11. Transfer files while preserving hard links:
# rsync -avH /path/to/source/ /path/to/destination/
This command will preserve hard links during the transfer, which can be useful when transferring files that have hard links, such as backups.
12. Transfer files with verbose output:
# rsync -avv /path/to/source/ /path/to/destination/
This command will display verbose output during the transfer, including the file names, sizes, and transfer speeds.
13. Transfer files while excluding hidden files:
# rsync -av --exclude='.*' /path/to/source/ /path/to/destination/
This command will exclude hidden files (those that begin with a dot) from the transfer, while copying all other files and directories.
14. Transfer files while preserving extended attributes:
# rsync -avX /path/to/source/ /path/to/destination/
This command will preserve extended attributes, such as ACLs and SELinux contexts, during the transfer.
15. Dry run of the transfer:
# rsync -avn /path/to/source/ /path/to/destination/
This command will perform a dry run of the transfer, displaying what would happen if files were transferred, but not actually transferring any files. This is useful for testing the command before performing a real transfer.
Conclusion :
Rsync is a powerful tool for copying and synchronizing files and directories in Linux. These 15 command line examples and explanations should help system administrators in Oracle Linux 8 to make effective use of rsync in their tasks. Rsync is a versatile and powerful tool that can help you manage your files and directories with ease. By using the right rsync command line options, you can streamline your file transfers, automate your backups, and keep your data in sync across multiple devices. Whether you’re a seasoned Linux administrator or a beginner, mastering rsync is a skill worth learning. We hope that the examples we’ve shared in this article have given you a solid foundation for exploring rsync further and taking your Linux skills to the next level. Good luck!!
Here is a table format explanation of some common rsync command line options:
Option | Explanation |
---|---|
-a | Archive mode – preserves permissions, ownership, timestamps, and symlinks |
-v | Verbose mode – displays detailed information about the transfer |
-z | Compresses the data being transferred |
-r | Recursive mode – transfers files and directories recursively |
-u | Update mode – skips files that are newer in the destination directory |
-h | Human-readable output – displays file sizes in a human-readable format |
-n | Dry-run mode – shows what would be transferred without actually transferring anything |
-P | Shows progress during the transfer and resumes a partially completed transfer |
–delete | Deletes files in the destination that are not in the source |
–exclude | Excludes specified files or directories from the transfer |
–bwlimit | Limits the bandwidth used for the transfer |
–link-dest | Uses a hard link to a previous backup for files that haven’t changed |
–dry-run | Shows what would be transferred without actually transferring anything (same as -n) |
–chmod | Applies the specified permissions to transferred files |
–timeout | Sets the I/O timeout in seconds for the transfer |
Note: This is not an exhaustive list of all rsync options, and the options may vary depending on the version of rsync you are using. It is always recommended to refer to the rsync manual for more information on specific options.