Link to home
Start Free TrialLog in
Avatar of mafairuz
mafairuzFlag for Oman

asked on

Transferring files to another host account using SSH

I've a shell script which daily backups my database.
I need to add a line or few after the backup process is over, so the script can also connect to another host account and transfer the new backup file to a specific folder in that account.

a secure connection is prefered, but when I try to use sftp manually, I couldn't connect to my other server as it says the port is not allowed in the remote server.  Therefore, I tried using ftp from my ssh and it worked fine, and I was able to transfer the file using "put" command.

Any advice + command lines I should add in the shell script
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

Hi,

If you want to use secure copy, then you need to have the port 22 open to the remote server.

Here a link on how to use scp to copy files between servers:

http://www.linuxjournal.com/article/8600
ASKER CERTIFIED SOLUTION
Avatar of Jan Bacher
Jan Bacher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you still get a 'not allowed', we will need to modify the firewall (iptables?) to allow incoming port 22 traffic from your originating server.
use scp with a user where you have installed a public key on the remote server
Avatar of mafairuz

ASKER

appreciate your replies .. maybe it is my fault not to mention that the remote server is not a dedicated server .. it is a shared host account which I have no control over its root or its configurations :s
can you login with ssh?
yes .. I can login with ssh from my dedicated server (local server) using ftp
> I can login with ssh ... using ftp
what now?
I asked for ssh, not ftp
no direct ssh is not possible unfortunatly
So you only have ftp? then there is no way of secure transfer as you asked in your question. And more worse: ftp is known to be one of the most insecure tranfers.

I'd use a https connection instead.
I've just requested to enable ssh ..
the host provider said it is possible, I just need to send them some documents.

So consider SSH enabled .. but restricted to my account which does not have a root access .. where to go from here
SSH is now working on the remote server .. with port number 22 .. and only accessing my account folder .. not the root.

any help from here would be appreciate it
mafairuz, with ssh enabled, you can now ssh to that machine and create your keys.  

when done, download the public key as mentioned above.

you should not need root access at this point.
I couldn't find the /etc/ssh folder jesper .. I thought maybe this is only available at root dir
No problem, that's the ssh daemon configuration directory.  You don't need it anymore.

When you ssh to the server do this (using 'support' as example):

support]$ ssh-keygen -t rsa

Hit enter and you will be prompted for a pass phrase; enter twice.

This creates two files, ~/.ssh/id_rsa and ~/.ssh/id_rsa.pub

Logout.

Login via sftp from the user account on the original machine to the remote server.

sftp> cd .ssh
sftp> get id_rsa.pub

Put the 'pub' file in /home/support/.ssh/ and rename it to "authorized_keys2" (no quotes).
  -> /home/support/.ssh/authorized_keys2

If you do an sftp test manually, you should not be prompted for a password.

Security -> keep that public key private.  If that got out of your hands, anyone could access your account on the remote server.  This is the downside of passwordless connections.  If you feel that your key has been compromised, generate a new key and download that.
ok the host you send the files, issue following command

  ssh-keygen

follow the instructions. It generates a public-private key pair.
Copy the private key (named *.pub somehow) to the remote server where you want to copy the files to. There you write the content of the .pub file into

  ~/.ssh/authorized_keys

Now you should be able to login using your private key, somehow like:

  (local host)# ssh -i /full/path/to/keyfile user@remote-server

if that works, you can copy files the same way

  (local host)# scp -i /full/path/to/keyfile local-file user@remote-server:remotef-file

Please read man-pages about scp, ssh and ssh-keygen 'cause you need to set the permissions to the ~/.ssh/authorized_keys and the key files proper. ssh is very pedantic, for obvious reason :)
dooh, I was typing to slow ...
jasper

when I tried to sftp from original to remote, I got the following error:
ssh: connect to host omanis.org port xxxxx: Connection refused

where xxxxx is the private port I use to connect to original server .. while remote accepts 22
ahoffmann
does your way overcome the ports problem I have !
ssh is port 22, you don't need anything there, scp uses that to, it's completely transparent (much better than ftp)
I'd recoomed to get rid of ftp, and sftp too (which is just a wrapper around scp anyway).
mafairuz,

Can you confirm that you can ssh?  Because if you can, you should be able to sftp or scp.

If you can do one but not the others, I'd strongly recommend calling the provider for assistance.  You really do not want to perform an unencrypted connection.
Dears jesper and ahoffmann
SSH is working for both servers, however,
local only allows SSH connection through a private port
remote allows SSH only through port 22

sftp and scp both has shown same error message, that they can not connect to remote host on the private port the server is using.  However, I found the option -P which can used with scp to specify the port of remote machine, I used it as follows:

scp -P 22 /path/to/backup.bkp remoteuser@remote.com:/home/user/

