Shell script to copy from data from one server to another without any password

Hi Everyone,

I want to make a script file to copy or sync data from one server to another without asking any password also before copying file i want to know the numbers of the files to be copied & if i say yes then only it will copy on remote server.

Can anyone help

1 Like

connect via scp

spawn scp "user@example.com:/home/santhosh/file.dmp" /u01/
dumps/file.dmp

# # # # # # # # # # # # # # # # # # # # #

except {
-re “. *es. *o. *” {
exp_send "yes\r "
exp_continue
}
-re “.*sword. *” {
exp_send “PASSWORD\r”
}
}
interact

https://blogs.oracle.com/SanthoshK/entry/automate_linux_scp_command

@ACID01001001

I request you to explain more clearly with solution, so that the user will understand properly and also make sure to preview the post before replying to user to avoid any errors. All I want is to keep this forum clean, formatted with proper information…

1 Like

I would prefer using ssh keys and rsync.

Ubuntu example:

#first generate a ssh key on source machine1
    mkdir ~/.ssh
    chmod 700 ~/.ssh
    ssh-keygen -t rsa
#Just leave blank when asked for a passfrase and hit enter

#copy your public key to destination machine2
ssh-copy-id -i ~/.ssh/id_rsa.pub username@machine2
#replace machine2 with ip address of machine2 if necessary and username with your own

#sync /home/username/folder to /home/username/folder on machine2
rsync -avzP /home/username/folder -e ssh username@machine2/home/username/
1 Like

dear henrik

i also want to know the number of the files to be copied before i sync and if i say yes then only it will start copying
how to make the script for that

Once you have the SSHs ready, you can use the following command to count files:

find . -type f | wc -l

After that use select for your yes/no question. The script can look like this:

#!/bin/bash
find . -type f | wc -l

echo -n "Should I continue with syncing files(y/n)? "
read answer
if echo "$answer" | grep -iq "^y" ;then
  #  YOUR SYNC COMMAND HERE
  rsync -avzP /home/username/folder -e ssh username@machine2/home/username/
else
    echo No
fi

Of course you may need to change the sync command but this should be enough. I hope that helps.

2 Likes

Thanks Marin T
I have done the same and it is working
thanks again