Tuesday, March 26, 2013

Learn to use rsync For Transferring Files in Linux



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 why do I care?

It can perform differential uploads and downloads of files over the network, transferring only the data that is different. This is great for doing backups between servers, or transferring contents between a development server and a production server. (Just what I used it for.) This is really neat because instead of needing to DL the files locally and then re-upload to the server, you just ask the servers to talk to each other.

 

How do I get it?

If your install of Linux doesn't already have it, use the appropriate package manager. (yum, apt-get, etc)
For example if you are using Debian or Ubuntu Linux, type the following command:

# apt-get install rsync


 

Copy file from a local computer to a remote server

Copy file from /www/backup_file.tar.gz to a remote server called epsilonk.com
$ rsync -v -e ssh /www/backup_file.tar.gz dummyaccount@epsilonk.com:~
Output:
Password:
sent 19009 bytes  received 35 bytes  2093.31 bytes/sec
total size is 19045

Copy file from a remote server to a local computer

Copy file /homedummyacount/some_file.txt from a remote server epsilonk.com to a local computer's /files directory:
$ rsync -v -e ssh dummyaccount@epsilonk.com:~/some_file.txt /files

Synchronize a local directory with a remote directory

$ rsync -r -a -v -e "ssh -l dummyaccount" --delete /local/www epsilonk.com:/www

Synchronize a remote directory with a local directory

$ rsync -r -a -v -e "ssh -l dummyaccount" --delete epsilonk.com:/www/ /local/www

Mirror a directory between my "old" and "new" web server/ftp

You can mirror a directory between an old (old.epsilonk.com) and new web server with the command
$ rsync -zavrR --delete --links --rsh="ssh -l user" my.old.server.com:/home/www /home/www

This assumes that ssh keys are set for password less authentication


The most common rsync command options

  • --delete : delete files that don't exist on sender
  • -v : Verbose 
  • -vv for more details that Verbose
  • -e "ssh options" : specify the ssh as remote shell
  • -a : archive mode
  • -r : recurse directories
  • -z : compress file data

As always though, consult the man.


Thanks to +Brian Downey of thelinuxfix.com for introducing me to rsync. :-) 

    No comments:

    Post a Comment