rsync

Rsync is a tool to synchronize data. Before you read this tutorial, you should understand the conditions from which I am writing this.

My situation is as follows:

  • I use two separate machines to update this website.
  • I need a “fast” tool to read/write “changed” files.
  • I need to do this over an SSH (secure shell) connection.

Both of my development boxes are either running FreeBSD or Ubuntu, so under FreeBSD, I compiled rsync from ports.

cd /usr/ports/net/rsync
make install clean

*Be sure to compile with the “Use SSH for RSH connections” option.

Once rsync is compiled, you are ready to create a script. Again, keep in mind I am using this tool to connect to a remote hosting company server to update my website. You will have to change what I have posted here. I have included #comments.

My sample `webdevget’ script updates files FROM my server TO my development machines.

#!/bin/sh

echo "Starting connection"
/usr/local/bin/rsync \
 -razv \
 --delete \
 --progress \
 --exclude=logs \
 --exclude=*~ \
 username@hosting.com:/home/username/* \
 /home/remote/files
 echo "Connection closed."
 echo "Done!"

What this script does, line by line:

#!/bin/sh

# Acknowledge the script has been initialized
echo "Starting connection"
# start `rsync' with the 'recursive', 'archive',
# 'compress', and 'verbose' flags, excluding any
# logs and backup (~) files
/usr/local/bin/rsync \
 -razv \
 --delete \
 --progress \
 --exclude=logs \
 --exclude=*~\

# log in to 'hosting.com' with 'username' (you will be
# prompted for a password), and copy all files from
# '/home/username/' to the '/home/remote/files' directory.
username@hosting.com:/home/username/* \
/home/remote/files

# Acknowledge the script has been ended
echo "Connection closed."
echo "Done!"