I'm migrating some home folders from one Linux box to another using scp. I've saved my public key into the destination box's authorized_keys file and the following command works perfectly when I type it at the command line:

scp myArch.tar root@myOtherServer:/root/dumps

... however when I put this into a script like so...

#!/bin/bash
tar -cf /tmp/$1.tar $1; wait
scp /tmp/$1.tar root@myOtherServer:/root/dumps

.. and call it by passing a folder name as a parameter, I get the following error.

[root@myHost home]# /root/migrate myHomeFolder
Permission denied (publickey).
lost connection

Any ideas what could be the problem?

link|improve this question
Can you add an echo $HOME to the script? I wonder if the script doesn't have the environment variable to let it know where .ssh is. – Paul Oct 9 '11 at 11:23
I assume you meant export $HOME - still the same error – Xoundboy Oct 9 '11 at 11:27
I meant "echo" so you could take a look at the value the script thought it was set to. But forcing the issue works just as well. I think it might be time to run verbose logging on the server. – Paul Oct 9 '11 at 11:57
You could also use the -i parameter on the scp command to make absolutely sure it is sending the right private key, and -v to see if there were any issues with it – Paul Oct 9 '11 at 11:59
feedback

1 Answer

try executing the script with sudo ./script.sh may be because you are writing to /root

link|improve this answer
Nope. That doesn't work...any why would it anyway? sudo only applies to the local machine - the problem here is the connection to the remote – Xoundboy Oct 9 '11 at 10:50
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.