A simple script to sync remote hosts, steps to achieve it :
1.Avoid SCP prompting for password [ local host ] => ssh-keygen -t dsa -b 2048 -f ~/key.pub
2.SCP the key for the first time to the [ remote host ] =>
scp ~/key.pub remoteuser@remotehost:/home/remoteuser/
- In remote host : [ Authorize the Host from which key was received ]
if [ ! -d .ssh ]; then mkdir .ssh chmod 700 .ssh fi mv key.pub .ssh/ cd .ssh/ if [ ! -f authorized_keys ]; then touch authorized_keys chmod 600 authorized_keys ; fi cat key.pub >> authorized_keys
“rsync + Cron jobs” to keep both the hosts in sync :
################################################################## #!/bin/sh
RSYNC=/usr/bin/rsync
SSH=/usr/bin/ssh
KEY=/home/whoami/key.pub
LOCAL_PATH=/sync/dir
REMOTE_USER=remote-user
REMOTE_HOST=remote-host
REMOTE_PATH=/remote/dir
$RSYNC -az -e “$SSH -i $KEY”
$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH $LOCAL_PATH
##################################################################
Setting up cron
mv syncScp.sh /etc/cron.daily/
or
crontab -e
0 5 * * 5 /home/you/syncScp.sh
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.