Skip to content

Instantly share code, notes, and snippets.

@pyokagan
Created October 7, 2012 07:06
Show Gist options
  • Select an option

  • Save pyokagan/3847379 to your computer and use it in GitHub Desktop.

Select an option

Save pyokagan/3847379 to your computer and use it in GitHub Desktop.
sshinotifytransfer - Transfers files in one direction
#! /bin/bash
#Transfers files in one direction
usage="$0 remote dest"
if [[ !( -n $1 ) || !( -n $2 ) ]]; then
echo $usage >&2
exit 2
fi
lockfile="/var/lock/sshinotifytransfer_${1//\//_}_${2//\//_}"
rsynclockfile="/var/lock/rsync_${1//\//_}_${2//\//_}"
sshhost="${1%%:*}"
sshdest="${1#*:}"
(
flock -n 9 || exit 1
#Check if the required commands exist on the host SSH Server
if [[ `ssh $sshhost 'which inotifywatch 2>&1 >/dev/null; echo $?' | tr -d ' '` != '0' ]]; then
echo "Host $sshhost does not have inotifywatch. Install inotify-tools." >&2
exit 1
fi
#First start rsync
flock $rsynclockfile -c "rsync -a --progress --remove-source-files $1 $2"
#Then start the loop
ssh $sshhost "inotifywait -m -r -q -e moved_to -e close --format '%f' '$sshdest'" | while read filename
do
flock $rsynclockfile -c "rsync -a --progress --remove-source-files $1 $2"
done
) 9> $lockfile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment