Meniu

How to synchronize data between two servers using rsync

Rsync: rsync is an application used in unix systems which synchronize files and directories between two machines.rsync behaves like rcp(remote file copy),but it much faster than rcp.because rsync sends the differences in the files instead of sending entire files.

Servers are SERVER_SOURCE & SERVER_DESTINATION

Configure SSH key authenticationIn SERVER_DESTINATION open sshd_config file

vi /etc/ssh/sshd_config

Then verify the following entries

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

save & exit

Then restart ssh daemon

/etc/init.d/sshd restart

Then in SERVER_SOURCE, create a key pair for key authentication between two servers for ssh connection

ssh-keygen -t rsa -b 4096

this will create a public key file and a privite key file with size 4KB.

Then public key will upload to the SERVER_DESTINATION and privite key will keep on SERVER_SOURCE
To put the public key in SERVER_DESTINATION, we will create a directory named .ssh under home directory of the user we want use to connect say rsync_user.Then inside [home directory]/.ssh/ we will create a file called ‘authorized_keys’. if it is already there,open it and append the content of public key file.

save&exit

Set the permission to 600 for file ‘authorized_keys’ and 700 for directory ‘.ssh’
Then test the working by trying to login using ssh from SERVER_SOURCE,if it is connected without prompting for a password.
—THE SSH KEY AUTHENTICATION IS DONE—

Now we need to create a rsync script to synchonize two servers
Here it is rsync.sh

#!/bin/bash
src=’/var/log’
DST=’/var/log’
DESTHOST=’’
DSTUSER=’rsync_user’
LOGFILE=’rsync_log’

echo $’\n\n’ >> $LOGFILE
rsync -av -e ssh $DSTUSER@$DSTHOST:$DST $SRC
2>&1 >> $LOGFILE
echo “Updation Completed at: ‘/bin/date’” >> $LOGFILE

keep this script in home directory and give the execute permission for user, ie

chmod 700 rsync.sh

Then we need to execute this script automatically,for that we should setup a cron job so

crontab -e

This will open file to enter the job details

#Run script rsync.sh everyday @ 123.59

59 123 * * * /[homedirectory]/rsync.sh

John Doe

Articole publicate de la contributori ce nu detin un cont pe gnulinux.ro. Continutul este verificat sumar, iar raspunderea apartine contributorilor.
  • | 340 articole

Nici un comentariu inca. Fii primul!
  • powered by Verysign