Rsync Crash Course

Sunday, April 29, 2012

Today a friend asked me to give him a crash course in rsync, a great program that I use a lot to copy files from my servers to home, and visa-versa. Have you ever tried copying a file over a network only to have your terminal hang for a while until you realise that your wireless has disconnected, your cat has pulled out the phone line, or the little man inside your computer decided to have a field day and took a toilet break? Let rsync save you from that knowing that pain again.

Background:
rsync is another alternative to cp/scp. Except it has a lot more features. Such as the ability to resume a transfer, copy computer to computer, and has the ability to transfer over ssh with a beautiful progressbar.



General Usage:
 rsync <source> <destination>  




My main default command that I like to use is:
 rsync -aPhz --no-whole-file --inplace <source> <destination> 
  Options:
  -a is Archive (also gives options -rlptgoD)  
     -r recursive
     -l copy the symlinks as a symlink  
     -p preserve permissions  
     -g preserve group  
     -o preserve owner  
     -D preseve special/devices  
  -P show a nice Progress bar  (best feature ever) and also resumes transfers!
  -h make it Human readable  
  -z compress(z) the transfer for us slow internet people.  
 --no-whole-file (sends the whole file as-is, not chunk by chunk)
 --inplace (Does not make a temp file first, copies straight to the directory)

Some other options are if your host doesnt run ssh on the standard port 22, you may struggle to get it working, '-e' is your solution
-e 'ssh -p 2234'


Usage for different <source> and <destination>:

These are some of the type of different source and destinations you can use. Make sure that when you are copying directories that you keep the end '/' which tells rsync that it needs to copy the files in that directory, not just an empty directory.

Remote to Local:
rsync bob@myserver.com:/home/bob/ ~/ (This copyies bob's HomeDir to your current HomeDir)

Local to Remote:
rsync -aPhz -e 'ssh -p 33033' /var/www/ serveruser@myserver:/var/www/ (Copies your webserver to your server via port 33033 which is running ssh)


Examples:
Coyping a Single Directory with no recursive:
rsync -aPhz --no-whole-file --no-r --inplace <source> <destination>

Copy a Single Directory with recursion:
rsync -aPhz --no-whole-file --inplace <source> <destination>

Conclusion:
Hopefully you have learnt something today and will use the power of rsync in your next copy procedure!

2 comments:

Kiike said...

It's always nice to have useful posts like this,s o thanks for sharing your knowledge!
As for the ssh port tip, it's very useful to have a ~/.ssh/config file, where you can define aliases and configurations for every host.
For example you can have a section like this:
Host myserver
HostName server.example.com
Port 1234
Compression yes

That's very useful and just works(tm) with rsync, scp, ssh, etc.

Zystem said...

Really good write up ... on rsync.
I use it all the time.

and kike's right, scp rocks, and I like it better than sftp.

Post a Comment