Mirroring My Home Directory

One of the things I needed to get going today was the regular mirroring of my Chicago home directory to my new London home directory. In the past, there has been only one, but with the disaster/recovery tests requiring that we take the NFS servers offline, if I want to log into my account in London, I needed to have a local home directory there so that it didn't choke trying to get back to the Chicago file servers.

Once I got word that things were set up properly in London, I checked to make sure that I knew the destination directory - accessible from Chicago, and then I whipped up a little script that would make it very easy to keep things in sync:


#
# Specify the source and the destination
#
srcDir="/home"
target="beatyro"
destDir="/final/London/fileserver/directory/"
# make sure we have the source directory or bail
if [ ! -d "${srcDir}/${target}" ]; then
  echo "You don't have the source directry!"
  exit 0
fi
# ...make sure we have the destination too
if [ ! -d "${destDir}" ]; then
  echo "You don't have the destination!"
  exit 0
fi

# run the command to update the home directory
cd ${srcDir}
rsync -aREpogtxz --delete --exclude='.snapshot*' \
      --exclude='.nfs*' --exclude='.wastebasket' \
      --exclude='.Trash' "${target}" "${destDir}"

I've used something like this guy for a while now, mirroring my MacBook Pro's Users directory to an external drive. The idea is simple, exclude what you don't want, and everything else will automatically get mirrored. By adding in the --delete I made sure that if I removed something from Chicago, it was removed from London too. No need to have stragglers in London.

I like the fact that rsync is capable of doing a lot more than simply working between machines in a client/server mode. In several cases, there's no need to have two machines - you just want to make this directory mirrored on this base path. Simple. Clean.

Add this into a simple, nightly crontab job and every day I'll be sure to keep things in sync. Nice.