Transfer files securely through servers is a regular and important task for the system administrators. We can use SCP and rsync as primary command-line tools for achieving this kind of job. rsync (remote sync) is a very flexible and adaptable command-line tool.
Most Linux applications use the rsync tool for mirroring data by synchronizing files and folders between two locations over a remote shell, or from/to a remote rsync daemon. rsync copy files incrementally, which means rsync can copy only the differences between source and destination that gives you much efficiency.
Let’s see how we can use the rsync command to copy files between the Linux servers.
You can install the rsync command-line tool simply as follows.
$ sudo apt-get install rsync -y
$ sudo yum install rsync -y
Commonly used rsync options. See all available options here.
-v, --verbose increase verbosity -q, --quiet suppress non-error messages -a, --archive This equivalent to -rlptgoD. -r, --recursive copy files recursively including subdirectories and its content. -b, --backup make backups -e, --rsh=COMMAND specify the remote shell to use -z --compress compress file data during the transfer
NOTE: rsync should available on both source and destination servers.
How to copy files between two local directories using rsync
$ rsync -arv /home/geekdosage/sample-dir /opt/temp-directory/
Let’s try to understand the above command. we have used the rsync command with a couple of options. It will copy
/home/geekdosage/sample-dir directory with the content inside
sample-dir directory to
/opt/temp-directory within the same computer/server.
How to copy files from local directory to remote server using rsync
$ rsync -arvz /home/geekdosage/sample-dir [email protected]:/path/to/directory/
Here we have used
-z option to reduce the time take for copying since it compresses the data during the transfer.
This command copy /home/geekdosage/sample-dir locally available directory to a remote server’s given directory. we can use an IP address of the remote server or a hostname.
How to copy files from the remote server to local directory using rsync
$ rsync -arvz [email protected]:/remote/path /local/directory/path
How to copy files using rsync when remote server ssh listening port different instead of port 22
For security concerns, some servers use a different port other than 22. Therefore we can use the ‘
-e’ option to set the listening ssh port as follows.
$ rsync -arvz -e "ssh -p 522" /home/centos/sample-dir [email protected]:/path/to/directory/
And also you can use ‘
-i’ option to set identity file to authenticate with remote servers as follows.
$ rsync -arvz -e "ssh -p 522 -i /home/centos/identity.pem" /home/centos/sample-dir [email protected]:/path/to/directory/
NOTE: For fast data transmission always try to compress large files or set of files into one file using
zip command-line tools
How to exclude files and directories when using rsync to copy files
we can exclude unnecessary files and directories by using ‘–exclude‘ argument.
$ rsync -arvz --exclude=logs /var/sample-dir [email protected]:/path/to/directory/
It is possible to exclude a set of files and directories by providing that list included in a file. We can use the ‘–exclude-from‘ argument.
$ cat excluded-files.txt
OUTPUT logs tmp .git
$ rsync -arvz --exclude-from=excluded-files.txt /var/sample-dir [email protected]:/path/to/directory/
Rsync is a convenient and useful tool for administrative tasks. In this article, we have explained the most common usage of rsync with examples. Type man rsync to refer to the manual page of the rsync command for more in-detail information. Please comment on your doubts or suggestions.