HowTo Use rsync For Transferring Files Under Linux or UNIX

How do you install and use rsync to synchronize files and directories from one location (or one server) to another location? – A common question asked by new sys admin.

rsync is a free software computer program for Unix and Linux like systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction.

So what is unique about the rsync command?

It can perform differential uploads and downloads (synchronization) of files across the network, transferring only data that has changed. The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection.

How do I install rsync?

Use any one of the following commands to install rsync. If you are using Debian or Ubuntu Linux, type the following command:
# apt-get install rsync
OR
$ sudo apt-get install rsync
If you are using Red Hat Enterprise Linux (RHEL) / CentOS 4.x or older version, type the following command:
# up2date rsync
RHEL / CentOS 5.x or newer (or Fedora Linux) user type the following command:
# yum install rsync

Always use rsync over ssh

Since rsync does not provide any security while transferring data it is recommended that you use rsync over ssh session. This allows a secure remote connection. Now let us see some examples of rsync command.

Comman rsync command options

  • --delete : delete files that don’t exist on sender (system)
  • -v : Verbose (try -vv for more detailed information)
  • -e "ssh options" : specify the ssh as remote shell
  • -a : archive mode
  • -r : recurse into directories
  • -z : compress file data

Task : Copy file from a local computer to a remote server

Copy file from /www/backup.tar.gz to a remote server called openbsd.nixcraft.in
$ rsync -v -e ssh /www/backup.tar.gz jerry@openbsd.nixcraft.in:~
Output:

Password:
sent 19099 bytes  received 36 bytes  1093.43 bytes/sec
total size is 19014  speedup is 0.99

Please note that symbol ~ indicate the users home directory (/home/jerry).

Task : Copy file from a remote server to a local computer

Copy file /home/jerry/webroot.txt from a remote server openbsd.nixcraft.in to a local computer’s /tmp directory:
$ rsync -v -e ssh jerry@openbsd.nixcraft.in:~/webroot.txt /tmp

Task: Synchronize a local directory with a remote directory

$ rsync -r -a -v -e "ssh -l jerry" --delete /local/webroot openbsd.nixcraft.in:/webroot

Task: Synchronize a remote directory with a local directory

$ rsync -r -a -v -e "ssh -l jerry" --delete openbsd.nixcraft.in:/webroot/ /local/webroot

Task: Synchronize a local directory with a remote rsync server or vise-versa

$ rsync -r -a -v --delete rsync://rsync.nixcraft.in/cvs /home/cvs
OR
$ rsync -r -a -v --delete /home/cvs rsync://rsync.nixcraft.in/cvs

Task: Mirror a directory between my “old” and “new” web server/ftp

You can mirror a directory between my “old” (my.old.server.com) and “new” web server with the command (assuming that ssh keys are set for password less authentication)
$ rsync -zavrR --delete --links --rsh="ssh -l vivek" my.old.server.com:/home/lighttpd /home/lighttpd

Read related previous articles

Other options – rdiff and rdiff-backup

The rdiff command uses the rsync algorithm. A utility called rdiff-backup has been created which is capable of maintaining a backup mirror of a file or directory over the network, on another server. rdiff-backup stores incremental rdiff deltas with the backup, with which it is possible to recreate any backup point. Next time I will write about these utilities.

rsync for Windows Server/XP/7/8

Please note if you are using MS-Windows, try any one of the program:

  1. DeltaCopy
  2. NasBackup
Further readings