I got the following message:
==============
The authenticity of host 'remote.com (xx.xx.xx.xx)' can't be established.
RSA key fingerprint is xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'remote.com,xx.xx.xx.xx(RSA) to the list of known hosts.
==============
Then I was asked for a password of the remote user, and when I submit, I got the following error:
==============
stdin: is not a tty
backup.bkp                             100%  233     0.2KB/s   00:00
==============

I checked the remote server, the file was successfully copied.  However, is this a secure way to copy? plus how to make this run from a script as whenever I try to use it again, it asks for a password.

It's rather puzzling that you have to specify port 22 -- that is the default.

The very first time that you connect via ssh from one specific account to another specific account on a specific machine, you will be prompted to add that host.

Once you have done that, you should expect to never see that again.  If you do *do not automatically say 'yes' to continue* as you may be a victim of a man in the middle attack.  

If you always connect by hostname and then you connect by IP, those are treated as two different hosts -- so be consistent.

As to the password, once you follow the directions and copy the public key to your local machine's authorized keys, you should not be prompted for the password.  If you do, contact your provider as it may be that they are requiring it.  If that's the case, we'll get rid of the public key and create a perl script using Expect to perform the login.
> sftp and scp both has shown same error message, ..
please get rid of sftp unless ssh works and the scp works

> stdin: is not a tty
this sound like a shell resource script (.profile .login or whatever) asking for some input. I.g. this doesn't matter as long as you use scp interactively.

>  is this a secure way to copy?
yes

> plus how to make this run from a script as whenever I try to use it again, it asks for a password.
see the suggestions about generating a key pair.

> local only allows SSH connection through a private port
I guess there is some misunderstaning somewhere here
As you showed above with the -P option, you can connect on the standard port 22 to the remote host, hence the -P 22 options seems to be useless.
Probably you mean (are someone told you) that connection *to* your local server are allowed through private port only. In that case you need to clarify what exactly was meant by "private port". Usually people mean ports <1024 .
I will try again with the public-private keys and come back to you with results

==============
> local only allows SSH connection through a private port
I guess there is some misunderstaning somewhere here
As you showed above with the -P option, you can connect on the standard port 22 to the remote host, hence the -P 22 options seems to be useless.
Probably you mean (are someone told you) that connection *to* your local server are allowed through private port only. In that case you need to clarify what exactly was meant by "private port". Usually people mean ports <1024 .
================
if I don't use -P 22, I get the following error:
ssh: connect to host remote.com port xxxxx: Connection refused

What I meant by private port is it is using a special port other than 22 .. which is 5 digits port  xxxxx

to make it clear, when I ssh my local server, I can not connect unless I set the port on my SSH client to xxxxx, but when I conntect to remote host using SSH client, I leave the default 22 untouch to get connected.
and also will check with remote host support if they requiers password all time as jasper recommended
It *sounds* as if your local server (listening on a non-standard port) is presuming a non-standard port connection when creating an outbound ssh from that machine and which requires you to specify the standard "-P 22" in your outbound connections.

If that's the case and it works, you will need that option in your scp transfer, too.
see man ssh for -P (upper case p), which is different to scp's -P (upper case p), unfortunately :(
My version of ssh (OpenSSH) uses "-P" to specify port for both ssh and scp.
okey finally worked without passwords
I was producing the keys in the remote server and then copying the *.pub key to the local server .. as I understand from the comments.  So I did it again the other way around, and now it is working without requesting a password.

Okey last bit
I shall just add the following command to my script I guess:
scp -P 22 /path/to/backup_file remoteuser@remotehost.com:/path/to/backup_dir

is that correct? and secure ?  how about if the remote server is down at the moment of initiating this command, there should be a way to report this by e-mail.  if I may have your help on this as well please.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I usually test for reachability since I have yet to have an ssh server die.  I use fping (easy to install) and transfer the file if the connection is alive.

RESULT=`/usr/local/bin/fping 192.168.1.1`

if [ "$RESULT" == "192.168.1.1 is alive" ]; then
 # scp command here
else
 mail -s "File Transfer Failed - Host Not Reachable" user@domain.com
fi

Scp is an encrypted transfer -- so yes, it is as secure as Ssh.
I have applied the changes, and just waiting for midnight (mytime) to reach to see how things will work ..
just a wonder .. I notice that my server memory becomes low everyday till the server has no available memory at all where it goes down and then it is restarted with fresh memory .. I thought maybe I can do something better than "service mysql restart" after the backup process is over every night, so I get the memory refreshed as well without having to wait till we go through a server down ..

