Using rsync on ssh


What is rsync:
rsync is a piece of open source software
that provides fast incremental file transfer.
If rsync is used on top of the ssh protocol
then a really secure way is made to transfer
files between directories on two different servers.

An example procedure:
How to get files secure from a remote machine(named REMOTE01)
to a local machine(named:LOCAL01)with rsync.
For safe file transfer a user named syuser is made on the LOCAL01.
And a user named remuser is made on the REMOTE01.
First check if ssh and rsync is installed
and is part of the search path of the syuser and remuser.
To check this give the following commands:

Which rsync
/usr/local/bin/rsync

Which ssh
/usr/bin/ssh

If these programs are found everything should work.
If not install the packages or check the user profile
and look if the right search path is available.
Make sure that the ip addresses and the names of the machines
is in the /etc/hosts file on both machines.
Next prepare the both (the LOCAL01 and the REMOTE01) machines to work
with a special rsync ssh key. (Check links below)

Preparation of LOCAL01
Preparation of REMOTE01

After the preparing of the LOCAL01 and the REMOTE01
The connection can be tested with the rsync command on ssh:

rsync -avz -e "ssh -i /home/syuser/keys/LOCAL01-rsync-key" remuser@REMOTE01:
/home/remuser/upload/ /home/syuser/download

receiving file list ... done
test.txt
wrote 36 bytes read 260 bytes 197.33 bytes/sec
total size is 583 speedup is 1.97


Now the file test.txt put in the upload directory
from the remuser on REMOTE01, is copied
in the download directory of the syuser on the LOCAL01.
Read the man pages of rsync to get more information
on the parameters that are used.
When things are not working! Then the following link provide
some trouble shooting tips:

Trouble shooting page

If everything works well.
An upload can be done everyday, every week or every month etc,
by making use of the crontab.
For this purpose a script is made for the rsync command
so that it can be executed by the crontab.
Cause a short script name in the crontab looks better then
a long command with lots of parameters.
Below the script in this example named: download01.sh

#!/bin/sh
RSYNC=/usr/local/bin/rsync
SSH=/usr/bin/ssh
KEY=/home/syuser/keys/LOCAL01-rsync-key
RUSER=remuser
RHOST=REMOTE01
RPATH=/home/remuser/upload/
LPATH=/home/syuser/download/
$RSYNC -az -e "$SSH -i $KEY" $RUSER@$RHOST:$RPATH $LPATH


When the script is made, place it in the crontab like this:

0 5 * * * /home/syuser/scripts/download01.sh

Every morning on 5 o'clock the download directory receives
(very secure) added or changed files from the upload directory in the REMOTE01.
Files that are still the same in both directories will not be resend.
This last feature of rsync is very good for reducing network traffic load.

Back