=> Read rsync man page
=> Official rsync documentation

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
63 comments… add one
  • Mustafa Pasha Mar 25, 2017 @ 13:38

    Hi,
    In my scenario i want to take back up through rsync only changed files in remote system.
    lets say in my 1st system my folder is pasha and in my 2nd system folder is kalyan , when i synchronise both the folders i get complete backup ,

    Then i added some files in pasha and again synchronise with the remote system kalyan.

    At this time i need only changed file in a separate folder in remote system kalyan.

    i mean day wise incremental backup.
    Thanks.

  • perez murphy Nov 29, 2016 @ 9:00

    You can remove spaces, replace spaces with underscore, Uppercase/lowercase filename, add a prefix/suffix, remove/replace strings and also catalog files by adding an incremental number to the file name.

  • Mojtaba May 22, 2014 @ 14:33

    Hi,

    I think you must change these with each other:

    Task: Synchronize a local directory with a remote directory

    $ rsync -r -a -v -e “ssh -l jerry” –delete /local/webroot openbsd.nixcraft.in:/webroot

    Task: Synchronize a remote directory with a local directory

    $ rsync -r -a -v -e “ssh -l jerry” –delete openbsd.nixcraft.in:/webroot/ /local/webroot

    the format is rsync source dest.

  • Ory CHHEAN May 7, 2014 @ 8:18

    Dear Everyone,

    I need help.

    I would like to do a schedule backup using cron job in Ubuntu.

    If I use “rsync -ave ssh root@remote_ip:/var/mail/ /tmp/backup_location/”, it will need to enter remote host password.

    So how can I use above command accompanied by remote host password.

    Thank you for your help.

    • Tanja II Aug 1, 2017 @ 12:45

      Hi! Did you find how to do it? Have to do exactly the same

  • joe Jan 8, 2014 @ 4:17

    I’m not sure if I’m simply misreading the “Task” titles, but it seems you’ve got the “Synchronize a local directory with a remote directory” and “Synchronize a remote directory with a local directory” mixed up.

    To say “Synchronize X with Y” implies X is the destination and Y is the source; X is the thing being acted upon, Y is what is being used to act. However, this is definitely NOT what is shown in the example command. Rsync’s first directory argument is the source, the second argument is the destination.

  • vinoth Sep 24, 2013 @ 6:49

    i have rsync errror in my downloading system.the network ip address is to be changed.
    so how to configure new static ip address in rsync

  • Daniel Sep 18, 2013 @ 15:58

    Thanks so much for this intro. I’ve been meaning to start using rsync for ages, this got me straight in.

  • sachin Aug 1, 2013 @ 1:49

    I am trying to use r sync to sync up files between 4 servers . The issue is all the servers would be master servers and files can come on any of the server at anytime . Can i use rsync in this case ? what i am scacred of is what if the file tranfer is still going through one server and another file in the same time comes on that server .

  • pete Jul 26, 2013 @ 2:56

    thanks. that was helpful.

  • Rahul Janghel Apr 25, 2013 @ 15:15

    Hi Vivek, Friends,

    Is there any way to exclude any directory from sync at either end ?
    How it is better than unison ?

    Regards,
    Rahul Janghel.

  • Siddharth Rana Dec 9, 2012 @ 9:43

    which protocol version rsync support ?

  • rum Nov 8, 2012 @ 3:33

    Bump

    keep this alive.

  • BrianBlaze Oct 24, 2012 @ 16:04

    Thank you thank you thank you 😀

  • Rashid Iqbal Sep 30, 2012 @ 14:35

    I am copying a 2gb single file over 2MB link,
    If copy is fail and I want to start the copy again from where it left then what is the option I need.

  • vik Sep 26, 2012 @ 8:32

    Hi,
    Can anyone assure me that the rsync –delete & –delete-after parameter deletes files
    only on the DESTINATION (target) & that NO file deletion occurs on the SOURCE?

    Thanks,
    vik

  • Sapna Joshi Aug 17, 2012 @ 10:45

    Hi Vivek,

    I am totally fresher in Unix, and I want to cut the log files from server1 and paste them to server2. I am very much confused on the command part.

    Below are my understanding , Please suggest.
    1.) Create public key through ” ssh-keygen -t rsa”
    2.) Copy the public key to Server2 –> If yes, then where exactly I have to copy the key?
    3.) write command in Server1 “scp -C $file Path$ $public_key_username@Server2.com:/path$
    –> I want to cut the file from server1, what is the exact command for that?
    –> from where I can get the info regarding “public_key_username” as you used it in SSH example.
    4.) You must have SSH running on Server2 to make this work.
    5.) write cron job to make it automatic.

    Require guidance on same.
    Thanks in advance.

    Regards,
    Sapna Joshi

  • Rowan Reid Aug 8, 2012 @ 7:53

    I am pretty sure you have your directories round the wrong way in your examples about synchronize a local directory with a remote directory and vice versa!

  • nyiko Jul 11, 2012 @ 12:08

    hi
    i have downloaded a file in one account and want to copy the file to the other account in the same machine,i have tried to copy using scp and cp command but without success,it keeps prompting for password.please help

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.