what exactly to restart in this case to get the memory refreshed again .. !
What process(es) are consuming memory ( ps -aux | more ) and do any of the offending applications have memory leaks associated with them?
It is a bit strange .. as all of the processes are just 0.0 or 0.1% using the memory, however, I am just left with almost 100Mb out of 2GB of RAM
the only one that has almost 13% is /usr/sbin/mysqld
There are also the following processes use 1.3%, 1.2%, 0.4%
cpanellogd - setting up logs for user
cpanellogd - http logs for user
/usr/sbin/named -u named

When server goes down, the few first hours I use top command, all these processes are running, but the memory is almost 300MB used, just by time, this increases to what I have now, almost 1.9GB used.
What versions of MySqld and Bind?
how do i check that jesper
> I am just left with almost 100Mb out of 2GB of RAM
how do you check that?

BTW, is the initial questione answered now?
yes ahoffmann, just awaiting for the proper time (off-peak) to test everything together ..
as part of my script was to restart mysql after backup is over, which I am trying to figure a better command for it .. so the script becomes complete ..
sorry missed your first question
I use "top" command
BIND
  dig @your.nameserver.ip chaos txt version.bind

MYSQL
  rpm -qa | grep mysql-server
    or
  grep Version /var/log/mysqld.log
BIND
==============================
; <<>> DiG 9.2.4 <<>> @my.nameserver.ip chaos txt version.bind
; (1 server found)
;; global options:  printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4173
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 0

;; QUESTION SECTION:
;version.bind.                  CH      TXT

;; ANSWER SECTION:
version.bind.           0       CH      TXT     "9.2.4"

;; AUTHORITY SECTION:
version.bind.           0       CH      NS      version.bind.

;; Query time: 0 msec
;; SERVER: xx.xx.xx.xx#xx(xx.xx.xx.xx)
;; WHEN: Mon Aug  6 14:29:15 2007
;; MSG SIZE  rcvd: 62
===============================

MYSQL
===============================
Version: '4.1.21-standard'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Edition - Standard (GPL)
===============================
mysql and bind both have memory leaks of some sort.  Whether you're hitting them is under question.

I would recommend first downloading, installing and runing mysqld under valgrind to check for memory problems for each application.

Those are both really old versions.
although valgrind slows down the programs, but I might give it a try sometimes at midnight ..
so for now, which restart would refresh the memory? I think a reboot would do, but isn't there an easier way? restarting a specific service!

Thanks for your time :)
I would think that stopping and starting a service would free memory up which is what 'service mysqld restart' does (stop; start).

Have you tried it and does it help?
yes try that every night after backup .. but once started, I notice save memory status when I run "top" command before and after restart ..
I just see memory status refreshed when the server is no longer have memory available and goes down, then a program which checks services starts it again .. at that time only, I see the memory is in good shape ..
Do you see any other services other than mysqld failing to run and having to be restarted?
normally apache go down first most of time, but rarely mysqld go first .. these two only are affected most of the time
Are you running fast cgi within apache or extensive perl scripts?  and what version of apache?

/path/to/httpd -v
I only have a php forum running ..
I couldn't get the apache version using httpd -v
I get the following:
==============
root@server [~]# /etc/rc.d/init.d/httpd -v
usage: /etc/rc.d/init.d/httpd (start|stop|restart|fullstatus|status|graceful|configtest|help)

start      - start httpd
startssl   - start httpd with SSL enabled
stop       - stop httpd
restart    - restart httpd if running by sending a SIGHUP or start if
             not running
fullstatus - dump a full status screen; requires lynx and mod_status enabled
status     - dump a short status screen; requires lynx and mod_status enabled
graceful   - do a graceful restart by sending a SIGUSR1 or start if not running
configtest - do a configuration syntax test
help       - this screen
==============
apache will be located in:

/usr/sbin/httpd
/usr/local/sbin/httpd

if you are root or  you have all of the executable paths set as a regular user, you can type 'which httpd' to find the actual path.

or from root, you can type 'find /usr -name httpd'

the httpd in /etc/rc.d/init.d is just the script to start/restart/stop the daemon
root@server [/]# /usr/local/apache/bin/httpd -v
Server version: Apache/1.3.37 (Unix)
Server built:   Apr 25 2007 04:08:19
Short of upgrading or running valgrind to determine the memory leaks (and probably upgrade what's needed), you are left with stopping and starting services to circumvent the problem.

Are you using yum, apt-get or ports to stay up with new versions/revisions?
I will check with helpdesk as I have a managed server, and should take care of upgrade and all other stuff.  maybe this will solve the problem.

As you said, I will keep restarting the services for a while till the problem gets fixed
Thanks, mafairuz.  I'll keep an eye out (my timezone is CDT) so, if you run into a problem, I'll check just as soon as I can.
> I use "top" command
.. and you're talking about the amount of memory reported as free in the first 5 line?
If so, and you're on Linux, then this should report a value near 0 (zero), that's a good configured Linux then ;-)
yes ahoffmann :)

