Synchronize remote hosts

18 Jan 2010

by hemanth

  1. A simple script to sync remote hosts, steps to achieve it :
  2.  
  3. 1.Avoid SCP prompting for password [ local host ] =>
  4. ssh-keygen -t dsa -b 2048 -f ~/key.pub
  5.  
  6. 2.SCP the key for the first time to the [ remote host ] =>
  7.  
  8. scp ~/key.pub remoteuser@remotehost:/home/remoteuser/
  9.  
  10. 3. In remote host :
  11. [ Authorize the Host from which key was received ]
  12.  
  13. if [ ! -d .ssh ]; then
  14. mkdir .ssh
  15. chmod 700 .ssh
  16. fi
  17. mv key.pub .ssh/
  18. cd .ssh/
  19. if [ ! -f authorized_keys ]; then
  20. touch authorized_keys
  21. chmod 600 authorized_keys ;
  22. fi
  23. cat key.pub >> authorized_keys
  24.  
  25. "rsync + Cron jobs" to keep both the hosts in sync :
  26.  
  27. ##################################################################
  28. #!/bin/sh
  29.  
  30. RSYNC=/usr/bin/rsync
  31. SSH=/usr/bin/ssh
  32. KEY=/home/`whoami`/key.pub
  33. LOCAL_PATH=/sync/dir
  34. REMOTE_USER=remote-user
  35. REMOTE_HOST=remote-host
  36. REMOTE_PATH=/remote/dir
  37.  
  38. $RSYNC -az -e "$SSH -i $KEY" \
  39. $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH $LOCAL_PATH
  40.  
  41. ##################################################################
  42.  
  43. # Setting up cron
  44. mv syncScp.sh /etc/cron.daily/
  45.  
  46. or
  47. # crontab -e
  48. 0 5 * * 5 /home/you/syncScp.sh
Share this