Copy a file on remote server

I have ssh keys setup and running properly between two servers. I have a Korn shell script that is logging into the remote server and needs to backup the authorized_keys and/or authorized_keys2 files.

The following syntax works perfectly
-------------------------------------
ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; cp -p authorized_keys authorized_keys.orig"

Now, what I want to do is actually check for the existence of authorized_keys or authorized_keys2 (because either or both could be on the system). I am attempting this using the following code:

ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; if [[ -f authorized_keys ]]; then cp -p authorized_keys authorized_keys.orig fi"

When I run the command above, I get the following error:

ksh: syntax error at line 1 : `then' unmatched

I have tried the command both with and without the brackets, etc. and nothing seems to work. Can anyone help me with getting this command to run remotely over ssh from ServerA against ServerB ?

Thanks much for any assistance on this :slight_smile:

I think I just figured out the answer to my own question. I came up with the following, if someone else has a better method, please feel free to post, otherwise, hopefully this will help others out.

ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; if [[ -f authorized_keys ]]; then "cp -p authorized_keys authorized_keys.orig" ; fi"

ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; if [[ -f authorized_keys2 ]]; then "cp -p authorized_keys2 authorized_keys2.orig" ; fi"