Upload using SSH and rsync

Jul 23, 2010

It should be really easy to publish website content to a server. One way to do this is by using SSH and rsync. Start by configuring SSH to work with keys rather than a password:

Via the terminal login using a password to establish that the connection works, and that you are were you expect to be:

ssh username@domain.org

Type the password, and ‘yes’ when prompted to access the ‘unknown’ host. Then, assuming that the local machine has already been set up with private and public ssh keys, type:

cat ~/.ssh/id\_rsa.pub | ssh username@domain.org 'cat - >> ~/.ssh/authorized\_keys'

You need to type the password when prompted. It is then possible to login with ssh username@domain.org.

But even that can be trimmed down. So the final step for me was to add a shell alias to speed up access a little bit: alias domainname="ssh username@domain.org" Which I added to my shell profile, found here: ~/.profile\

Rsync automated uploading of changes to server.

# This script assumes that ssh access using keys is already in place.
# Useful urls:
# http://ss64.com/bash/rsync.html
# http://www.unix.com/unix-dummies-questions-answers/45839-problems-using-rsync-leopard-ssh-seeking-solution-alternative.html 
# Some rsync options explained
# -v verbose (add more v's for more detail)
# -a archive mode (same as -rlptgoD )
# -c
# -n dry run
# -r recursive
# -z compress during transfer
# --delete-after


function upload_to_server () {
  echo "Uploading $1"
  rsync -e ssh -avcz --exclude '.DS\_Store' --exclude /\*.tmproj $1/html/ $MT:/home/gridnumber/domains/$1/html/
  echo " "
  echo " "