rsync or remote synchronization is a software utility for Unix-Like systems that efficiently sync files and directories between two hosts or machines. One of them being the source or the local-host from which the files will be synced, the other one being the remote-host, on which synchronization will take place. There are basically two ways in which rsync can copy/sync data:
- Copying/syncing to/from another host over any remote shell like ssh, rsh.
- Copying/Syncing through rsync daemon using TCP.
Rsync is famous for its delta-transfer algorithm, in which it copies only the differences between the source files present in the local-host and the existing files in the destination or the remote host.
rsync local-file user@remote-host:remote-file
What Happens here: Rsync will first use SSH to connect as
user to remote-host and will ask for
user's password. Once connected, it will invoke the remote host’s rsync and then the two programs will determine what parts of the local-file needs to be copied so that the remote file matches the local one. Please note the following behavior of rsync:
- Files that do not exist on the remote-host are copied.
- Files that have been updated will be synced, rsync will copy only the changed parts of files to the remote host.
- File that is exactly the same are not copied to the remote host at all.
Syntax of rsync:
rsync [options] source [destination]
- -a, –archieve: This is equivalent to using -rlptgoD. Archive mode includes all the necessary options like copying files recursively, preserving almost everything (like symbolic links, file permissions, user & group ownership and timestamps).
Note: The archive mode does not preserve hard links, because finding multiply-linked files is expensive. A -H options must be explicitly specified for hard links.
- -v, –verbose: By default, rsync works silently. A single -v will give us information about what files are being transferred and a brief summary about the data transferred at the end. Two -v options will give us information on the status of delta-transmission and on what files are up to date so as to be skipped and slightly more information at the end. More than two -v options are generally used for debugging rsync.
- -h, –human-readable format: Outputs in a human readable format.
-h, --human-readable format
- -z, –compress: Compress file data during the transfer
The above command will list the files and directories present in the directory foo.
rsync -avh foo/ bar/
The above command will copy/sync all the files and directories present in directory
foo to directory bar. If the destination directory is not present (here
bar), rsync automatically creates one and copies all the data in it.
- Using a remote-shell program as the transport(such as
ssh(Secure Shell) or
- Contacting an rsync daemon directly via TCP.
Here we will be discussing rsync over ssh.
rsync -avhze ssh /foo user@remote-host:/tmp/
To specify the type of protocol to be used,
-e option is used.
rsync -avhe ssh --chown=USER:GROUP /foo user@remote-host:/tmp/
The above command will sync all the files present in directory /foo with the files present in directory /tmp in the
remote-host with all the files owned by USER with group GROUP.
group must already be created in the remote-host.
--ignore-existing-files:We can also skip the already existing files on the destination. This can generally be used when we are performing backups using the –link-dest option, while continuing a backup run that got interrupted.
rsync --ignore-existing -avhe /foo user@remote-host:/tmp/
So any files that do not exist on the destination will be copied over. Here I have deleted the
geeksforgeeks folder from the directory
foo, so it should copy only the
Note: This does not ignore existing directories, or nothing would get done. Even if there are some changes in a file in the local host, it still would not be synced if its present on the remote host.
rsync -avhe ssh --progress /foo user@remote-host:/tmp/
When the transfer completes for a particular file, rsync outputs a summary line as shown below.
In the above image, if we look at the file
/foo/file2, it tells us that
- The file was 44 bytes long in total.
- The average rate of transfer for the whole file was 42.97 kilobytes per second over the 0:00:00 seconds that it took to complete.
- It was the second transfer during the current rsync session.
- There are 10 more files for the remote-host to check (to see if they are up-to-date or not) remaining out of the 13 total files in the file-list.
- Files that do not exist on the remote-host are copied.
- Files that exist on both local and remote but have a newer timestamp on the local-host are copied to remote-host. (Conversely, files that have an older timestamp are not copied).
Here, I made some changes in file1 and file2, but the changes in file2 were done recently. So only, file2 will get synced.
rsync -avhe ssh --progress --update /foo root@remote-host:/tmp/
--remove-source-filesflag to automatically delete the files from the web server.
rsync -avhe ssh --remove-source-files /foo user@backup-server:/tmp
Note: This will delete only the files and not the directories.
rsync -avhe ssh /foo --delete user@remote-host:/tmp/
So, here file1, file2, file3 were deleted on the local-host, and as can be seen, are updated in the remote-host as well.
Note:rsync does not delete the files automatically to sync the directories present on both sides.
rsync -avhe ssh --dry-run --chown=USER:GROUP /foo user@remote-host:/
- man command in Linux with Examples
- last command in Linux with Examples
- Cat command in Linux with examples
- until command in Linux with Examples
- gdb command in Linux with examples
- sum command in Linux with Examples
- cvs command in Linux with Examples
- apt-get command in Linux with Examples
- wc command in Linux with examples
- pwd command in Linux with Examples
- df command in Linux with Examples
- w command in Linux with Examples
- ZIP command in Linux with examples
- env command in Linux with Examples
- ed command in Linux with examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.