Help with finishing touch on script to upload C5 database automatically through shell script.

Permalink
I have been working on a shell script (using bash) to upload my C5 database from localhost to my hosting server.

I've almost got it except that I have never run commands remotely through SSH before.

Does anyone have any experience with this such that they could help me combine the following two commands into one?

# we login to our server through SSH
ssh -v -p 2222 carlos@<domain_name>
# lastly we run the mysql program remotely to import it into our
# hosted mysql database
mysql -u carlos -p<password> <database_name_on_server> < junk.sql


junk.sql is the dump of the database locally that has already been transferred by ftp.

The problem I am running into is that the SSH program asks me for a password and then leaves me in the SSH shell. When I exit the shell the next line (the mysql line) runs but since I am not longer connected it obviously fails.

Is there a way to connect through SSH without it stopping me to ask for a password such that I stay connected?

I know how to run an SSH command remotely in principle but I can't get around this password request on the part of SSH.

The total line would be..

ssh -v -p 2222 carlos@<domain_name> 'mysql -u carlos -p<password> <database_name_on_server> < junk.sql'


The beauty of this method is that I don't have to fiddle with phpmyadmin or anything.

Just run the script. If I can get it to work by getting around the SSH password obstacle.

Any input would be appreciated.

Carlos

 
triplei replied on at Permalink Best Answer Reply
triplei
Hey Carlos,

This sounds like a perfect place to use ssh-keys to do the authentication instead of passwords. A long time ago I wrote a quick article here:http://danklassen.com/wordpress/2008/05/creating-ssh-keys/... which runs through the general process.

Hopefully that points you in the right direction and can get things working for you.

Sounds like you'll have a handy script there... the only challenge I would see would be if there were changes made on live during the time since you're last pull which would get overwritten when you re-import your local copy.
carlos123 replied on at Permalink Reply
That's exactly what I need triplei! Thanks for the link to your article. I will get right to it as soon as I post my reply.

The only problem I see with what you say at your article is that, although you won't need to enter a password anymore since ssh will now use keys, you will need to still enter your passphrase manually it would seem. Which will again interrupt the script.

I guess I will just skip the passphrase part to get around that.

Regarding changes being made live...since this particular site is my own and since any changes are made locally off my own Apache running off my localhost computer there will never be a conflict between changes made at the live site and any that I might have also made locally.

It's always going to be a one way street between my localhost copy of Apache and Concrete5 and my host server.

The reason I wrote this script was to avoid the hassle of opening up two instances of phpMyAdmin, exporting in the one, copying the SQL from one to the other, and importing into the other. Not to mention having to log in and out of both. A real hassle when a script will do it all...automatically.

Carlos
tomreitz replied on at Permalink Reply
tomreitz
Try this:
ssh -l <login_name> -p 2222 <your_ssh_server> 'mysql -u carlos -p<password> <database_name> < [path/to/]junk.sql'


If you're asking if there's a way to pass your password to ssh as a parameter (as you can with mysql), so you don't have to type it, I don't believe that's possible.

Alternately, put your mysql call in a .sh script on the server, then just
ssh -l  <login_name> -p 2222 <your_ssh_server> 'the_script.sh'
carlos123 replied on at Permalink Reply
Alas...my shared hosting plan does not allow the use of authentication keys used through the shell (though they do make available secure FTP).

Absolutely no logic to such other than they just don't make it available.

Oh well.

So much for that idea.

I'll have to dump it all, upload it through ftp, through my script and then log in through a secure shell and run the rest of the command manually.

Still a whole lot better than doing it manually through phpMyAdmin but doing it all through a script would have been real nice.

Thanks for the input you all.

Carlos