Thursday, October 27, 2011

rsync & rsync via SSH

As you know, rsync may work with it's native rsyncd server and via SSH. If source or destination contains '::' this addresses rsync' mountpoint. Otherwise it works via SSH using cryptographic channel.
Anyway, we use both of them. And there are many cases then you have expressions like:
#!/usr/bin/env bash

URL1=xxx.yyyy.com::point/file1.txt # native rsync
URL2=xxx.yyyy.com:some_folder/file2.txt # rsync-ssh
#... 
rsync --contimeout 10 --timeout 40 $URL1 file1.txt
rsync -e ssh "-o BatchMode=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o ServerAliveInterval=40" $URL2 dir/

# same things many times

As you can see, we have to use different syntax for native- and ssh-rsync: --contimeout is available only for native case. Really bad news, especially if I want to omit 'rsync' and it's options each time. Should I know what type of URI I'll got in specific call?!. No! It's easy to rewrite it as follows:

SSH_OPTS="-o BatchMode=yes -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no -o ConnectTimeout=10 -o ServerAliveInterval=40"

# select rsync or rsync-ssh syntax automatically
function RSYNC
{
  local looks_like_rsync=

  for a in "$@"
  do
    if [[ $a =~ :: ]]; then
      looks_like_rsync=yes
      break
    fi
  done

  if [[ -n $looks_like_rsync ]]; then
    rsync --contimeout=10 --timeout=40 -tv "$@"
    return $?
  fi

  rsync -tv -e "ssh $SSH_OPTS" "$@"
}

No comments:

Post a Comment