Rsync, short for remote sync, is a versatile file copying and synchronizing tool for unix based systems. Compared to ‘cp’ command, rsync is more powerful and flexible. First, rsync only copies the delta files between the source and the destination, it could save copious amounts of time and system resources. Second, rsync is able to compress files as they are being transferred.
In this tutorial I will show you how to use rsync to synchronize files locally and remotely, it’s basic use of rsync, but it will cover almost usages a web admin needs.
Rsync usually comes with system, if it is not installed, please run the following command, take CentOS for example.
yum -y install rsync
Now, it’s time to move on, here is the basic syntax of rsync:
rsync [option] source destination
Rsync has a lot of options, here is a short list of commonly used options. If you want to dig deeply into this tool, take a loot at this page.
-a, archive mode, copies files recursively and preserves users, groups, file permissions, and timestamps.
-h, output numbers in a human-readable format.
-v, increase verbosity, this is especially useful when synchronizing large amounts of data.
-z, compress file data during the transfer.
-delete, delete extraneous files from destination directory. Any files and directories at the destination will be deleted if they aren’t at source.
rsync -av /home/web1/ /home/backup/
This example will synchronize all the contents in /home/web1/ directory into /home/backup/ directory, but the web1 directory will not be created in /home/backup/ directory. If you want to create the web1 directory at the same time, please drop the slash at the end of source path.
rsync -av /home/web1 /home/backup/
Here is a screenshot shows the local synchronization process.
The above example shows how to synchronize files locally, here is another example shows how to synchronize files to remote host. Please replace ‘user’ with your remote host’s user.
rsync -avz /home/web1 [email protected]:/home/backup/
You will be promoted for the remote user’s password. Besides, the remote users must also have write permissions for the target directory.
Here is a screenshot shows the remote synchronization process.