have a look here:
============
Tasks: 127 total,   1 running, 108 sleeping,   1 stopped,  17 zombie
Cpu(s): 29.0% us, 12.0% sy,  0.1% ni, 56.1% id,  2.7% wa,  0.1% hi,  0.0% si
Mem:   2074736k total,  1925180k used,   149556k free,   209620k buffers
Swap:  2048276k total,   152264k used,  1896012k free,  1266956k cached
============

do you think this is good ?!
if it is a Linux system, it's not that bad ;-)
if it is not a Linux system, then you should check for memory leaks
it is almost off-peak started an hour ago .. but I think it gets worse at peak hours
yes it is linux system
what does 'vmstat 2' running iteratively for a count of about 30 lines indicate?
was that q. for me jesper ? as I have no idea at all :)
what is "worse"?
I mean the load on server ahoffmann ..
yes, mafairuz!  please run 'vmstat 2' and let it go for about 30 lines.  then we can see if you are swapping, have enough CPU, what the memory is.  sometimes this helps.
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 0  1 152256 165908 211908 1229476    6    6    41   178    1     3 29 12 56  3
 2  1 152256 170948 211908 1229728    4    0    62  1012 1405  1051 38 16 31 14
 0  0 152256 168124 211908 1229728    0    0     2   900 1405   852 19 10 59 11
 0  0 152256 179516 211908 1229728    0    0     0     0 1278   531 17 10 73  0
 0  0 152256 179052 211908 1229728    0    0     4    20 1364  1311 36 17 46  1
 0  0 152256 186404 211908 1229728    0    0     2  1616 1392   999 31 16 42 10
 0  0 152256 178980 211912 1229724    0    0    12   304 1189   526 15  5 78  2
 3  0 152256 159332 211924 1229712    0    0    14    20 1195   990 33 15 52  1
 0  0 152256 194852 211924 1229972    0    0    12   626 1281   568 23 11 61  5
 1  0 152256 126820 211924 1262212    0    0     2     0 1197  1123 31 12 57  1
 2  0 152256 158948 211932 1252584    0    0     0    70 1218   599 20 12 68  0
 5  0 152256  88420 211932 1285344    0    0    90   412 1214  1067 44 14 40  4
 2  0 152256 177516 211940 1230216    0    0    26    32 1232   575 22 12 65  1
 0  0 152256 177132 211940 1230216    0    0     0   488 1179   574 17  7 76  0
 0  0 152256 177036 211940 1230216    0    0     4    24 1201   361 10  5 85  1
 0  0 152256 160524 211948 1230208    0    0     6    16 1168   694 20  7 73  1
 0  0 152256 155852 211948 1230468    0    0    22   478 1274   805 29 11 60  0
 0  0 152256 170372 211948 1230208    0    0     2   208 1281   459 12  6 81  1
 0  0 152256 175364 211948 1230208    0    0     2     0 1141   511 14  9 77  1
 1  0 152256 165956 211948 1230208    0    0    28     0 1146   613 26  8 64  3
 0  0 152256 172228 211948 1230468    0    0    16   438 1227  1373 43 21 34  2
procs -----------memory---------- ---swap-- -----io---- --system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in    cs us sy id wa
 4  0 152256  89220 211948 1295988    0    0    12   736 1243   873 26  9 63  2
 0  0 152256 177292 211948 1230468    0    0    16   574 1197   728 22 13 63  1
 0  0 152256 158412 211956 1230460    0    0    12   654 1180   884 19  7 73  2
 1  0 152256 159244 211956 1230460    0    0    14     0 1199   564 26  8 64  1
 0  0 152256 175564 211964 1230452    0    0     0    18 1222   673 20 10 70  0
 0  0 152256 166156 211964 1230452    0    0     8   576 1271   872 24  9 67  0
 1  0 152256 155404 211964 1230452    0    0     0    24 1199   383 11  4 85  0
 0  0 152256 151564 211972 1230444    0    0     0    16 1183   883 25  9 67  0
 2  0 152256 134540 211972 1230704    0    0    20   438 1208   617 18  5 75  2
 2  0 152256 147804 211976 1238760    0    0     0    16 1340   708 23 12 65  0
 0  1 152256 165652 211976 1230700    0    0     0   176 1318   834 22 11 67  0
At this moment, it doesn't look too bad.  user cpu is moderate but there is no swapping in and out of memory right now.
as I said .. it is off-peak .. it gets heavy at morning where the server normally is unable to manage the load and goes down ..
I think I've taken much of your time guys with this issue .. I will add a new separate question another time if I see no improvement I will have after upgrading the software ..

few more minutes and will get back to you with results of the script and file transfer we've done so far.
alright
script worked as needed :)

Thanks guy, I will split the points by half as both your answers have helped me out.
Appreciate it
125 points is an odd number, so has to be 63 & 62 :)
Thanks again