Skip to navigation

Rsync: syncing back and forth using only one single ssh connection

Rsync is really handy for transferring files and keeping directories in sync, and it’s damn fast. If you transfer only very little, what takes longest is actually establishing the SSH connection to the server. While this is usually negligible, it has still irked me. Some googling turned up this post. Nice. One SSH tunnel for rsyncing back and forth.

Here’s a little example for a shell script I use to sync my mutt directory up and down:

if `ping -c1 $server > /dev/null 2>&1` ; then                                          
    ssh -S $HOME/.tunnel.socket -M -N -f $server  # create shared connection                       
    $HOME/bin/muttup && $HOME/bin/muttdown          # sync up and down                               
    ssh -q -S $HOME/.tunnel.socket -O exit $server 2> /dev/null # close shared                     
                                                                  # connection                       
fi

I’ve set this to run whenever mutt starts, to keep my mutt stuff synced across different machines. (muttup and muttdown are obviously other tiny scripts. I keep them separately so I can run them individually)

Comments are closed.