The rsync command securely and efficiently synchronizes files with a remote location.
After completing this section, students should be able to efficiently and securely synchronize the contents of a local file or directory with a remote copy.
The rsync tool is another way to securely copy files from one system to another. It differs from scp in that if two files or directories are similar between two systems, rsync only needs to copy the differences between the systems, while scp would need to copy everything.
One of the advantages of rsync is that it can copy files between a local system and a remote system securely and efficiently. While the initial synchronization of a directory takes about the same time as copying it, any subsequent synchronization only requires the differences to be copied over the network.
One of the most important options of rsync is the -n option to perform a dry run. A dry run is a simulation of what happens when the command really gets executed. It will display the changes it will perform when the command is executed without the dry run option. It is recommended to perform a dry run of any rsync operation to ensure no important files get overwritten or deleted.
The two most common options when synchronizing files and folders with rsync are the -a and -v options. While the -v option adds verbosity to the output as the synchronization proceeds, the -a option stands for "archive mode" and enables the following options all in one:
-r, synchronize recursively the whole directory tree
-l, synchronize symbolic links
-p, preserve permissions
-t, preserve time stamps
-g, preserve group ownership
-o, preserve the owner of the files
-D, synchronize device files
While the -a already synchronizes symbolic links, there are additional options necessary to preserve hardlinks, as they are treated as separate files instead. The -H option enables the handling of hardlinks, so the rsync command will identify the hardlinks present in the source folder and link the files accordingly in the destination folder instead of just copying them as separate files.
The -a option does not synchronize advanced file permissions, such as ACLs or SELinux file contexts. To enable the synchronization of ACLs, the -A option is required in addition to the -a option, while to synchronize the SELinux contexts from the source files to the target files, the -X option is to be added.
The basic way of using rsync is to synchronize two local folders. In the following example, the /var/log directory gets a synchronized copy in the /tmp folder. The log directory with its content is created in the /tmp directory.
[student@desktopX ~]$su -Password:redhat[root@desktopX ~]#rsync -av /var/log /tmp...
To only synchronize the content of a folder without newly creating the folder in the target directory, a trailing slash needs to be added at the end of the source directory. In this example, the log directory is not created in the /tmp folder. Only the content of the /var/log/ directory is synchronized into the /tmp folder.
[root@desktopX ~]#rsync -av /var/log/ /tmp...
When entering the source directory for rsync, it is very important to remember that whether a trailing slash is present on the directory name matters. It will determine whether the directory or just the contents of the directory are synchronized to the target. Note: Tab-completion will automatically add a trailing slash to the end of directory names.
Similar to scp, the rsync command expects remote file system locations to be specified in the format [user@]host:/path. In case the optional user@ portion is missing, the user invoking the rsync command is used to connect to the remote location. It is possible to use a remote location as either source or target. In the following example, the local folder /var/log gets a synchronized copy in the /tmp directory on the serverX machine. For rsync to synchronize ownership of the transferred files, the target location must be written as user root, so connect to the remote system serverX as the root user. The connecting user root must then authenticate with the SSH server by any of the accepted methods; for example, password or SSH keys.
[root@desktopX ~]$rsync -av /var/log serverX:/tmproot@serverX's password:redhat...
In the same way, the remote folder /var/log on serverX can be synchronized to the local directory /tmp on desktopX:
[root@desktopX ~]$rsync -av serverX:/var/log /tmproot@serverX's password:redhat...
rsync(1) man page