RSYNC script - sync the data between two servers


Author : Dhanabal S

Sync the data between two linux servers

Solution:

Assumption:

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

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/id_rsa.pub      (public key file)
#/root/.ssh/id_rsa               (private key file)

4)      Now upload the public key to the Destination Server
# scp /root/.ssh/id_rsa.pub 192.168.0.152:/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 ( id_rsa.pub) to “authorized_keys”
# cd /root/.ssh
# mv id_rsa.pub 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@192.168.0.152

            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/rsync.sh
#!/bin/bash
SOURCEPATH=’/home’
DESTPATH=’/home’
DESTHOST=’192.168.0.152′
DESTUSER=’root’
LOGFILE=’rsync.log’
echo $’\n\n’ >> $LOGFILE
rsync -av –rsh=ssh $SOURCEPATH \  $DESTUSER@$DESTHOST:$DESTPATH 2>&1 >> $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/rsync.sh

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/rsync.sh

            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

5 Comments

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

    $DESTUSER@$DESTHOST:$DESTPATH 2>&1 >> $LOGFILE

    Thanks!

    ReplyDelete
  2. http://en.wikipedia.org/wiki/Standard_streams
    stranded input =0
    stranded output =1
    stranded error =2
    go to that link and you can get more information about that.

    ReplyDelete
  3. http://stackoverflow.com/a/16283739/1862107 this will help you more.

    ReplyDelete
  4. how to send a mail Success log files

    ReplyDelete