RSYNC script - sync the data between two servers

Author : Dhanabal S

Sync the data between two linux servers



a)      Source server ip address:
b)      Destination server ip address:

Source server - The server we are connecting from to upload the data
Destination server - The server we are connecting to receive the data

Setting the SSH key authentication:

1)      Make sure the Destination server have the ability to use key authentication enabled. In the sshd configuration file (usually ‘/etc/ssh/sshd_config’) enable the following options if they are not already set.
# vi /etc/ssh/sshd.conf
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

2)      In Source server create the public / private key pair to used for authentication with the following command
# ssh-keygen -t rsa

Note: Do not enter a passphrase for this, just hit enter when prompted.
3)      Now two files (public and private key) are created in the home directory of the user. If you are execute this as a root user means the files will be in,
# /root/.ssh/      (public key file)
#/root/.ssh/id_rsa               (private key file)

4)      Now upload the public key to the Destination Server
# scp /root/.ssh/

Note: Be sure to keep this private key safe. With it anyone will be able to connect to the Destination Server that contains the public key.

5)      In the Destination Server rename the public key file ( to “authorized_keys”
# cd /root/.ssh
# mv authorized_keys

6)      Change the file permission of that public key as well as ssh folder permission also
# chmod 600 /root/.ssh/authorized_keys
# chmod 700 /root/.ssh

7)      Test the keys are working or not , by connecting the Destination Server from the Source Server
# ssh root@

            If all is working it should not be prompted for a password but instead connected directly to a shell on the Destination Server.

Creating rsync script:

8)      Create one simple rsync script to sync both the servers and place it into the user’s home directory
# vi /root/
echo $’\n\n’ >> $LOGFILE
echo “Completed at: `/bin/date`” >> $LOGFILE
Note: In this script 4 variables are used
SOURCEPATH - Source path to be synced
DESTPATH - Destination path to be synced
DESTHOST - Destination IP address or host name
DESTUSER - User on the destination server
The script will send all output to the ‘rsync.log’ file specified in the script

9)      Give the executable permission for this script
# chmod 700 /root/

10)    Now run the script and check, it is connect to the Destination Server, and transfer the files all without your interaction.

Setting up the cron job:

11)   Setup a cron job to run the script automatically at a predefined interval.
# crontab –e
0 * * * * /root/

            This will run the script once in every hour. Your 2 servers should now be syncing the chosen directory once every hour.

Author : Dhanabal S  


Post a Comment


  1. In the script above, what does the 2>&1 represent?



    stranded input =0
    stranded output =1
    stranded error =2
    go to that link and you can get more information about that.

  3. this will help you more.

  4. how to send a mail Success log files