Executing Commands Remotely with SSH

Posted by John
on Sunday, 05 October 2008

Turns out you can actually run commands remotely via SSH, plant some scripts on your server and boom you can clear your logs, backup your db and restart your webservers all without having to remote in; major help!

For example;

ssh -i /home/me/.ssh/mybox root@mybox.com '/var/www/apps/myapp/start-myapp.sh'

Here I'm specifying an ssh key file to use to connect to the remote box via the -i parameter, then the username root (because we want the script to have all the power it needs to without specifying sudo; web address of the box (or you can use the ip address). Then surrounding the command I want to run in single-quotes.

Simple effective and saves me from remoting in one night when I'm not myself, hitting the wrong key & doing something I really shouldn't ;-)

Freezing Rails

Posted by John
on Tuesday, 18 December 2007

Sometimes with remote server's you may have to supply the files relating to the current version of rails your writing for in order to run your app on someone else's server, like mosso.com.

Freezing Rails

So in Terminal, while in your Rails apps directory run:

rake rails:freeze:gems

This will unpack the rails version your using into the /vendor/rails directory of your app.

From there you can upload it to your server and it should just work.

Thawing Out

To reverse the process...

rake rails:unfreeze

This removes the frozen version from your vendor/rails directory.

Picking Up Changes

Also note new rails versions come with config file changes, to pick these up run:

rake rails:update:configs

Freezing to the Edge

You can also freeze (or grab a local development copy) of rails via...

rake rails:freeze:edge

Or Freeze to a specific release with...

rake rails:freeze:edge TAG=rel_2-0-2

This freezes to Rails 2.0.2

Also for...

You can also do this for individual gems, by first grabbing the gemsonrails gem...

sudo gem install gemsonrails

Then moving to your Rails app directory and running...

gemsonrails

This installs some extra RAKE tasks into your app, so now you can freeze a local copy of say the RedCloth gem into your app via...

rake gems:freeze GEM=RedCloth

Rsync + SSH quickly

Posted by John
on Monday, 29 October 2007

Another little mini article on automating file remote file transfers with RSYNC + SSH.

RSYNC

RSYNC is a Linux tool that allows you to copy all the files from the source to the destination, but copies only the one's which have changed between them; limiting the data you have to send rather than doing a blanket copy all.

SSH

SSH is a tool that allows you to gain remote access to a server from your computer.

RSYNC + SSH

Put these two together and you'll be able to copy files from your pc to a remote box as seemlessly as you do a COPY * from the command prompt.

Creating the Keyfile

First off it's good to stick your .SSH keys in the .SSH directory rather than all over the place so type...

CD .SSH

...From the terminal to navigate to your .SSH directory.

Now type...

ssh-keygen -d

This will generate a stronger SSH key. When asked for the name of your key type 'STORAGE' as we'll use this to backup our files to somewhere else. Don't enter a password if you want it to not prompt you for one everytime you do a transfer.

Two files will be created, a private key, and a public .pub public key. These will be both binded to your PC and wherever you upload the public key file you will have access to from your PC.

Copy to your Remote Box

Now use WinSCP, TRANSMIT or some FTP tool to access your remote box and create a .SSH directory in it's root.

Create a text file called 'authorized_keys' with no extension and copy-n-paste the contents of your .ssh/storage.pub file into this text file on your remote box and save it.

Now you will have access to the remote box from your PC.

The RSYNC Command

Now to copy files from your PC to the remote box type this complex RSYNC command...

rsync -azvCL --exclude=.DS_Store --progress -e "ssh -i .ssh/storage" * user@mybox.com:/home/backup/

Let's break this down...

  • -azvCL, this tells RSYNC to compress the data across the connection if it can so if your sending 22mb of text data it'll take less time; plus it'll recursively copy all the files and directories you choose.

  • --exclude, defines what files to exclude in the transfer, for this i'm on a MAC box and I want to exclude the file thumbnails OSX creates.

  • --progress, tells rsync to show us interactively the progress of our transfers.

  • *-e*, is the important bit, this tells rsync to use SSH and where the keyfiles are located.

  • Replace USER with the username for your remote box, and storage.com with the domain name or IP address of your remote box.

  • :/home/backup/ tells RSYNC to copy the files to the backup directory located in the base of your remote box.

The beauty with RSYNC over FTP is that if the transmission breaks half-way it won't copy the file, so you won't be left with half of a file on your remote server thinking it's the full copy; and with all these options enabled it's hellishly quicker.

P.S. Use Markdown + SmartyPants for semantically correct XHTML.

Enjoy!