Last Updated : 31 Jul, 2023
rsync or remote synchronization is a software utility for Unix-Like systems that efficiently sync files and directories between two hosts or machines. One is the source or the local-host from which the files will be synced, the other is the remote-host, on which synchronization will take place. There are basically two ways in which rsync can copy/sync data:
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.
Example:
rsync local-file user@remote-host:remote-file
What Happens Here:
Rsync will first use SSH to connect as the 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 need to be copied so that the remote file matches the local one. Please note the following behavior of rsync:
rsync [options] source [destination]Option available in `rsync` command in Linux
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).
By default, rsync operates silently. Using a single "-v" option provides information on transferred files and a summary at the end. Adding two "-v" options gives status updates on delta-transmission and skipped files, along with more information at the end. Multiple "-v" options are typically used for debugging rsync.
Outputs in a human readable format.
Compress file data during the transfer
Examples Using `rsync` as a list commandIf only the source path is specified, the contents of the source are listed in an output format similar to ls -l
.
rsync foo/The above command will list the files and directories present in the directory foo.
Output:
Copy/Sync files and directory locallyIf neither the source or destination path specifies a remote host, the rsync commands behave as a copy command.
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.
Output:
Rsync using sshThere are two different ways for rsync to contact a remote system:
ssh
(Secure Shell) or rsh
(Remote Shell)).rsync -avhze ssh /foo user@remote-host:/tmp/To specify the type of protocol to be used,
-e
option is used.
Output:
Rsync with particular file permissionsIf we want to sync files to the local or remote host with the permissions of the files being changed. The following command must be 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.
Output:
Note: The user
and 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 geeksforgeeks
directory.
Note: This does not ignore existing directories, or nothing will get done. Even if there are some changes in a file in the local host, it still would not be synced if it's present on the remote host.
Output:
Show progress during transferTo show the progress while transferring the data from local-host to remote-host, we can use -–progress
option.
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.
Output:
In the above image, if we look at the file /foo/file2
, it tells us that
rsync -avhe ssh --progress --update /foo root@remote-host:/tmp/Output: Automatically delete files from local-host after successful transferring
Now, suppose we have a web server and a backup server and we created a daily backup and synced it with our backup server and then we don’t want to keep the local copy of the backup in our web server. So, instead of deleting it manually after successful transfer, we can use the --remove-source-files
flag to automatically delete the files from the web server.
rsync -avhe ssh --remove-source-files /foo user@backup-server:/tmpOutput:
Note: This will delete only the files and not the directories.
Delete the files that have been deleted on the local-hostIf there are some files that are deleted on the local-host and we want that to be updated on the remote host as well, then we need to use the --delete
option.
rsync -avhe ssh /foo --delete user@remote-host:/tmp/Output:
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.
Performing a Dry run with `rsync`A Dry run makes `rsync` perform a trial run that doesn't make any changes and displays almost the same output as a real run would do. It is generally used with the -v, --verbose and/or -i, --itemize-changes options so as to see what a `rsync` command would do before one actually runs it.
rsync -avhe ssh --dry-run --chown=USER:GROUP /foo user@remote-host:/Output: Conclusion
In this article we discussed the rsync command in Linux which is a versatile and powerful tool for synchronizing files and directories between hosts or machines. With its delta-transfer algorithm and various options, it offers efficient data synchronization, backup capabilities, and file transfer management. By mastering the rsync command, Linux users can ensure file consistency, optimize data transmission, and streamline their data management workflows.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4