#!/bin/bash #------------------------------------------------------------ # Author : John Bender and Mitchell Hashimoto # Description : Runs the `unison` folder synchronization # utility for shared folders. #------------------------------------------------------------ #------------------------------------------------------------ # Argument verification #------------------------------------------------------------ if [ $# -ne 3 ]; then echo "Usage: `basename $0` from_folder to_folder options" exit 1 fi # Sanitization function which turns any non-alphanumeric char # into a dash '-' and returns it by echoing the result. function sanitize() { local DATA=$1 DATA=${DATA//[^a-zA-Z0-9_-]/-} echo ${DATA} } #------------------------------------------------------------ # "Configuration" #------------------------------------------------------------ # TODO Change lockfile to depend on the from/to folder to # allow multiple syncs of diff folders to happen at once. FROM=$1 TO=$2 OPTIONS=$3 LOCK_FILE=`basename $0`-$(sanitize ${TO}).lck #------------------------------------------------------------ # Am I Running? #------------------------------------------------------------ if [ -f "${LOCK_FILE}" ]; then # The file exists, verify its running and if so exit. OUR_PID=`head -n 1 "${LOCK_FILE}"` TEST_RUNNING=`ps -p ${OUR_PID} | grep ${OUR_PID}` if [ "${TEST_RUNNING}" ]; then # The process is running, echo and exit. echo "Unison sync already running. [PID: ${OUR_PID}]" exit 0 fi fi # We're not running if we reached this point, so lock # it up. echo $$ > "${LOCK_FILE}" #------------------------------------------------------------ # The Meat #------------------------------------------------------------ echo "Beginning Sync:" echo " -- From: ${FROM}" echo " -- To: ${TO}" echo " -- Options: ${OPTIONS}" while [ 1 ]; do echo "Syncing: $(date)" # TODO check result and output log data... somewhere! sudo unison ${FROM} ${TO} ${OPTIONS} sleep 1 done #------------------------------------------------------------ # Cleanup and Exit #------------------------------------------------------------ rm -f "${LOCK_FILE}" exit 0