Pages

Wednesday, December 5, 2007

shell script to mirror data on network using rsync , ssh-key

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ssh keys
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

First of all we need to generate ssh key on localhost and copy the key to remote machine. so in future communication happen without supply password to remote machine.

here steps follows to genereate ssh keys :

localhost # ssh-keygen -t dsa

localhost # file ~/.ssh/id_dsa

localhost # file ~/.ssh/id_dsa.pub

localhost # ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.50

locahost # ssh root@192.168.0.50


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
how to rsync ( man rsync )
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

I love rsync to mirror data , just cos of its wast features that it provides.here goes some sample of how to use rsync.note that more than this can be done with rsync.

localhost# rsync -avz /home/file root@192.168.0.50:/home/.

above rsync program copies /home/file to targeted machine .

-a, --archive
-v, --verbose
-z, --compress

localhost# rsync -avz -e ssh /home/file root@192.168.0.50:/home/.

-e, --rsh=COMMAND specify the remote shell to use

localhost# rsync -avz --delete /home/dir/ root@192.168.0.50:/home/dir/.

Note : be careful to use --delete option .this will exactly sync source and target dir and keep exactly copy of source to target deleting rest of modufyied files into that dir.

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
shell script to run in cron for regular updates
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#!/bin/bash

echo " ">>nohup.out
echo " ">>nohup.out

echo "*******Start of `date` ***********" >>nohup.out
echo "Start of /var/spool/mail directory " `date` >>nohup.out
nohup rsync -az --delete /var/spool/mail/ root@192.168.0.50:/var/mail
echo "End of /var/spool/mail directory " `date` >>nohup.out
echo "===================================" >>nohup.out

echo " ">>nohup.out
echo "Start of /home directory " `date` >>nohup.out
nohup rsync -az --delete /home/ root@192.168.0.50:/home
echo "End of /home directory " `date` >>nohup.out
echo "===================================" >>nohup.out

echo " ">>nohup.out
nohup rsync -avz /etc/group root@192.168.0.50:/etc/mailhost
nohup rsync -avz /etc/passwd root@192.168.0.50:/etc/mailhost
nohup rsync -avz /etc/shadow root@192.168.0.50:/etc/mailhost

echo "*********End of `date` *************" >>nohup.out

echo " ">>nohup.out
echo " ">>nohup.out

1 comment:

Anilkumar Naik said...

SSH protocol version 2
algorithm to be used: The client uses his private key, $HOME/.ssh/id_dsa or $HOME/.ssh/id_rsa, to sign the session
identifier and sends the result to the server. The server checks whether the matching public key is listed in
$HOME/.ssh/authorized_keys and grants access if both the key is found and the signature is correct. The session
identifier is derived from a shared Diffie-Hellman value and is only known to the client and the server.

If public key authentication fails or is not available, a password can be sent encrypted to the remote host to prove
the user’s identity.

-bash-3.00$ cp id_dsa.pub authorized_keys