To provide a clear and practical procedure for using the rsync utility to synchronize files and directories between local systems and remote hosts, including push and pull operations, while preserving file attributes and optimizing data transfer.
This article applies to Linux systems where rsync is used to copy and synchronize files or directories locally or between systems over SSH. It covers basic rsync syntax, commonly used options, source and destination path handling, and local, remote push, and remote pull operations.
rsync (Remote Synchronization) is a fast and efficient tool used to copy and synchronize files or directories between local systems or remote hosts. It optimizes data transfer by copying only the portions of files that have changed since the last synchronization, rather than transferring the entire file each time. This approach reduces bandwidth usage and significantly improves synchronization performance.
rsync syntax is really simple:rsync [OPTIONS] SOURCE DESTINATION
The following are some of the most useful options to add to the rsync command:
-v or --verbose: the verbose flag will display information about the progress of the task.-a or --archive: equals -rlptgoD. The archive flag sets the most common options instead of setting them individually.The following are the options set with -a that can also be set one by one:
-r or --recursive: recurses into directories. Must be used when you need to copy all the contents inside a directory.-l or --links: copies symlinks as symlinks, does not copy the file to which they point to avoiding loopbacks to other directories.-p or --perms: preserves the permission of the file.-t or --times: preserves the original modification timestamps.-g or --group: preserves the files' groups ownership.-o or --owner: preservers the files' owners.-D: preserves the device files and special files. You can use the options --devices and --specials to accomplish this.Some other options that can be used are:
-progress: displays the progress status per file.--max-size=SIZE: avoids transferring files larger than the specified size.--min-size=SIZE: avoids transferring files smaller than the specified size.It is important to take into consideration how the source and destination paths are specified since a slash can change the behavior.
For example, to copy everything inside a directory called in /home into another one called /new/destination/ the correct rsync syntax would be:
To copy the directory so that a new folder called home is also created in the destination directory the syntax would be:
Note that a single slash after /home will change the end result.
When having SSH access to a remote host it is possible to use rsync with a really similar syntax. To copy the contents from the /home directory to the /home/new directory in a host called REMOTE_HOST a push operation is needed. The syntax would be:
It is possible to retrieve files from a remote host to your local machine using rsync with a pull operation. The syntax is similar as the mentioned above but now the remote host is the source: